신원형

Merge branch 'login' into 'master'

Login (Merge)



See merge request !1
1 +{
2 + "env": {
3 + "browser": true,
4 + "es2021": true
5 + },
6 + "extends": "eslint:recommended",
7 + "parserOptions": {
8 + "ecmaVersion": "latest",
9 + "sourceType": "module"
10 + },
11 + "rules": {
12 + }
13 +}
...@@ -5,6 +5,23 @@ ...@@ -5,6 +5,23 @@
5 "version": "0.2.0", 5 "version": "0.2.0",
6 "configurations": [ 6 "configurations": [
7 { 7 {
8 + "type": "node",
9 + "request": "launch",
10 + "name": "Mocha All",
11 + "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
12 + "args": [
13 + "--timeout",
14 + "999999",
15 + "--colors",
16 + "${workspaceFolder}/test"
17 + ],
18 + "console": "integratedTerminal",
19 + "internalConsoleOptions": "neverOpen",
20 + "skipFiles": [
21 + "<node_internals>/**/*.js"
22 + ]
23 + },
24 + {
8 "type": "pwa-node", 25 "type": "pwa-node",
9 "request": "launch", 26 "request": "launch",
10 "name": "Launch Program", 27 "name": "Launch Program",
......
...@@ -40,33 +40,45 @@ ex) ...@@ -40,33 +40,45 @@ ex)
40 */ 40 */
41 41
42 export async function get_schedule(id, pw, target_date) { 42 export async function get_schedule(id, pw, target_date) {
43 - login(id, pw).then(async driver => { 43 + return await using_selenium( async (driver) => {
44 - load(driver, target_date) 44 + return await login(driver, id, pw)
45 - .then(it => { 45 + .then(async () => {
46 - console.log(it) 46 + return await load(driver, target_date)
47 - logout(driver) 47 + .then((data) => {
48 - }) 48 + //logout(driver)
49 + console.log(data)
50 + return data
51 + })
52 + })
49 }) 53 })
50 } 54 }
51 55
52 -export async function login(id, pw) { 56 +export async function using_selenium(next) {
53 const option = new firefox.Options() 57 const option = new firefox.Options()
54 - option.setBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe') 58 + option.addArguments("-headless");
59 +
55 const driver = new selenium.Builder() 60 const driver = new selenium.Builder()
56 .forBrowser('firefox') 61 .forBrowser('firefox')
57 .setFirefoxOptions(option) 62 .setFirefoxOptions(option)
58 .build() 63 .build()
59 64
65 + next(driver).finally(() => {
66 + driver.quit()
67 + })
68 +}
69 +
70 +export async function login(driver, id, pw) {
71 +
60 await driver.get("https://khcanvas.khu.ac.kr/") 72 await driver.get("https://khcanvas.khu.ac.kr/")
61 73
62 const idInput = await driver.findElement(selenium.By.xpath('//*[@id="login_user_id"]')); 74 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"]')); 75 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]')); 76 const login_button = await driver.findElement(selenium.By.xpath('//*[@id="form1"]/div/div[3]'));
65 - 77 +
66 await idInput.sendKeys(id); 78 await idInput.sendKeys(id);
67 await pwInput.sendKeys(pw); 79 await pwInput.sendKeys(pw);
68 await login_button.click(); 80 await login_button.click();
69 - 81 +
70 return driver 82 return driver
71 } 83 }
72 84
...@@ -76,7 +88,7 @@ export async function load(driver, until) { ...@@ -76,7 +88,7 @@ export async function load(driver, until) {
76 await driver.get(`https://khcanvas.khu.ac.kr/api/v1/planner/items?start_date=${start_date}`); 88 await driver.get(`https://khcanvas.khu.ac.kr/api/v1/planner/items?start_date=${start_date}`);
77 await sleep(1000) 89 await sleep(1000)
78 await driver.findElement(selenium.By.xpath('/html/body/div/div/nav/ul/li[2]')).click(); 90 await driver.findElement(selenium.By.xpath('/html/body/div/div/nav/ul/li[2]')).click();
79 - 91 +
80 const data = await driver.findElement(selenium.By.xpath('/html/body/div/div/div/div[2]/div/div/div[2]/pre')); 92 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)) 93 const text = JSON.parse((await data.getText()).slice(9))
82 94
...@@ -87,7 +99,7 @@ export async function load(driver, until) { ...@@ -87,7 +99,7 @@ export async function load(driver, until) {
87 export async function logout(driver) { 99 export async function logout(driver) {
88 await driver.get("https://khcanvas.khu.ac.kr/") 100 await driver.get("https://khcanvas.khu.ac.kr/")
89 101
90 - const logoutPanel = await driver.findElement(selenium.By.xpath('html/body/div[2]/header[2]/div[1]/ul/li[1]/button/div[1]')).click(); 102 + 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) 103 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')); 104 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() 105 logout.click()
......
1 import fs from 'fs/promises' 1 import fs from 'fs/promises'
2 import { login, load, logout } from './khcanvas.js'; 2 import { login, load, logout } from './khcanvas.js';
3 3
4 -fs.readFile("asdffdsa.txt").then(it => {
5 - const auth = it.toString().split('|')
6 - login(auth[0], auth[1]).then(async driver => {
7 - load(driver, new Date())
8 - .then(it => {
9 - console.log(it)
10 - logout(driver)
11 - })
12 - }).catch(err => { console.log(err) })
13 -})
...\ No newline at end of file ...\ No newline at end of file
......
This diff is collapsed. Click to expand it.
...@@ -14,12 +14,12 @@ ...@@ -14,12 +14,12 @@
14 "license": "", 14 "license": "",
15 "dependencies": { 15 "dependencies": {
16 "@types/selenium-webdriver": "^4.1.0", 16 "@types/selenium-webdriver": "^4.1.0",
17 - "eslint": "^8.15.0",
18 "mocha": "^10.0.0", 17 "mocha": "^10.0.0",
19 "selenium-webdriver": "^4.1.2" 18 "selenium-webdriver": "^4.1.2"
20 }, 19 },
21 "type": "module", 20 "type": "module",
22 "devDependencies": { 21 "devDependencies": {
23 - "@types/node": "^17.0.35" 22 + "@types/node": "^17.0.35",
23 + "eslint": "^8.16.0"
24 } 24 }
25 } 25 }
......
1 +//@ts-check
2 +//https://github.com/gatoona/AWS-Selenium
3 +import * as canvas from '../khcanvas.js'
4 +import * as rd from 'readline'
5 +import * as mocha from 'mocha'
6 +import process from 'node:process';
7 +import util from 'util'
8 +import { rejects } from 'assert';
9 +
10 +
11 +mocha.describe('khcanvas', () => {
12 + mocha.it('opening selenium', async () => {
13 + await canvas.using_selenium(async (driver) => {
14 + await driver.get("http://khuhub.khu.ac.kr");
15 + await driver.getTitle().then( (title) => {
16 + console.log(title);
17 + });
18 + });
19 + })
20 +});
21 +
22 +
23 +mocha.describe('khcanvas', () => {
24 + mocha.it('get schedule', async () => {
25 + const rl = rd.createInterface({
26 + input: process.stdin,
27 + output: process.stdout
28 + })
29 +
30 + const question = util.promisify(rl.question).bind(rl);
31 +
32 + const a = await question('a');
33 + const b = await question('b');
34 +
35 + return await canvas.get_schedule(a, b, new Date())
36 + .then(it => console.log(it))
37 + .catch(it => {
38 + console.log(it)
39 + rejects(it)
40 + })
41 + })
42 +});
...\ No newline at end of file ...\ No newline at end of file
......