Skip to content

Instantly share code, notes, and snippets.

@valentingavela
Last active July 11, 2019 19:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valentingavela/83c8c260dc021f1c5077f03b758c3bbf to your computer and use it in GitHub Desktop.
Save valentingavela/83c8c260dc021f1c5077f03b758c3bbf to your computer and use it in GitHub Desktop.
pluralsigth video download
/*
DISCLAIMER: this snippet is just for educational purposes. Use at your own risk.
Instructions:
-Step 1: Get a Pluralsight account.
-Step 2: Start the first video of a course.
-Step 3: paste the following code in the console.
Your videos will be downloaded in your current downloads folder.
*/
var video = document.querySelector('video');
var counter = 0;
var currentVideo = '';
var lastVideo = '';
var videoName = '';
async function downloadVideo() {
console.log('downloading');
await fetch(currentVideo)
.then(res => res.blob())
.then(blob => downloadBlob(blob, `${counter}-${videoName}.mp4`));
}
async function downloadSubtitles() {
console.log('downloading Subtitles');
videoId = () => {
const splitted = currentVideo.split('/');
const index = splitted.findIndex(word => word === 'resolution') + 1;
return splitted[index];
}
const subtitleUri = `https://app.pluralsight.com/transcript/api/v1/caption/json/${videoId()}/en`;
encodedSubtitleBlob = await fetch(subtitleUri)
.then(subtitle => subtitle.json())
.then(subtitle => {
return subtitle.map((sub, i) => {
function time(ms) {
return new Date(ms).toISOString().slice(11, -1).replace('.', ',');
}
var nextIndex = i + 1 == subtitle.length ? subtitle.length - 1 : i + 1;
return (i + 1) + "\r\n" + time(sub.displayTimeOffset * 1000) + " --> " + time(subtitle[nextIndex].displayTimeOffset * 1000) + "\r\n" + sub.text;
}).join("\r\n\r\n")
})
.then(endodedSubtitle => {
var data = encode(endodedSubtitle);
return new Blob([data], {
type: 'text/plain'
});
})
downloadBlob(encodedSubtitleBlob, `${counter}-${videoName}.srt`);
}
function downloadBlob(blob, name) {
var url = window.URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = name;
document.body.appendChild(a)
a.click();
a.remove();
}
function encode(s) {
var out = [];
for (var i = 0; i < s.length; i++) {
out[i] = s.charCodeAt(i);
}
return new Uint8Array(out);
}
function sleep(milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}
function goToNextVideo() {
document.querySelector('#next-control').click();
}
async function run() {
console.log('run');
await sleep(3 * 1000);
video = document.querySelector('video');
currentVideo = video.src;
videoName = document.querySelector('.title-clip').innerText;
if (currentVideo !== lastVideo) {
video.pause();
await downloadSubtitles();
await sleep(50 * 1000);
await downloadVideo();
await sleep(100 * 1000);
goToNextVideo();
lastVideo = currentVideo;
counter++;
run();
} else {
console.log('finish');
}
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment