-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (53 loc) · 1.95 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
const fs = require('fs');
const util = require('util');
const xml2js = require('xml2js');
const spawn = require('child-process-promise').spawn;
const sanitize = require('sanitize-filename');
const parseString = util.promisify(xml2js.parseString);
const escapeShell = str => sanitize(str).replace(/`/g, '\'');
async function downloadItem(filename, link) {
console.log(`[+] Starting download for ${filename}`);
const promise = spawn('curl', [`"${link}"`, '-o', filename], { shell: true });
const childProcess = promise.childProcess;
childProcess.stdout.on('data', function (data) {
console.log('stdout: ' + data.toString());
});
childProcess.stderr.on('data', function (data) {
process.stdout.write(data.toString());
});
await promise;
console.log(`[+] Done`);
}
function createFolderIdNeeded(folderName) {
if (!fs.existsSync(folderName)){
fs.mkdirSync(folderName);
}
}
const paddedNumber = number => ('0000' + number).slice(-4);
(async function runAsync() {
const args = process.argv;
if (args.length < 3) {
console.log('Usage: node index.js <rss_file.xml>');
process.exit(1);
}
const filename = process.argv[2];
const file = fs.readFileSync(filename);
const rss = await parseString(file);
const entries = rss.rss.channel[0].item;
const courseTitle = escapeShell(rss.rss.channel[0].title[0]);
const items = entries.map(item => ({
title: item.title[0],
url: item.enclosure[0]['$'].url,
}));
createFolderIdNeeded('./videos');
createFolderIdNeeded(`./videos/${courseTitle}`);
process.chdir(`./videos/${courseTitle}`);
console.log(`[+] Creating folder: ./videos/${courseTitle}`);
for (let index = 0; index < items.length; index++) {
const item = items[index];
const number = paddedNumber(index + 1);
const filename = `"${escapeShell(`${number}-${item.title}.mp4`)}"`
await downloadItem(filename, item.url);
}
console.log('[+] All course downloaded');
}()).catch(console.error);