E_Campus.js
5.21 KB
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
const puppeteer = require("puppeteer");
//use puppeteer
Info = {
id : "null",
pw : "null",
peed : "null",
subjects : []
}
function setTimeoutPromise(ms) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(), ms);
});
}
async function login(id, pw) {
try {
//for visibility, headless: false
const browser = await puppeteer.launch({
headless: false,
args: [
"--window-size=1920x1080",
"--start-maximized",
"--no-sandbox",
"--disable-dev-shm-usage",
"--lang=ko",
],
});
const page = await browser.newPage();
await page.setViewport({
width: 1920,
height: 1080,
});
await page.goto(
"https://e-campus.khu.ac.kr/xn-sso/login.php?auto_login=&sso_only=&cvs_lgn=&return_url=https%3A%2F%2Fe-campus.khu.ac.kr%2Fxn-sso%2Fgw-cb.php%3Ffrom%3D%26login_type%3Dstandalone%26return_url%3Dhttps%253A%252F%252Fe-campus.khu.ac.kr%252Flogin%252Fcallback"
);
await page.type("#login_user_id", id);
await page.type("#login_user_password", pw);
//press the login button on E_Campus homepage
await Promise.all([
page.click("#form1 > div > div.login_btn > a > span"),
page.waitForNavigation(),
]);
//When login is failed, return {err: "Incorrect~"}
if (
page.url() ===
"https://e-campus.khu.ac.kr/xn-sso/gw-cb.php?from=&login_type=standalone&return_url=https%3A%2F%2Fe-campus.khu.ac.kr%2Flogin%2Fcallback"
) {
return false;
} else {
// 내 강의실
// let selector =
// "#visual > div > div.xn-main-login-container > div:nth-child(2) > div.xn-main-link-wrap.xn-main-lms-link-wrap > a";
// await page.waitForSelector(selector);
// await Promise.all([page.click(selector), page.waitForNavigation()]);
const promise1 = Promise.resolve();
promise1.then(value => {
Info.id = id;
Info.pw = pw;
})
await page.goto("https://khcanvas.khu.ac.kr/");
return page;
}
} catch (err) {
console.log(err);
}
}
//function that get your peed for your Ecampus calendar
async function getPeed(page) {
selector = "#global_nav_calendar_link > div.menu-item-icon-container > svg";
await page.waitForSelector(selector, { timeout: 1000 });
await Promise.all([page.click(selector), page.waitForNavigation()]);
selector = "#calendar-feed > button";
await page.waitForSelector(selector);
await page.click(selector);
let data = {};
let temp = await page.$("#calendar-feed-box-lower > a");
data.name = await page.evaluate((data) => {
return data.textContent;
}, temp);
data.link = await page.evaluate((data) => {
return data.href;
}, temp);
// console.log(Promise.resolve(data));
const promise2 = Promise.resolve(data.link);
promise2.then(value => {
Info.peed = value;
})
return Promise.resolve(data);
// let temp = await page.$("#calendar-feed-box-lower > a");
// const feed = await page.evaluate((data) => data.href, temp);
// selector =
// "body > div:nth-child(9) > div.ui-dialog-titlebar.ui-widget-header.ui-corner-all.ui-helper-clearfix > button > span";
// await page.waitForSelector(selector, { timeout: 1000 });
// await page.click(selector);
// return feed;
}
async function getSubjects(page) {
await page.click(
"#global_nav_courses_link > div.menu-item-icon-container > svg"
);
let data = [];
let selector =
"#nav-tray-portal > span > span > div > div > div > div > div > ul:nth-child(3) > li";
await page.waitForSelector(selector);
const number = await page.$$eval(selector, (data) => data.length);
for (let index = 0; index < number; index++) {
data.push(await getOne(page, index + 1));
}
// const promise1 = Promise.resolve(data);
// promise1.then(value => {
// Info.data = value;
// })
return data;
}
async function getOne(page, index) {
let data = {};
let temp = await page.$(
"#nav-tray-portal > span > span > div > div > div > div > div > ul:nth-child(3) > li:nth-child(" +
index +
") > a"
);
data.name = await page.evaluate((data) => {
return data.textContent;
}, temp);
data.link = await page.evaluate((data) => {
return data.href;
}, temp);
// data.subjectName = await page.$eval("#nav-tray-portal > span > span > div > div > div > div > div > ul:nth-child(3) > li:nth-child(" + index + ") > a", (data) => data.textContent);
// data.subjectLink = await page.$eval("#nav-tray-portal > span > span > div > div > div > div > div > ul:nth-child(3) > li:nth-child(" + index + ") > a", (data) => data.href);
return Promise.resolve(data);
}
// function that contact E_Campus
// if your account is correct, value = Promise {
// { name: '캘린더 피드를 보려면 클릭하세요.',
// link: 'https://khcanvas.khu.ac.kr/feeds/calendars/user_~~~.ics'
// }
//}
// if your account is incorrect, value = [ '', '', 'Incorrect user ID or password.' ]
// exports.login = login;
// exports.getPeed = getPeed;
// exports.getSubjects = getSubjects;
//console test
async function runrun() {
page = await login("lorem", "lorem");
await getPeed(page);
// await getPeed(page).then((value => {
// console.log(value.link);
// }));
// await getSubjects(page);
console.log(Info);
}
runrun();