-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (88 loc) · 2.46 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
const cheerio = require('cheerio');
const request = require('request');
const baseURL = 'https://scele.cs.ui.ac.id';
const cookies = request.jar();
const profile = {};
let courses = [];
const req = (url, form, method) => new Promise((resolve, reject) => {
const payload = {
method: method || 'GET',
url,
followAllRedirects: true,
jar: cookies,
form: form || undefined,
};
request(payload, (error, res, body) => {
if (!error && res.statusCode === 200) {
resolve(cheerio.load(body));
} else {
reject(error);
}
});
});
const updateProfile = ($) => {
const userText = $('.usertext').html();
profile.npm = userText.split('- ')[1].split(' ')[0];
profile.fullName = userText.split(' - ')[0];
profile.nickName = userText.split(`${profile.npm} `)[1];
};
const fetchContent = ($, contentElement) => {
const contents = [];
$(contentElement)
.find('.activity')
.each((_, element) => {
contents.push({
title: $(element)
.find('.instancename')
.html()
.split('<')[0],
link: $(element)
.find('a')
.attr('href'),
comment: $(element)
.find('.contentafterlink')
.text(),
type: $(element)
.attr('class')
.split(' ')[1],
});
});
const announcement = $(contentElement)
.find('.summary')
.text();
return { contents, announcement };
};
const fetchDetail = async (link) => {
const section = {};
const $ = await req(link);
const sectionSelector = '.course-content .content';
$(sectionSelector).each((_, element) => {
const title = $(element)
.find('.sectionname')
.text();
section[title] = fetchContent($, element);
});
return section;
};
const updateCourses = ($) => {
const selector = '#wrapper > header.navbar > nav > div > div > ul:nth-child(1) > li:nth-child(5) > ul > li > a';
courses = [];
$(selector).each((_, element) => {
courses.push({
link: $(element).attr('href'),
longTitle: $(element).attr('title'),
shortTitle: $(element).html(),
fetchDetail: () => fetchDetail($(element).attr('href')),
});
});
};
exports.login = async (username, password) => {
const $ = await req(`${baseURL}/login/`, { username, password }, 'POST');
if ($('.loginerrors').length > 0) {
throw new Error('Login failed');
}
updateProfile($);
updateCourses($);
};
exports.getProfile = () => profile;
exports.getCourses = () => courses;