Last active
December 12, 2015 02:09
-
-
Save mauricesvay/4696817 to your computer and use it in GitHub Desktop.
Automatically unzip compressed subtitles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var exec = require('child_process').exec; | |
var http = require("http"); | |
var fs = require('fs'); | |
var APIKEY = 'c8c4da831d5c'; | |
function showScrape(filename, callback) { | |
var url = "http://api.betaseries.com/shows/scraper.json?key=" + APIKEY + "&file="; | |
url = url + encodeURIComponent(filename); | |
http.get(url, function(res) { | |
var data = ''; | |
res.on('data', function (chunk) { | |
data += chunk; | |
}); | |
res.on('end', function(){ | |
callback(JSON.parse(data).root); | |
}); | |
}).on('error', function(e) { | |
console.log("Got error: " + e.message); | |
}); | |
} | |
function subtitlesShow(params) { | |
var url = "http://api.betaseries.com/subtitles/show/{{SLUG}}.json?key=" + APIKEY + "&language=VO&season={{SEASON}}&episode={{EPISODE}}"; | |
if (typeof params.episode.url === 'undefined') { | |
console.log('Cannot find subtitles'); | |
return; | |
} | |
url = url.replace('{{SLUG}}', params.episode.url); | |
url = url.replace('{{SEASON}}', params.episode.season); | |
url = url.replace('{{EPISODE}}', params.episode.episode); | |
http.get(url, function(res) { | |
var data = ''; | |
res.on('data', function (chunk) { | |
data += chunk; | |
}); | |
res.on('end', function(){ | |
data = JSON.parse(data); | |
if (data && data.root && data.root.subtitles && data.root.subtitles[0]) { | |
subtitleDownload(data.root.subtitles[0].file, data.root.subtitles[0].url); | |
} else { | |
console.log("Cannot find subtitles"); | |
} | |
}); | |
}).on('error', function(e) { | |
console.log("Got error: " + e.message); | |
}); | |
} | |
function subtitleDownload(filename, url) { | |
console.log(url, '->', filename); | |
if (url.match(/^https/)) { | |
//Force HTTP | |
url = url.replace(/^https/,'http'); | |
} | |
var file = fs.createWriteStream(filename); | |
var request = http.get(url, function(response) { | |
response.pipe(file); | |
response.on('end', function(){ | |
if (filename.match('.zip')) { | |
exec( | |
'unzip "' + filename + '"', | |
function (error, stdout, stderr) { | |
if (error === null) { | |
fs.unlink(filename); | |
} | |
} | |
); | |
} | |
}); | |
}); | |
} | |
if (process.argv.length === 3) { | |
showScrape(process.argv[2], function(episode){ | |
subtitlesShow({ | |
episode: episode | |
}); | |
}); | |
} else { | |
console.log("Usage:"); | |
console.log([ | |
process.argv[0], | |
process.argv[1], | |
"<filename>" | |
].join(" ")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment