Skip to content

Instantly share code, notes, and snippets.

@fredantell
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredantell/cb351cd3b37945b7b24b to your computer and use it in GitHub Desktop.
Save fredantell/cb351cd3b37945b7b24b to your computer and use it in GitHub Desktop.
Grab subtitles
var postURLs = [];
var movieLinks = $('dl dt b a');
var transcriptData = [];
var extractSubs = function(subs) {
var output = '';
var start;
var end;
var text = '';
var hour = 0;
var min = 0;
var sec = 0;
var frac = 0;
var pad = function(time) {
return time < 10 ? '0' + time : '' + time;
}
var formatTime = function(hour, min, sec, frac) {
return pad(hour) + ':' + pad(min) + ':' + pad(sec) + ',' + frac;
}
var convertDuration = function(dur) {
var h, m, s, f;
var time = parseFloat(dur, 10);
h = Math.floor(time / 60 / 60);
m = parseInt((time/60 - h), 10)
s = parseInt(time % 60, 10)
f = parseInt((time - Math.floor(time)) * 1000, 10);
return formatTime(h, m, s, f);
}
//loop through all but the last item which I will handle manually later
for (var i = 0; i < subs.length - 1; i++) {
start = subs[i].attributes['data-duration'].value;
end = subs[i + 1].attributes['data-duration'].value;
text = subs[i].innerHTML;
output += '' +
(i + 1) + '\n' +
convertDuration(start) + ' --> ' + convertDuration(end) + '\n' +
text + '\n\n';
}
//the above loop won't work for the last item since it has a subs[i +1] that will throw an error
//so I manually perform the last loop and then return the result of the entire string
//concatenation.
var results = (function lastLoop() {
var item = subs[subs.length - 1];
start = item.attributes['data-duration'].value;
end = parseFloat(start, 10) + 5;
text = item.innerHTML;
output += '' +
(i + 1) + '\n' +
convertDuration(start) + ' --> ' + convertDuration(end) + '\n' +
text + '\n\n';
return output;
})()
// console.log(results);
return results;
};
var processResponse = function(data, num) {
var el = document.createElement('div');
el.innerHTML = data.VideoTranscriptHtml;
var rawSubs = el.querySelectorAll('.transcript');
transcriptData[num] = extractSubs(rawSubs);
};
var ajaxCall = function(url, num) {
$.ajax({
"url": url,
type: 'POST',
success: function(res) {
console.log(res);
processResponse(res, num);}
})
};
var performAllAjaxCalls = function() {
//build the array of URLs to use for your POST request
for (var i = 0; i < movieLinks.length; i++) {
postURLs.push(movieLinks[i].id.replace('lnk-', '/CourseTranscript/'));
}
//perform all the POST requests for postURLs.
//Via processResponse, write the answers to the transcriptData array
for (var i = 0; i < postURLs.length; i++) {
ajaxCall(postURLs[i], i);
}
};
var logOutResults = function() {
for (var i = 0; i < transcriptData.length; i++) {console.log(transcriptData[i], '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n')}
};
var init = function() {
performAllAjaxCalls();
setTimetout(logOutResults, 5000);
};
init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment