opensubtitles-api
Advanced tools
Comparing version 4.1.0 to 5.0.0
@@ -38,4 +38,4 @@ const fs = require('fs') | ||
let chunk_size = 65536 //64 * 1024 | ||
let buf_start = new Buffer(chunk_size * 2) | ||
let buf_end = new Buffer(chunk_size * 2) | ||
let buf_start = Buffer.alloc(chunk_size * 2) | ||
let buf_end = Buffer.alloc(chunk_size * 2) | ||
let file_size = 0 | ||
@@ -42,0 +42,0 @@ let checksum |
@@ -20,2 +20,13 @@ const OS = require('./opensubtitles.js') | ||
optimizeQueryTerms(input) { | ||
// parse | ||
const normalize = this.normalizeProt(); | ||
input.imdbid = input.imdbid && parseInt(input.imdbid.toString().replace('tt', ''), 10) | ||
input.season = input.season && parseInt(input.season) | ||
input.episode = input.episode && parseInt(input.episode) | ||
input.hash = input.hash && input.hash.toString().length >= 32 && input.hash.toString().toLowerCase() | ||
input.filesize = input.filesize && parseInt(input.filesize) | ||
input.fps = input.fps && input.fps.toString() | ||
input.filename = input.filename || input.path && path.basename(input.path) | ||
return this.checkHash(input).then(obj => { | ||
@@ -25,10 +36,18 @@ const output = Array() | ||
if (obj) { // first data call | ||
output[i] = obj | ||
if (obj) { | ||
input.hash = obj.moviehash.toLowerCase() | ||
input.filesize = parseInt(obj.moviebytesize) | ||
} | ||
// first data call | ||
if (input.hash || input.filesize) { | ||
output[i] = { | ||
moviehash: input.hash && input.hash, | ||
moviebytesize: input.filesize && input.filesize.toString() | ||
} | ||
i++ | ||
} | ||
if (input.filename || input.path) { // second data call | ||
if (input.filename) { // second data call | ||
output[i] = Object() | ||
output[i].tag = input.filename || path.basename(input.path) | ||
output[i].tag = input.filename | ||
i++ | ||
@@ -39,7 +58,7 @@ } | ||
output[i] = Object() | ||
output[i].imdbid = input.imdbid.toString().replace('tt', '') | ||
output[i].imdbid = input.imdbid.toString() | ||
if (input.season && input.episode) { | ||
output[i].season = parseInt(input.season).toString() | ||
output[i].episode = parseInt(input.episode).toString() | ||
output[i].season = input.season.toString() | ||
output[i].episode = input.episode.toString() | ||
} | ||
@@ -54,4 +73,4 @@ i++ | ||
if (input.season && input.episode) { | ||
output[i].season = parseInt(input.season).toString() | ||
output[i].episode = parseInt(input.episode).toString() | ||
output[i].season = input.season.toString() | ||
output[i].episode = input.episode.toString() | ||
} | ||
@@ -88,8 +107,6 @@ i++ | ||
} | ||
optimizeSubs(response, input) { | ||
// based on OpenSRTJS, under MIT - Copyright (c) 2014 Eóin Martin | ||
let fileTags | ||
let fileTagsDic = Object() | ||
let inputTags | ||
let inputTagsDic = Object() | ||
const normalize = this.normalizeProt() | ||
@@ -99,5 +116,7 @@ const matchTags = (sub, maxScore) => { | ||
if (!fileTags) fileTags = normalize(input.filename).toLowerCase().match(/[a-z0-9]{2,}/gi) | ||
if (!fileTags.length) return 0 | ||
if (!inputTags) | ||
inputTags = input.filename && normalize(input.filename).toLowerCase().match(/[a-z0-9]{2,}/gi) | ||
if (!inputTags || inputTags.length <= 2) return 0 | ||
const subNames = normalize(sub.MovieReleaseName + '_' + sub.SubFileName) | ||
@@ -108,8 +127,8 @@ const subTags = subNames.toLowerCase().match(/[a-z0-9]{2,}/gi) | ||
for (let tag of fileTags) fileTagsDic[tag] = false | ||
for (let tag of inputTags) inputTagsDic[tag] = false | ||
let matches = 0 | ||
for (let subTag of subTags) { // is term in filename, only once | ||
if (fileTagsDic[subTag] == false) { | ||
fileTagsDic[subTag] = true | ||
if (inputTagsDic[subTag] == false) { | ||
inputTagsDic[subTag] = true | ||
matches++ | ||
@@ -119,5 +138,4 @@ } | ||
return parseInt((matches / fileTags.length) * maxScore) | ||
return parseInt((matches / inputTags.length) * maxScore) | ||
} | ||
const subtitles = Object() | ||
@@ -131,19 +149,24 @@ | ||
// remove duplicate and empty | ||
var seen = {} | ||
response = response.filter(sub => { | ||
return sub && seen.hasOwnProperty(sub.IDSubtitle) ? false : (seen[sub.IDSubtitle] = true) | ||
}) | ||
return Promise.all(response.map(sub => { | ||
if (!sub || input.extensions.indexOf(sub.SubFormat) == -1) return | ||
// parse | ||
sub.imdbid = (sub.SeriesIMDBParent && sub.SeriesIMDBParent !== '0') | ||
? parseInt(sub.SeriesIMDBParent, 10) | ||
: sub.IDMovieImdb && parseInt(sub.IDMovieImdb, 10) | ||
sub.season = sub.SeriesSeason && parseInt(sub.SeriesSeason) | ||
sub.episode = sub.SeriesEpisode && parseInt(sub.SeriesEpisode) | ||
sub.filesize = parseInt(sub.MovieByteSize) | ||
sub.hash = sub.MovieHash != "0" && sub.MovieHash.toLowerCase() | ||
sub.fps = sub.MovieFPS && parseInt(sub.MovieFPS) > 0 && sub.MovieFPS.toString() | ||
// imdbid check | ||
if (input.imdbid) { | ||
let tmpId = parseInt(input.imdbid.toString().replace('tt', ''), 10) | ||
// check: extension, imdb, episode | ||
if ((input.extensions.indexOf(sub.SubFormat) == -1) | ||
|| (input.imdbid && input.imdbid != sub.imdbid) | ||
|| (input.season && input.episode && (input.season != sub.season || input.episode != sub.episode))) return | ||
if (sub.SeriesIMDBParent && sub.SeriesIMDBParent.toString() !== '0') { | ||
if (parseInt(sub.SeriesIMDBParent, 10) !== tmpId) return // tv episode | ||
} else { | ||
if (sub.IDMovieImdb && parseInt(sub.IDMovieImdb, 10) !== tmpId) return // movie | ||
} | ||
} | ||
// episode check | ||
if (input.season && input.episode && (sub.SeriesSeason !== parseInt(input.season).toString() || sub.SeriesEpisode !== parseInt(input.episode).toString())) return | ||
const tmp = { | ||
@@ -160,17 +183,20 @@ url: input.gzip ? sub.SubDownloadLink : sub.SubDownloadLink.replace('.gz', ''), | ||
fps: parseFloat(sub.MovieFPS) || null, | ||
format: sub.SubFormat | ||
format: sub.SubFormat, | ||
utf8: input.gzip ? sub.SubDownloadLink.replace('download/', 'download/subencoding-utf8/') : sub.SubDownloadLink.replace('.gz', '').replace('download/', 'download/subencoding-utf8/'), | ||
vtt: sub.SubDownloadLink.replace('download/', 'download/subformat-vtt/').replace('.gz', '') | ||
} | ||
// version | ||
if (input.hash && sub.hash == input.hash || input.filesize && input.filesize == sub.filesize) { | ||
tmp.score += 9 | ||
} else { | ||
tmp.score += matchTags(sub, 7) | ||
if ((input.fps && sub.fps) && (sub.fps.startsWith(input.fps) || input.fps.startsWith(sub.fps))) | ||
(tmp.score += 1) | ||
} | ||
// score calculations | ||
sub.MatchedBy === 'moviehash' && (tmp.score += 8) | ||
sub.MatchedBy === 'tag' && (tmp.score += 7) || (tmp.score += matchTags(sub, 7)) | ||
// rank | ||
sub.UserRank.match(/trusted|administrator/) && (tmp.score += 0.5) | ||
sub.UserRank.match(/platinum member|gold member/) && (tmp.score += 0.2) | ||
let matchByFPS = sub.MovieFPS && input.fps && parseInt(sub.MovieFPS) > 0 && (sub.MovieFPS.startsWith(input.fps) || input.fps.toString().startsWith(sub.MovieFPS)) | ||
sub.MatchedBy === 'imdbid' && (tmp.score += 5) && matchByFPS && (tmp.score += 0.5) | ||
!sub.MatchedBy.match(/moviehash|tag|imdbid/) && matchByFPS && (tmp.score += 2) | ||
sub.UserRank.match(/trusted|administrator/) && (tmp.score += 4) | ||
sub.UserRank.match(/platinum member|gold member/) && (tmp.score += 3) | ||
// store subs for sorting | ||
@@ -177,0 +203,0 @@ if (!subtitles[tmp.langcode]) { |
{ | ||
"name": "opensubtitles-api", | ||
"version": "4.1.0", | ||
"version": "5.0.0", | ||
"description": "opensubtitles.org api wrapper", | ||
@@ -23,4 +23,4 @@ "author": { | ||
"dependencies": { | ||
"xmlrpc": "1.x.x", | ||
"urijs": "1.x.x" | ||
"urijs": "^1.19.1", | ||
"xmlrpc": "^1.3.2" | ||
}, | ||
@@ -27,0 +27,0 @@ "scripts": { |
42
tests.js
@@ -1,28 +0,28 @@ | ||
const OS = require('./index.js') | ||
const UA = 'TemporaryUserAgent' | ||
const imdb = '0898266', show = 'The Big Bang Theory', s = '01', ep = '01' | ||
const OS = require('./index.js'); | ||
const UA = 'TemporaryUserAgent'; | ||
const imdb = '0898266', show = 'The Big Bang Theory', s = '01', ep = '01'; | ||
let os, test | ||
let os, test; | ||
test = 'http client' | ||
test = 'http client'; | ||
os = new OS({ | ||
useragent: UA, | ||
ssl: false | ||
}) | ||
console.time(test) | ||
}); | ||
console.time(test); | ||
os.api.ServerInfo().then(() => { | ||
console.timeEnd(test) | ||
console.timeEnd(test); | ||
test = 'https client' | ||
test = 'https client'; | ||
os = new OS({ | ||
useragent: UA, | ||
ssl: true | ||
}) | ||
console.time(test) | ||
return os.api.ServerInfo() | ||
}); | ||
console.time(test); | ||
return os.api.ServerInfo(); | ||
}).then(() => { | ||
console.timeEnd(test) | ||
console.timeEnd(test); | ||
test = 'search' | ||
console.time(test) | ||
test = 'search'; | ||
console.time(test); | ||
return os.search({ | ||
@@ -33,9 +33,9 @@ season: s, | ||
limit: 'all' | ||
}) | ||
}); | ||
}).then(() => { | ||
console.timeEnd(test) | ||
console.log('Passed test.') | ||
console.timeEnd(test); | ||
console.log('Passed test.'); | ||
}).catch((err) => { | ||
console.log('Test failed') | ||
console.log(err) | ||
}) | ||
console.log('Test failed'); | ||
console.log(err); | ||
}); |
52846
855
Updatedurijs@^1.19.1
Updatedxmlrpc@^1.3.2