yifysubtitles
Advanced tools
Comparing version 2.0.1 to 2.0.2
41
index.js
@@ -8,5 +8,8 @@ const fs = require('fs'); | ||
const srt2vtt = require('srt-to-vtt'); | ||
const cheerio = require('cheerio'); | ||
const langsFormat = require('./langs'); | ||
const apiUri = 'http://api.yifysubtitles.com/subs'; | ||
const uri = 'http://www.yifysubtitles.com/movie-imdb'; | ||
const downloadUri = 'http://yifysubtitles.com'; | ||
@@ -19,18 +22,27 @@ const langK = Object.keys(langsFormat); | ||
const apiCall = imdbId => { | ||
return got(`${apiUri}/${imdbId}`, {json: true}) | ||
.then(res => ( | ||
(res.body.subtitles === 0) ? | ||
Promise.resolve({}) : | ||
res.body | ||
)); | ||
// Since yifysubtitle api is not working anymore we scrape the site instead | ||
const scrape = imdbId => { | ||
return got(`${uri}/${imdbId}`) | ||
.then(res => cheerio.load(res.body)) | ||
.then($ => { | ||
return $('tbody tr').map((i, el) => { | ||
const $el = $(el); | ||
return { | ||
rating: $el.find('.rating-cell').text(), | ||
language: $el.find('.flag-cell .sub-lang').text().toLowerCase(), | ||
url: $el.find('.download-cell a').attr('href').replace('subtitles/', 'subtitle/') + '.zip' | ||
}; | ||
}).get(); | ||
}) | ||
}; | ||
const langFilter = (subs, langs) => { | ||
return Object.keys(subs).reduce((acc, apiLang) => { | ||
if (langs.some(userLang => userLang === apiLang)) { | ||
acc[apiLang] = subs[apiLang].sort((a, b) => b.rating - a.rating)[0]; | ||
const data = langs.reduce((acc, l) => { | ||
const lang = subs.filter(s => s.language === l).sort((a, b) => b.rating - a.rating); | ||
if (lang.length) { | ||
acc[l] = lang[0]; | ||
} | ||
return acc; | ||
}, {}); | ||
return data | ||
}; | ||
@@ -66,6 +78,3 @@ | ||
const runConditional = (imdbId, opts, res) => { | ||
if (!res.success) { | ||
return Promise.reject(res.message); | ||
} | ||
return Promise.resolve(langFilter(res.subs[imdbId], opts.langs.map(formatLangLong))) | ||
return Promise.resolve(langFilter(res, opts.langs.map(formatLangLong))) | ||
.then(res => downloads(res, opts)); | ||
@@ -87,6 +96,6 @@ }; | ||
return apiCall(imdbId) | ||
.then(res => Object.keys(res).length ? runConditional(imdbId, opts, res) : []); | ||
return scrape(imdbId) | ||
.then(res => res.length ? runConditional(imdbId, opts, res) : []); | ||
}; | ||
module.exports = yifysubtitles; |
{ | ||
"name": "yifysubtitles", | ||
"version": "2.0.1", | ||
"description": "A simple wrapper to download subtitles from yifysubtitles api.", | ||
"version": "2.0.2", | ||
"description": "A simple wrapper to download subtitles from the yifysubtitles website.", | ||
"license": "MIT", | ||
@@ -23,8 +23,9 @@ "repository": { | ||
"dependencies": { | ||
"cheerio": "^0.22.0", | ||
"global": "^4.3.1", | ||
"got": "^6.6.3", | ||
"p-map": "^1.1.0", | ||
"srt-to-vtt": "^1.1.1", | ||
"streamz": "^1.6.2", | ||
"unzipper": "^0.7.2", | ||
"p-map": "^1.1.0" | ||
"unzipper": "^0.7.2" | ||
}, | ||
@@ -31,0 +32,0 @@ "devDependencies": { |
@@ -5,3 +5,3 @@ [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo) | ||
# yifysubtitles | ||
> Search, download and convert subtitles in [VTT format](https://developer.mozilla.org/en/docs/Web/API/Web_Video_Text_Tracks_Format) for [YTS movies](https://yts.ag/) | ||
> Download and convert subtitles in [VTT format](https://developer.mozilla.org/en/docs/Web/API/Web_Video_Text_Tracks_Format) for [YTS movies](https://yts.ag/) | ||
@@ -8,0 +8,0 @@ |
Sorry, the diff of this file is not supported yet
123647
167
7
+ Addedcheerio@^0.22.0
+ Addedboolbase@1.0.0(transitive)
+ Addedcheerio@0.22.0(transitive)
+ Addedcss-select@1.2.0(transitive)
+ Addedcss-what@2.1.3(transitive)
+ Addeddom-serializer@0.1.1(transitive)
+ Addeddomelementtype@1.3.1(transitive)
+ Addeddomhandler@2.4.2(transitive)
+ Addeddomutils@1.5.1(transitive)
+ Addedentities@1.1.2(transitive)
+ Addedhtmlparser2@3.10.1(transitive)
+ Addedlodash.assignin@4.2.0(transitive)
+ Addedlodash.bind@4.2.1(transitive)
+ Addedlodash.defaults@4.2.0(transitive)
+ Addedlodash.filter@4.6.0(transitive)
+ Addedlodash.flatten@4.4.0(transitive)
+ Addedlodash.foreach@4.5.0(transitive)
+ Addedlodash.map@4.6.0(transitive)
+ Addedlodash.merge@4.6.2(transitive)
+ Addedlodash.pick@4.4.0(transitive)
+ Addedlodash.reduce@4.6.0(transitive)
+ Addedlodash.reject@4.6.0(transitive)
+ Addedlodash.some@4.6.0(transitive)
+ Addednth-check@1.0.2(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedstring_decoder@1.3.0(transitive)