-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·299 lines (271 loc) · 10.7 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
'use strict';
const puppeteer = require('puppeteer');
const inquirer = require('inquirer');
const fs = require('fs');
const httpntlm = require('httpntlm');
require('dotenv').config();
const machine_type = process.platform;
const fileSeparator = () => {
return machine_type === 'win32' ? '\\' : '/';
};
const pupp_options = {
headless: true,
};
const userAuthData = {
username: process.env.GUC_SK_USERNAME,
password: process.env.GUC_SK_PASSWORD,
};
const HOST = process.env.HOST_URL;
const authenticateUser = () => {
return new Promise((resolve, reject) => {
httpntlm.get(
{
...userAuthData,
url: `${HOST}/apps/student/HomePageStn.aspx`,
rejectUnauthorized: false,
},
(err, res) => {
console.log(res.statusCode === 200 ? '[+] You are authorized\n============' : '[!] You are not authorized. Please review your login credentials.');
resolve(res.statusCode === 200);
}
);
});
};
const navigateTo = async (page, target_link) => {
await page.goto(target_link, {
waitUntil: 'networkidle2',
timeout: 500000,
});
};
const getSeasons = async (page) => {
return await page.evaluate(function () {
const seasons = [];
document.querySelectorAll('div[class="menu-header-title"]').forEach((el) => {
const title = el.innerHTML.trim();
seasons.push({
name: title.substring(title.indexOf('Title') + 6).trim(),
sid: parseInt(title.substring(title.indexOf(':') + 1, title.indexOf(',')).trim()),
courses: [],
});
});
seasons.forEach((_, index) => {
const seasonCourses = document.querySelectorAll(`table[id="ContentPlaceHolderright_ContentPlaceHoldercontent_r1_GridView1_${index}"]`)[0].children[0].children;
for (let i = 1; i < seasonCourses.length; i++) {
const courseName = seasonCourses[i].children[1].innerText.trim().replaceAll('|', '');
const is_active = (seasonCourses[i].children[2].innerText.trim() === 'Active');
if(!is_active){
continue;
}
seasons[index].courses.push({
name: courseName.substring(0, courseName.lastIndexOf('(')).trim().replace('(', '[').replace(')', ']'),
id: parseInt(courseName.substring(courseName.lastIndexOf('(') + 1, courseName.lastIndexOf(')')).trim()),
});
}
});
return seasons;
});
};
const resolveContentName = async (page) => {
await page.evaluate(() => {
document.querySelectorAll('a[download]').forEach((el) => {
const fileName = el.parentElement.parentElement.parentElement.children[0].children[0].innerHTML;
const fileExtension = el.href.split('.')[document.querySelectorAll('a[download]')[0].href.split('.').length - 1];
const fullName = `${fileName}.${fileExtension}`;
el.download = fullName;
});
});
};
const getContent = async (page, courses, seasonId) => {
const content = [];
const getWeeks = async (page) => {
return await page.evaluate(() => {
const weeks = [];
document.querySelectorAll('div.weeksdata').forEach((el) => {
const weekAnnouncement = el.children[1].children[0].innerText.trim();
const weekDescription = el.children[1].children[1].innerText.trim();
const tempWeekContent = el.children[1].children[2].children;
const weekContent = [];
for (let i = 1; i < tempWeekContent.length; i++) {
const orgName = tempWeekContent[i].children[0].children[0].innerText.trim();
const name = tempWeekContent[i].children[0].children[2].children[0].children[0].download.trim().replace('/', '').replace(':', '').toLowerCase();
const id = tempWeekContent[i].children[0].children[2].children[0].children[1].id;
const watchId = tempWeekContent[i].children[0].children[2].children[0].children[1].attributes['data-contentid'].value;
const url = orgName.includes('(VoD)')
? `https://playback.dacast.com/content/info?contentId=${id}&provider=dacast`
: tempWeekContent[i].children[0].children[2].querySelector('a#download').href.trim();
const watched = tempWeekContent[i].children[0].children[3].querySelector('i.fa-eye-slash').style.display == 'none';
if (!orgName.includes('https://'))
weekContent.push({
name,
url,
watched,
watchId,
});
}
weeks.push({
name: el.querySelector('h2.text-big').innerText,
announcement: weekAnnouncement,
description: weekDescription,
content: weekContent,
});
});
return weeks;
});
};
const getCourseAnnouncements = async (page) => {
return await page.evaluate(() => document.querySelector('div[id="ContentPlaceHolderright_ContentPlaceHoldercontent_desc"]').innerText.trim());
};
for (let i = 0; i < courses.length; i++) {
const courseUrl = `${HOST}/apps/student/CourseViewStn.aspx?id=${courses[i].id}&sid=${seasonId}`;
await navigateTo(page, courseUrl);
await resolveContentName(page);
content.push({
name: courses[i].name,
url: courseUrl,
weeks: await getWeeks(page),
announcements: await getCourseAnnouncements(page),
});
}
return content;
};
const getAnswers = async (questions, checkbox, message) => {
const answers = await inquirer.prompt([
{
type: checkbox ? 'checkbox' : 'list',
message: message,
name: 'userAnswers',
choices: questions,
validate(answer) {
if (answer.length < 1) {
return 'You must choose at least one course.';
}
return true;
},
loop: false,
},
]);
return checkbox ? answers.userAnswers.map((a) => questions.findIndex((q) => q.name === a)) : questions.findIndex((q) => (q.name || q) === answers.userAnswers);
};
const watchContent = async (page, watchId) => {
return await page.evaluate((watchId) => {
let el = document.querySelector(`input[data-contentid="${watchId}"]`);
if (el !== null) el.click();
el = document.querySelector('button[class="close closeclose"]');
if (el !== null) el.click();
}, watchId);
};
const downloadContent = async (page, season, courseName, weeks) => {
courseName = courseName.replace(':', '');
const download = (url, file_path, file_name) => {
if (!fs.existsSync(file_path))
fs.mkdirSync(file_path, { recursive: true }, (err) => {
console.log('There is an error in making directories, please report it. Error is: ', err.message);
});
console.log(`[-] Downloading file (${file_name})...`);
return new Promise((resolve, reject) => {
if (url.includes('https://playback.dacast.com')) {
const line = `${file_path}${fileSeparator()}${file_name}==${url}\n`;
fs.appendFile('VODs.txt', line, (err) => {
if (err) console.log('There is an error in file writing, please report it. Error is: ', err.message);
});
console.log(`[+] The VOD (${file_name}) will be downloaded later`);
console.log('------------');
resolve();
} else {
httpntlm.get(
{
...userAuthData,
url: url,
rejectUnauthorized: false,
binary: true,
},
(err, res) => {
// Request failed
if (err) {
console.log('There is an error in the request, please report it. Error is: ', err.message);
reject('Request Error');
}
// Request success, write to the file
fs.writeFile(`${file_path}${fileSeparator()}${file_name}`, res.body, (err) => {
if (err) {
console.log('There is an error in file writing, please report it. Error is: ', err.message);
reject('FileWriting Error');
}
console.log(`[+] Download completed. "${file_name}" is saved successfully in ${file_path}`);
console.log('------------');
resolve();
});
}
);
}
});
};
const rootPath = `.${fileSeparator()}cms_downloads${fileSeparator()}${season}${fileSeparator()}${courseName}`;
for (let i = 0; i < weeks.length; i++) {
const weekName = weeks[i].name.replace(':', '').toLowerCase();
const weekAnnouncement = weeks[i].announcement;
const weekDescription = weeks[i].description;
const weekContent = weeks[i].content;
for (let j = 0; j < weekContent.length; j++) {
const fileUrl = weekContent[j].url;
const fileName = weekContent[j].name.replace(':', '').toLowerCase();
await download(fileUrl, `${rootPath}${fileSeparator()}${weekName}`, fileName);
// Watch the downloaded content
await watchContent(page, weekContent[j].watchId);
}
}
};
(async () => {
console.log('==> Session Started <==');
const browser = await puppeteer.launch(pupp_options);
const page = await browser.newPage();
// 00- Authenticate User
console.log('[-] Authenticating...');
let user_auth = await authenticateUser();
if (!user_auth) {
await browser.close();
return;
}
await page.authenticate(userAuthData);
await navigateTo(page, `${HOST}/apps/student/ViewAllCourseStn`);
const seasons = await getSeasons(page);
const selectedSeason = seasons[await getAnswers(seasons, false, 'Please select a season', ['sid'])];
const downloadTypes = ['All content', 'Unwatched content', 'Select courses'];
const downloadType = await getAnswers(downloadTypes, false, 'Please select the download type');
let selectedCourses = [];
let coursesContent = [];
switch (downloadType) {
case 0:
selectedCourses = selectedSeason.courses;
coursesContent = await getContent(page, selectedCourses, selectedSeason.sid);
break;
case 1:
selectedCourses = selectedSeason.courses;
coursesContent = await getContent(page, selectedCourses, selectedSeason.sid);
coursesContent = coursesContent.map((course) => {
return {
...course,
weeks: course.weeks.map((week) => {
return {
...week,
content: week.content.filter((content) => content.watched === false),
};
}),
};
});
break;
case 2:
selectedCourses = (await getAnswers(selectedSeason.courses, true, 'Please select the courses you want', ['id'])).map((c) => selectedSeason.courses[c]);
coursesContent = await getContent(page, selectedCourses, selectedSeason.sid);
break;
default:
break;
}
for (let i = 0; i < coursesContent.length; i++) {
await navigateTo(page, coursesContent[i].url);
await downloadContent(page, selectedSeason.name, coursesContent[i].name, coursesContent[i].weeks);
}
// 6- End the session
await browser.close();
console.log('==> Session Ended <==');
})();