-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexJavascriptForBeginner.js
56 lines (56 loc) · 1.72 KB
/
indexJavascriptForBeginner.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
const cheerio = require("cheerio");
const axios = require("axios");
const async = require("async");
const fs = require("fs");
// Make a folder "First"
// That is why I need use mkdirSync
fs.mkdirSync("download", { recursive: true }, err => {
if (err) throw err;
});
// Parse the HTML file and cherry pick data from it
// We need file name and file url in this case
axios
.get("https://coursehunter.net/course/javascript-dlya-nachinayushchih")
.then(response => {
const $ = cheerio.load(response.data);
const result = [];
$(".lessons-item").map(function(el) {
// Pick the URL
let u = $(this)
.children()
.filter(function(child) {
return $(this).attr("itemprop") === "contentUrl";
})
.attr("href");
// Pick the name
let n = $(this)
.children()
.filter(function(child) {
return $(this).attr("class") === "lessons-name";
})
.text();
result.push({ name: n, url: u });
});
return result;
})
.then(contents => {
// I want to include the index nmuber as part of file name for sorting purpose
// But package async doesn't map the index
// I have to use Object.keys and ask async to map the index
let contentsIndexes = Object.keys(contents);
async.map(contentsIndexes, contentIndex => {
axios
// The option responseType is required
.get(contents[contentIndex].url, { responseType: "stream" })
.then(videoFile =>
videoFile.data.pipe(
fs.createWriteStream(
//file name
`./download/${parseInt(contentIndex) + 1}.${
contents[contentIndex].name
}.mp4`
)
)
);
});
});