-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCourse.js
115 lines (101 loc) · 5.15 KB
/
Course.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const COURSE_TITLE = '[ng-bind-html="productController.title"]';
class Course {
constructor(nightmare, course_link) {
this.nightmare = nightmare;
this.course_link = course_link;
this.course_details = {};
this.current_video = 0;
}
setCourseDetails() {
return new Promise((resolve, reject) => {
this.nightmare.goto(this.course_link).wait(COURSE_TITLE).evaluate(() => {
const getChapterVideoInfo = (chapter_index) => {
const root_elm = document.querySelector(`#toc-item-${chapter_index}`);
var videos = [];
Array.from(root_elm.querySelectorAll("a")).forEach((a, index) => {
videos[index] = {
title: a.innerText.trim(),
page_link: a.href
}
});
return videos;
}
const getChapters = () => {
var chapters = [];
document.querySelectorAll('.cover-toc__section-name.cover-toc__section-name--has-icon.ng-scope').forEach((chapter_title, index) => {
chapters[index] = {
title: chapter_title.innerText.trim().replace("\n", " "),
videos: getChapterVideoInfo(index)
}
})
return chapters;
}
const setChapters = () => {
const root_elm = document.querySelector('[ng-repeat="(key, chapter) in productController.tableOfContents track by $index"]');
const chapters = getChapters()
return chapters;
}
document.querySelector(".badge.badge-video").innerHTML = "";
const root = '[ng-show="!showSpinner"] ';
const hs = document.querySelectorAll(root + "h6");
const uls = document.querySelectorAll(root + "ul");
var course_details = {
title: document.querySelectorAll('[ng-bind-html="productController.title"]')[0].innerText.replace("[", "").replace("]", "").replace("Video", "").replace("\n", "").trim(),
author: hs[0].innerText.replace("By ", "").trim(),
date: hs[1].innerText.trim(),
quick_description: document.querySelectorAll(root + "p")[0].innerHTML,
key_features: "<ul>" + uls[1].innerHTML + "</ul>",
what_you_will_learn: "<ul>" + uls[2].innerHTML + "</ul>",
about: document.querySelector('[ng-bind-html="productController.productSummary.about"]').innerHTML,
about_author: document.querySelector('[ng-repeat="(key, author) in productController.productSummary.authorList"]').innerHTML,
table_of_contents: document.querySelector('.cover-toc__level-wrapper').innerHTML,
chapters: setChapters(),
total_videos: 0
}
course_details.chapters.map(chapters => {
chapters.videos.map(videos => {
course_details.total_videos += 1;
})
})
return course_details
}).then(course_details => {
this.course_details = course_details
console.log(`\nScraping: ${
this.course_details.title
}\nTotal Chapters: ${
this.course_details.chapters.length
}\nTotal Videos: ${this.course_details.total_videos}`);
resolve(course_details);
});
})
}
async setVideos(chapter_index = 0, video_index = 0) {
return new Promise((resolve, reject) => {
this.nightmare.goto(this.course_details.chapters[chapter_index].videos[video_index].page_link).wait("video").evaluate(() => {
return document.querySelector("video").src;
}).then(download_url => {
this.course_details.chapters[chapter_index].videos[video_index].download_url = download_url;
this.current_video++;
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(`${Math.round(10*(100*(this.current_video/this.course_details.total_videos))/10)}% - Completed [${ this.course_details.chapters[chapter_index].videos[video_index].title}]`)
if (video_index == this.course_details.chapters[chapter_index].videos.length - 1) {
chapter_index += 1;
video_index = -1;
}
video_index += 1;
if (this.course_details.chapters[chapter_index]) {
this.setVideos(chapter_index, video_index).then(() => {
resolve(this.course_details.chapters);
})
} else {
resolve(this.course_details.chapter);
}
})
})
}
getCourseDetails() {
return this.course_details;
}
}
module.exports = Course;