anytv-commons
Advanced tools
Comparing version 0.0.4 to 0.0.5
225
index.js
@@ -8,2 +8,4 @@ 'use strict'; | ||
var cudl = _interopDefault(require('cuddle')); | ||
var anytvCommons = require('anytv-commons'); | ||
var cld = _interopDefault(require('cld')); | ||
@@ -247,3 +249,3 @@ class ExtendableError extends Error { | ||
class Pipe extends EventEmitter { | ||
class Pipe$1 extends EventEmitter { | ||
@@ -304,3 +306,3 @@ constructor (name) { | ||
class Plumber extends Pipe { | ||
class Plumber extends Pipe$1 { | ||
@@ -641,5 +643,5 @@ constructor (config, args) { | ||
class Grouper extends Pipe { | ||
class Grouper extends Pipe$1 { | ||
constructor (max, mseconds) { | ||
constructor (max, seconds = 10) { | ||
@@ -649,9 +651,12 @@ super('Grouper'); | ||
this._max = max; | ||
this._mseconds = mseconds; | ||
this._stack = []; | ||
this._now = Date.now(); | ||
this._seconds = seconds; | ||
this._timer = 0; | ||
this.receive = this.receive.bind(this); | ||
this._regulate = this._regulate.bind(this); | ||
setInterval(this._regulate, 1000); | ||
} | ||
@@ -664,7 +669,4 @@ | ||
if (!this._timeout) { | ||
this._timeout = setTimeout(this._regulate, this._mseconds); | ||
} | ||
if ((this._now + this.mseconds) < Date.now() || this._stack.length >= this._max) { | ||
while (this._stack.length >= this._max) { | ||
this._timer = this._seconds; | ||
this._regulate(); | ||
@@ -678,16 +680,203 @@ } | ||
this._now = Date.now(); | ||
this._timer++; | ||
if (this._timeout) { | ||
if (this._timer >= this._seconds && this._stack.length) { | ||
this._timer = 0; | ||
this._total++; | ||
this.emit_done(this._stack.splice(0, this._max)); | ||
} | ||
} | ||
} | ||
function pad (num, size) { | ||
return ('000000000' + num).substr(-(size || 2)); | ||
} | ||
/** | ||
* Detects language of the item's description or title | ||
* Attaches `written_language` on the item | ||
*/ | ||
class CLD extends anytvCommons.Pipe { | ||
constructor () { | ||
super('CLD'); | ||
this.receive = this.receive.bind(this); | ||
this._extract_language = this._extract_language.bind(this); | ||
} | ||
// @@override | ||
receive (items) { | ||
this.debug('Received', items); | ||
for (let i = 0; i < items.length; i += 1) { | ||
const item = items[i]; | ||
if (item.flag | ||
&& this._is_done_for_today(item)) { | ||
continue; | ||
} | ||
items.splice(i, 1); | ||
// decrement index because the array length was also decremented | ||
i -= 1; | ||
this._total++; | ||
this.emit_done(this._stack.splice(0, this._stack.length)); | ||
this._detect(item); | ||
} | ||
clearTimeout(this._timeout); | ||
this._timeout = null; | ||
if (items.length) { | ||
this._total++; | ||
this.debug(`passing ${items.length}`); | ||
this.emit_done(items); | ||
} | ||
} | ||
_is_done_for_today (item) { | ||
return item.flag | ||
? pad(item.flag, 10)[5] === '1' | ||
: false; | ||
} | ||
_detect (item) { | ||
this.debug(`Extracting language of ${item.item_id}`); | ||
cld.detect(item.description || item.title, this._extract_language(item)); | ||
} | ||
_extract_language (item) { | ||
return (err, lang) => { | ||
item.written_language = lang | ||
&& lang.languages | ||
&& lang.languages.length | ||
&& lang.languages[0].name | ||
? lang.languages[0].name.toLowerCase() | ||
: ''; | ||
this.debug(`Done extracting language of ${item.item_id}; ${item.written_language}`); | ||
this.emit_done([item]); | ||
}; | ||
} | ||
} | ||
function get_page_token (index) { | ||
const alphanum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'; | ||
const i = index; | ||
let first_shifter = 0; | ||
let second_shifter = 0; | ||
/** | ||
* FIRST | ||
* > 0 ABCDEFGHIJKLMNOP | ||
* > 256 IJKLMNOP | ||
*/ | ||
let first_idx = ~~(i / 16); | ||
if (i >= Math.pow(2, 8)) { | ||
first_idx = first_idx % 8; | ||
first_shifter = 8; | ||
} | ||
/** | ||
* SECOND | ||
* let a = 2^13 | ||
* > 0 AEIMQUYcgkosw048 shift 0 | ||
* > a BFJNRVZdhlptx159 shift 1 | ||
* ~~(i/a) a is even CGKOSWaeimquy260 shift 2 | ||
* ~~(i/a) is odd DHLPTXbfjnrvz37- shift 3 | ||
*/ | ||
let second_idx = i % 16; | ||
if (i > Math.pow(2, 13)) { | ||
second_shifter = 1; | ||
if (i > (Math.pow(2, 13) * 2)) { | ||
second_shifter += (~~(i/Math.pow(2, 13)) % 2) ? 2 : 1; | ||
} | ||
} | ||
/** | ||
* THIRD | ||
* @todo learn the pattern for the suffix | ||
* ABCDEFGHIJKLMNOP | ||
* QRSTUVWXYZabcdef | ||
* ghijklmnopqrstuv | ||
* wxyz01234567890- | ||
* let a = (2^7) = 128 | ||
* > 0 0 Q = index 16 | ||
* > a 128 [alphanum] + E = index 4 | ||
* > a^2 16384 [alphanum] + AR = index 1 18 | ||
* > a^2 * 2 32768 [alphanum] + Ah = index 1 34 34-18 = 16 | ||
* > a^2 * 3 49152 [alphanum] + Ax = index 1 50 50-34 = 16 | ||
* > a^2 * 4 65536 [alphanum] + BB = index 2 2 66-50 = 16 | ||
* > a^2 * 5 81920 [alphanum] + BR | ||
* > a^2 * 6 98304 [alphanum] + ?? might be Bh | ||
* > a^2 * 7 114688 [alphanum] + ?? might be Bx | ||
* > a^2 * 8 131072 [alphanum] + ?? might be CB | ||
*/ | ||
let last_idx = ~~(i / Math.pow(2, 7)); | ||
let last = alphanum[last_idx % alphanum.length]; | ||
let last_suffix; | ||
if (!last_idx) { | ||
last = ''; | ||
last_suffix = 'Q'; | ||
} | ||
if (i >= 128) { | ||
last_suffix = 'E'; | ||
} | ||
if (i >= 16384) { | ||
last_suffix = 'AR'; | ||
} | ||
if (i >= 32768) { | ||
last_suffix = 'Ah'; | ||
} | ||
if (i >= 49152) { | ||
last_suffix = 'Ax'; | ||
} | ||
if (i >= 65535) { | ||
last_suffix = 'BB'; | ||
} | ||
if (i >= 81920) { | ||
last_suffix = 'BR'; | ||
} | ||
if (i >= 98304) { | ||
throw new Error(`PageToken last suffix unknown for >= 98304. | ||
Figure out the last suffix by inspecting nextPageToken of page ${get_page_token(98303)} with maxResults: 1`); | ||
} | ||
last += last_suffix; | ||
// build token | ||
// C and AA looks constant | ||
return [ | ||
'C', | ||
alphanum[first_idx + first_shifter], | ||
alphanum[(((second_idx % 16) * 4) + second_shifter) % alphanum.length], | ||
last, | ||
'AA' | ||
].join(''); | ||
} | ||
exports.ExtendableError = ExtendableError; | ||
@@ -699,3 +888,5 @@ exports.Config = Config; | ||
exports.Logger = Logger; | ||
exports.Pipe = Pipe; | ||
exports.Pipe = Pipe$1; | ||
exports.Grouper = Grouper; | ||
exports.CLD = CLD; | ||
exports.PageToken = get_page_token; |
{ | ||
"name": "anytv-commons", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Collection of libraries commonly used", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
21611
646