Skip to content

Instantly share code, notes, and snippets.

@linktohack
Created March 4, 2017 11:07
Show Gist options
  • Save linktohack/6a3d0081ea520b4e9875827d17aee1ff to your computer and use it in GitHub Desktop.
Save linktohack/6a3d0081ea520b4e9875827d17aee1ff to your computer and use it in GitHub Desktop.
Download sub etc
// @flow
var rp = require('request-promise');
var cheerio = require('cheerio');
var _ = require('lodash');
var fs = require('fs');
var Promise = require('bluebird');
var JSZip = require('JSZip');
var imdbID = 'tt1355644';
var language = 'Vietnamese';
downloadSub(imdbID, language).then(console.log.bind(console));
function load(uri) {
var options = {
uri: uri,
transform: function(body) {
return cheerio.load(body);
}
};
return rp(options);
}
function download(imdbID, language) {
return load(`http://www.imdb.com/title/${imdbID}`).then(function($) {
var header = $('h1[itemprop=name]').text();
var splited = header.match(/(.*?)\s+\(([0-9]+)\)/);
var name = splited[1];
var year = splited[2];
return Promise.all([name, year, load(`https://subscene.com/subtitles/title?q=${name}&l=`)]);
}).then(_.spread(function(name, year, $) {
var titles = $('div.title a');
var match = _.find(titles, function(el) {
return $(el).text() == `${name} (${year})`;
});
var link = 'https://subscene.com' + $(match).attr('href');
return Promise.all([name, year, load(link)]);
})).then(_.spread(function(name, year, $) {
var checked = $('a.imdb').attr('href') === `http://www.imdb.com/title/${imdbID}`
console.log(`checked: ${checked}`);
var subs = $('td.a1');
var subsForLang = _.filter(subs, function(el) {
return _.trim($(el).find('span.l.r').text()) === language;
});
var positiveSubs = _.filter(subsForLang, function(el) {
return $(el).find('span.l.r.positive-icon').length > 0;
});
console.log(`positive?: ${positiveSubs.length > 0}`);
var maybePositiveSub = positiveSubs.length > 0 ? positiveSubs : subsForLang;
var urls = _.map(maybePositiveSub, function(el) {
return 'https://subscene.com' + $(el).find('a').attr('href');
});
return load(_.first(urls));
})).then(function($) {
var link = 'https://subscene.com' + $('div.download a').attr('href');
return rp({ uri: link, encoding: null });
}).then(function(body) {
var zip = new JSZip();
return zip.loadAsync(body).then(function(contents) {
var name = _.get(_(contents.files).map().first(), 'name');
return zip.files[name].async('string');
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment