khcanvas.js
3.69 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
const selenium = require('selenium-webdriver')
const firefox = require('selenium-webdriver/firefox')
/*
import fs from 'fs/promises'
import { login, load, logout } from './khcanvas.js';
fs.readFile("asdffdsa.txt").then(it => {
const auth = it.toString().split('|')
login(auth[0], auth[1]).then(async driver => {
load(driver, new Date())
.then(it => {
console.log(it)
logout(driver)
})
}).catch(err => { console.log(err) })
})
*/
/*
open selenium session -> login -> get schedule -> logout -> close selenium session
id: string
pw: string
target_date: Date
returns unsubmitted assignments
ex)
[
{
'course_name': '오픈소스SW개발 00분반'
'due_date': '2022-05-15T14:59:59Z'
'assignment_name': '과제이름'
'points': 10.0
}
]
*/
async function get_schedule(id, pw, target_date) {
console.log("entered get_schedule_then")
const result = await using_selenium(async (driver) => {
console.log("entered using_selenium_then")
return await login(driver, id, pw)
.then(async () => {
console.log("entered login_then")
return await load(driver, target_date)
})
})
return await result
}
async function using_selenium(next) {
const option = new firefox.Options()
option.addArguments("-headless");
const driver = new selenium.Builder()
.forBrowser('firefox')
.setFirefoxOptions(option)
.build()
const result = next(driver)
return await result
//await next(driver).finally(() => {
// driver.quit()
//})
}
async function login(driver, id, pw) {
await driver.get("https://khcanvas.khu.ac.kr/")
const idInput = await driver.findElement(selenium.By.xpath('//*[@id="login_user_id"]'));
const pwInput = await driver.findElement(selenium.By.xpath('//*[@id="login_user_password"]'));
const login_button = await driver.findElement(selenium.By.xpath('//*[@id="form1"]/div/div[3]'));
await idInput.sendKeys(id);
await pwInput.sendKeys(pw);
await login_button.click();
return driver
}
async function load(driver, until) {
const start_date = until.toISOString()
await driver.get(`https://khcanvas.khu.ac.kr/api/v1/planner/items?start_date=${start_date}`);
await sleep(1000)
await driver.findElement(selenium.By.xpath('/html/body/div/div/nav/ul/li[2]')).click();
const data = await driver.findElement(selenium.By.xpath('/html/body/div/div/div/div[2]/div/div/div[2]/pre'));
const text = JSON.parse((await data.getText()).slice(9))
logout(driver)
return text.filter(it =>
((!it.submissions.submitted) && (!it.submissions.graded)) && it.plannable_type === "assignment").map(it => {
return {
"course_name": it.context_name,
"due_date": it.plannable.due_at,
"assignment_name": it.plannable.title,
"points": it.plannable.points_possible
}
})
}
async function logout(driver) {
// await driver.get("https://khcanvas.khu.ac.kr/")
// await driver.findElement(selenium.By.xpath('html/body/div[2]/header[2]/div[1]/ul/li[1]/button/div[1]')).click();
// await sleep(1000)
// const logout = await driver.findElement(selenium.By.xpath('html/body/div[3]/span/span/div/div/div/div/div/span/form/button'));
// logout.click()
// await sleep(1000)
driver.quit()
console.log("quit")
}
function sleep(ms) {
return new Promise((r) => setTimeout(r, ms));
}
module.exports.get_schedule = get_schedule
module.exports.using_selenium = using_selenium
module.exports.login = login
module.exports.load = load
module.exports.logout = logout