Last active
August 29, 2015 14:11
-
-
Save ivanhoe011/b683ba1f7aeaa3423015 to your computer and use it in GitHub Desktop.
Tutsplus lesson scrapper (save in Chrome's Dev tools as a snippet)
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
// run from the index page of the course | |
(function () { | |
var urls = [], | |
videos = []; | |
// get lesson urls | |
$('.lesson-index__lesson-link').each( function () { | |
urls.push( $(this).attr('href') ); | |
}); | |
// grab videos by injecting an iframe | |
function get_video(url, cnt) { | |
var $frm = $('<iframe/>') | |
.attr('src', url) | |
.on('load', function () { // when iframe loads | |
var title = $(this).contents().find('.breadcrumbs-bar__list li').last().text(), | |
src = $(this).contents().find('video').find('source').attr('src'); | |
$(this).remove(); // kill iframe | |
if ( (! title || ! src) && cnt++ < 4) { // re-try 3 times | |
console.log('#--> Retry '+ cnt); | |
setTimeout( function () { get_video(url, cnt); }, 250 * cnt); // retry after 0.5, 0.75 and 1sec | |
} else { // if we've got the video data (or failed 3 times) | |
videos.push("wget -O '"+ title.replace("'", '') +".mp4' "+ src); | |
if (urls.length > 0) { // if more lessons | |
get_video(urls.shift(), 1); // get next | |
} else { // we are done | |
$.each(videos, function (i,v) { console.log(v); }); | |
console.log("echo 'Done.'"); | |
} | |
} | |
}); | |
// inject iframe | |
$frm.appendTo( $('body') ); | |
} | |
get_video(urls.shift(), 1); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment