신원형

initial login feature (using selenium)

Showing 1 changed file with 101 additions and 0 deletions
1 +import * as selenium from 'selenium-webdriver';
2 +import * as firefox from 'selenium-webdriver/firefox.js'
3 +
4 +/*
5 +
6 +import fs from 'fs/promises'
7 +import { login, load, logout } from './khcanvas.js';
8 +
9 +fs.readFile("asdffdsa.txt").then(it => {
10 + const auth = it.toString().split('|')
11 + login(auth[0], auth[1]).then(async driver => {
12 + load(driver, new Date())
13 + .then(it => {
14 + console.log(it)
15 + logout(driver)
16 + })
17 + }).catch(err => { console.log(err) })
18 +})
19 +
20 +*/
21 +
22 +/*
23 +open selenium session -> login -> get schedule -> logout -> close selenium session
24 +
25 +id: string
26 +pw: string
27 +target_date: Date
28 +
29 +returns unsubmitted assignments
30 +
31 +ex)
32 +[
33 + {
34 + 'course_name': '오픈소스SW개발 00분반'
35 + 'due_date': '2022-05-15T14:59:59Z'
36 + 'assignment_name': '과제이름'
37 + 'points': 10.0
38 + }
39 +]
40 +*/
41 +
42 +export async function get_schedule(id, pw, target_date) {
43 + login(id, pw).then(async driver => {
44 + load(driver, target_date)
45 + .then(it => {
46 + console.log(it)
47 + logout(driver)
48 + })
49 + })
50 +}
51 +
52 +export async function login(id, pw) {
53 + const option = new firefox.Options()
54 + option.setBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
55 + const driver = new selenium.Builder()
56 + .forBrowser('firefox')
57 + .setFirefoxOptions(option)
58 + .build()
59 +
60 + await driver.get("https://khcanvas.khu.ac.kr/")
61 +
62 + const idInput = await driver.findElement(selenium.By.xpath('//*[@id="login_user_id"]'));
63 + const pwInput = await driver.findElement(selenium.By.xpath('//*[@id="login_user_password"]'));
64 + const login_button = await driver.findElement(selenium.By.xpath('//*[@id="form1"]/div/div[3]'));
65 +
66 + await idInput.sendKeys(id);
67 + await pwInput.sendKeys(pw);
68 + await login_button.click();
69 +
70 + return driver
71 +}
72 +
73 +export async function load(driver, until) {
74 + const start_date = until.toISOString()
75 +
76 + await driver.get(`https://khcanvas.khu.ac.kr/api/v1/planner/items?start_date=${start_date}`);
77 + await sleep(1000)
78 + await driver.findElement(selenium.By.xpath('/html/body/div/div/nav/ul/li[2]')).click();
79 +
80 + const data = await driver.findElement(selenium.By.xpath('/html/body/div/div/div/div[2]/div/div/div[2]/pre'));
81 + const text = JSON.parse((await data.getText()).slice(9))
82 +
83 + return text.filter(it =>
84 + !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]]))
85 +}
86 +
87 +export async function logout(driver) {
88 + await driver.get("https://khcanvas.khu.ac.kr/")
89 +
90 + const logoutPanel = await driver.findElement(selenium.By.xpath('html/body/div[2]/header[2]/div[1]/ul/li[1]/button/div[1]')).click();
91 + await sleep(1000)
92 + const logout = await driver.findElement(selenium.By.xpath('html/body/div[3]/span/span/div/div/div/div/div/span/form/button'));
93 + logout.click()
94 + await sleep(1000)
95 +
96 + driver.quit()
97 +}
98 +
99 +function sleep(ms) {
100 + return new Promise((r) => setTimeout(r, ms));
101 +}
...\ No newline at end of file ...\ No newline at end of file