-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
74 lines (67 loc) · 3.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const fs = require('fs');
const { argv } = require('yargs');
const cookieSerializer = require('./utils/cookieSerializer');
const { axiosSetCookie, limiter } = require('./utils/throttler');
const makeDirectory = require('./utils/makeDirectory');
const sanitizeFilename = require('./utils/filenameSanitizer');
const download = require('./utils/downloader');
const JSONcookie = fs.readFileSync(argv.cookiefile, 'utf8');
const cookie = cookieSerializer(JSONcookie);
const resolution = argv.resolution || '720';
const axiosWrapper = axiosSetCookie(cookie); // create an axios wrapper with the provided cookie
const throttledAxios = limiter.wrap(axiosWrapper); // use the axios wrapper in the limiter to create throttled axios
const slug = argv.courseurl.split('/courses/')[1];
// read download.log and return download session
const readDownloadLog = () => {
try {
if (fs.existsSync(`${__dirname}/download.log`)) {
const downloadSession = fs.readFileSync(`${__dirname}/download.log`, 'utf8');
return JSON.parse(downloadSession);
}
return {};
} catch (err) {
console.error('Error reading \'download.log\' file\n' + err);
}
};
(async () => {
const downloadSession = readDownloadLog();
if (downloadSession[slug] === undefined) downloadSession[slug] = [];
const { data: course } = await axiosWrapper(`https://api.frontendmasters.com/v1/kabuki/courses/${slug}`);
course.title = sanitizeFilename(course.title);
makeDirectory(course.title);
const unit = {
index: 0,
};
const lesson = {};
for (let element of course.lessonElements) {
try {
if (typeof (element) === 'string') {
unit.name = sanitizeFilename(element);
unit.index++;
lesson.index = 0;
lesson.directory = `${course.title}/${unit.index}. ${unit.name}`;
makeDirectory(lesson.directory);
} else {
lesson.index++;
const lessonHash = course.lessonHashes[element];
lesson.title = sanitizeFilename(course.lessonData[lessonHash].title);
lesson.fileName = `${unit.index}.${lesson.index} ${lesson.title}`;
// if video file already exists and download.log contains the video hash, skip download
console.log('\n', '\x1b[32m', `✔ Currently downloading: ${lesson.fileName}.webm`);
const videoFileExists = fs.existsSync(`${__dirname}/${lesson.directory}/${lesson.fileName}.webm`);
if (downloadSession[slug].includes(lessonHash) && videoFileExists) {
console.log('\x1b[33m', 'Already downloaded, skipping.');
console.log('\x1b[0m', '');
continue;
}
const sourceURL = `https://api.frontendmasters.com/v1/kabuki/video/${lessonHash}/source?r=${resolution}&f=webm`;
const { data: videoSource } = await axiosWrapper(sourceURL);
await download(videoSource.url, `${lesson.directory}/${lesson.fileName}.webm`, throttledAxios);
downloadSession[slug].push(lessonHash);
fs.writeFileSync('download.log', JSON.stringify(downloadSession));
}
} catch (err) {
console.error(err);
}
}
})();