Skip to content

Instantly share code, notes, and snippets.

@theonelucas
Created October 23, 2020 11:02
Show Gist options
  • Save theonelucas/a676e2eff71186d729a92c1151f9bb1a to your computer and use it in GitHub Desktop.
Save theonelucas/a676e2eff71186d729a92c1151f9bb1a to your computer and use it in GitHub Desktop.
(function (window) {
// Script definitions
var delay = 500, // Delay between each video
moduleTree = true, // If true, each module will have a separate directory, else, all modules will belong to the same directory
createCourseDirectory = false, // If true, will download the course in a separate directory
waitServerTime = true; // If true, will wait the necessary time defined by the server that bots are identified
// (One request each 10-30 seconds). If false, the server will block your account after
// the download of an average of 3-4 courses
// Get course structure
String.prototype.escapeRegExp = function () {
return this.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
};
String.prototype.replaceArray = function (find, replace) {
var replaceString = this;
for (var i = 0; i < find.length; i++) {
replaceString = replaceString.replace(new RegExp(find[i].escapeRegExp(), "g"), replace[i]);
}
return replaceString;
};
function hasClass(element, cls) {
return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1;
}
var notAllowed = ['<', '>', ':', '"', '/', '\\', '|', '?', '*'],
allowed = ['', '', ' -', '\'', '-', '-', '-', '', ''],
fileNameReplace = ['', '', '', '', '', '', '', '', '', '_'],
vids = [], // Array with all videos
counter = 0, // Videos counter
list = document.getElementsByTagName("section");
var courseName = document.getElementById("course-title").children[0].text;
var scriptName = courseName.replaceArray(notAllowed.concat(' '), fileNameReplace).toLowerCase();
courseName = courseName.replaceArray(notAllowed, allowed);
for (var i = 0; i < list.length; i++) {
if (list[i].className.match(/\bmodule\b/)) {
var li_list = list[i].getElementsByClassName('clips')[0].getElementsByClassName('title');
var module = list[i].getElementsByTagName("h2")[0].innerText.replaceArray(notAllowed, allowed);
if (li_list.length > 0) {
for (var y = 0; y < li_list.length; y++) {
counter++;
vids.push({
"id": counter,
"module": i + ' - ' + module,
"name": (counter + ' - ' + ((moduleTree === true) ? '' : (module + ' - ')) + li_list[y].innerText).replaceArray(notAllowed, allowed),
"link": null
});
}
}
}
}
function pauseVideo() {
if (delay >= 5000) {
window.setTimeout(function() {
document.getElementById("play-control").click();
}, 2000);
}
}
// Get video links
// if (waitServerTime === true) {
// delay = 31000;
// }
var estimatedTime = ((counter * delay) / 1000);
if (estimatedTime > 60) {
estimatedTime = (estimatedTime / 60).toFixed(2) + ' minutes';
} else {
estimatedTime = estimatedTime.toFixed(2) + ' seconds';
}
console.info('Starting to gather video links, estimated time left: ' + estimatedTime);
if (delay >= 5000) {
pauseVideo();
}
var index = 0,
length = vids.length;
var gather = window.setInterval(function () {
if (document.body.contains(document.getElementById('try-again-error'))) { // If an error occurred while loading the video, re-load it
document.getElementById('video-quality-high').click();
} else if ((index < length) && ((hasClass(document.getElementById('vjs_video_3'), 'vjs-waiting')) || ((index !== 0) && (vids[index-1]["link"] === document.getElementsByTagName("video")[0].getAttribute("src"))))) { // If the video is still loading, then wait fot ir
return false;
} else if (index < length) { // If we haven't finished yet, then process the current video
console.info("Getting video link " + (index + 1) + " of " + length + " of video \"" + vids[index]["name"] + "\"");
vids[index]["link"] = document.getElementsByTagName("video")[0].getAttribute("src");
document.getElementById('next-control').click();
index++;
// If the delay is high, pause current video to prevent the next video start before we get the link of the current video
if (delay >= 5000) {
pauseVideo();
}
} else { // If we have finished processing all videos, then validate the gathering of info, generate the script and download it
window.clearInterval(gather);
var status = {
"status": "success",
"errors": []
};
for (var vid in vids) {
if (vids[vid]["link"] === "") {
status["status"] = "failure";
status["errors"].push("Found empty links!");
}
for (var vid2 in vids) {
if ((vids[vid]["link"] === vids[vid2]["link"]) && (vid !== vid2)) {
status["status"] = "failure";
status["errors"].push("Found duplicate links! Please, try to increase the delay between each video, which is currently " + (delay / 1000).toFixed(2) + " second" + ((delay > 1000) ? "s" : ""));
}
}
}
console.log(vids);
if (status["status"] !== "success") {
console.error("Error while processing course!", status);
} else {
// Build and download the PowerShell Script
var data = '', // The PowerShell Script
lastModule = '', // Name of the last module
currentModule = ''; // Name of the current module
if (createCourseDirectory === true) {
data += 'mkdir ".\\' + courseName + '"\r\n';
data += 'cd ".\\' + courseName + '"\r\n';
}
for (vid in vids) {
if (moduleTree === true) {
currentModule = vids[vid]["module"];
if (lastModule !== currentModule) {
if (lastModule !== '') {
data += 'cd ..\r\n';
}
data += 'mkdir ".\\' + currentModule + '"\r\n';
data += 'cd ".\\' + currentModule + '"\r\n';
}
lastModule = currentModule;
}
data += 'Invoke-WebRequest "' + vids[vid]["link"] + '" -OutFile "' + vids[vid]["name"] + '.mp4"\r\n';
if ((waitServerTime === true) && (parseInt(vid)+1 < vids.length)) {
data += 'Start-Sleep -m 31000\r\n';
}
}
data += '[System.Windows.Forms.MessageBox]::Show("Course successfully downloaded!`n`nScript by: Theone Lucas (theone.luc@gmail.com)","Download finished",[System.Windows.Forms.MessageBoxButtons]::OKCancel,[System.Windows.Forms.MessageBoxIcon]::Asterisk)';
var
blob = new Blob([data], {type: 'text/plain'}),
a = document.createElement('a');
a.download = scriptName + '.ps1';
a.href = window.URL.createObjectURL(blob);
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':');
a.click();
console.info("Course successfully processed!", vids);
}
}
}, delay);
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment