Fixes for google drive subtitles

This commit is contained in:
calzoneman 2015-07-25 10:31:21 -07:00
parent b0e6de389e
commit 174ad8d81e
1 changed files with 13 additions and 8 deletions

View File

@ -42,12 +42,13 @@ function formatTime(time) {
return list.map(padZeros).join(':') + ms.toFixed(3).substring(1);
}
function unescapeHtmlEntities(text) {
function fixText(text) {
return text.replace(/&/g, '&')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&quot;/g, '"')
.replace(/&#39;/g, "'");
.replace(/&#39;/g, "'")
.replace(/-->/g, '--&gt;');
}
exports.convert = function convertSubtitles(subtitles) {
@ -55,10 +56,15 @@ exports.convert = function convertSubtitles(subtitles) {
var lines = slice.call($('transcript text').map(function (index, elem) {
var start = parseFloat(elem.attribs.start);
var end = start + parseFloat(elem.attribs.dur);
var text = elem.children[0].data;
var text;
if (elem.children.length) {
text = elem.children[0].data;
} else {
text = '';
}
var line = formatTime(start) + ' --> ' + formatTime(end);
line += '\n' + unescapeHtmlEntities(text) + '\n';
line += '\n' + fixText(text) + '\n';
return line;
}));
@ -66,16 +72,15 @@ exports.convert = function convertSubtitles(subtitles) {
};
exports.attach = function setupRoutes(app) {
app.get('/gdvtt/:id/:lang/:name.vtt', handleGetSubtitles);
app.get('/gdvtt/:id/:lang/(:name)?.vtt', handleGetSubtitles);
};
function handleGetSubtitles(req, res) {
var id = req.params.id;
var lang = req.params.lang;
var name = req.params.name;
var name = req.params.name || '';
var vid = req.query.vid;
if (typeof vid !== 'string' || typeof id !== 'string' || typeof lang !== 'string' ||
typeof name !== 'string') {
if (typeof vid !== 'string' || typeof id !== 'string' || typeof lang !== 'string') {
return res.sendStatus(400);
}
var file = [id, lang, md5(name)].join('_') + '.vtt';