khcanvas.js
3.3 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
import * as selenium from 'selenium-webdriver';
import * as firefox from 'selenium-webdriver/firefox.js'
/*
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
}
]
*/
export async function get_schedule(id, pw, target_date) {
return using_selenium((driver) => {
return login(driver, id, pw)
.then(() => {
return load(driver, target_date)
.then((data) => {
//logout(driver)
console.log(data)
return data
})
})
})
}
export async function using_selenium(next) {
const option = new firefox.Options()
option.addArguments("-headless");
const driver = new selenium.Builder()
.forBrowser('firefox')
.setFirefoxOptions(option)
.build()
next(driver).finally(() => {
driver.quit()
})
}
export 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
}
export 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))
return text.filter(it =>
!it.submissions.submitted && it.plannable_type === "assignment").map(it => new Map([['course_name', it.context_name], ['due_date', it.plannable.due_at], ['assignment_name', it.plannable.title], ['points', it.plannable.points_possible]]))
}
export async function logout(driver) {
await driver.get("https://khcanvas.khu.ac.kr/")
const logoutPanel = 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()
}
function sleep(ms) {
return new Promise((r) => setTimeout(r, ms));
}