Showing
5 changed files
with
95 additions
and
58 deletions
1 | -import puppeteer from "puppeteer"; | 1 | +const puppeteer = require("puppeteer"); |
2 | //use puppeteer | 2 | //use puppeteer |
3 | 3 | ||
4 | function setTimeoutPromise(ms) { | 4 | function setTimeoutPromise(ms) { |
... | @@ -39,6 +39,12 @@ async function login(id, pw) { | ... | @@ -39,6 +39,12 @@ async function login(id, pw) { |
39 | ) { | 39 | ) { |
40 | return false; | 40 | return false; |
41 | } else { | 41 | } else { |
42 | + // await page.goto('https://khcanvas.khu.ac.kr'); | ||
43 | + let selector = | ||
44 | + "#visual > div > div.xn-main-login-container > div:nth-child(2) > div.xn-main-link-wrap.xn-main-lms-link-wrap > a"; | ||
45 | + await page.waitForSelector(selector); | ||
46 | + await Promise.all([page.click(selector), page.waitForNavigation()]); | ||
47 | + | ||
42 | return page; | 48 | return page; |
43 | } | 49 | } |
44 | } catch (err) { | 50 | } catch (err) { |
... | @@ -50,19 +56,14 @@ async function login(id, pw) { | ... | @@ -50,19 +56,14 @@ async function login(id, pw) { |
50 | //function that get your peed for your Ecampus calendar | 56 | //function that get your peed for your Ecampus calendar |
51 | async function getPeed(page) { | 57 | async function getPeed(page) { |
52 | console.log("getPeed"); | 58 | console.log("getPeed"); |
53 | - // await page.goto('https://khcanvas.khu.ac.kr'); | ||
54 | - let selector = | ||
55 | - "#visual > div > div.xn-main-login-container > div:nth-child(2) > div.xn-main-link-wrap.xn-main-lms-link-wrap > a"; | ||
56 | - await page.waitForSelector(selector); | ||
57 | - await page.click(selector); | ||
58 | 59 | ||
59 | selector = "#global_nav_calendar_link > div.menu-item-icon-container > svg"; | 60 | selector = "#global_nav_calendar_link > div.menu-item-icon-container > svg"; |
60 | await page.waitForSelector(selector); | 61 | await page.waitForSelector(selector); |
61 | - page.click(selector); | 62 | + await page.click(selector); |
62 | 63 | ||
63 | selector = "#calendar-feed > button"; | 64 | selector = "#calendar-feed > button"; |
64 | await page.waitForSelector(selector); | 65 | await page.waitForSelector(selector); |
65 | - page.click(selector); | 66 | + await page.click(selector); |
66 | 67 | ||
67 | // let data = {}; | 68 | // let data = {}; |
68 | // let temp = await page.$("#calendar-feed-box-lower > a"); | 69 | // let temp = await page.$("#calendar-feed-box-lower > a"); |
... | @@ -127,4 +128,6 @@ async function getOne(page, index) { | ... | @@ -127,4 +128,6 @@ async function getOne(page, index) { |
127 | //} | 128 | //} |
128 | // if your account is incorrect, value = [ '', '', 'Incorrect user ID or password.' ] | 129 | // if your account is incorrect, value = [ '', '', 'Incorrect user ID or password.' ] |
129 | 130 | ||
130 | -export { login, getPeed, getSubjects }; | 131 | +exports.login = login; |
132 | +exports.getPeed = getPeed; | ||
133 | +exports.getSubjects = getSubjects; | ... | ... |
1 | //The prototype | 1 | //The prototype |
2 | -// import puppeteer from "puppeteer"; | 2 | +// const puppeteer = require("puppeteer"); |
3 | // const crawler = async() => { | 3 | // const crawler = async() => { |
4 | // try{ | 4 | // try{ |
5 | // const browser = await puppeteer.launch({headless: false, args:['--window-size=1920,1080']}); | 5 | // const browser = await puppeteer.launch({headless: false, args:['--window-size=1920,1080']}); |
... | @@ -21,7 +21,6 @@ | ... | @@ -21,7 +21,6 @@ |
21 | // } | 21 | // } |
22 | // crawler(); | 22 | // crawler(); |
23 | 23 | ||
24 | - | ||
25 | //The test on nodejs (evaluate version for using input data) | 24 | //The test on nodejs (evaluate version for using input data) |
26 | const puppeteer = require("puppeteer"); | 25 | const puppeteer = require("puppeteer"); |
27 | //Example for not using user_input_login_information | 26 | //Example for not using user_input_login_information |
... | @@ -36,28 +35,35 @@ function setTimeoutPromise(ms) { | ... | @@ -36,28 +35,35 @@ function setTimeoutPromise(ms) { |
36 | } | 35 | } |
37 | 36 | ||
38 | async function loglogin(id, pw) { | 37 | async function loglogin(id, pw) { |
39 | - try{ | 38 | + try { |
40 | - const browser = await puppeteer.launch({headless: false, args:['--window-size=1920,1080']}); | 39 | + const browser = await puppeteer.launch({ |
40 | + headless: false, | ||
41 | + args: ["--window-size=1920,1080"], | ||
42 | + }); | ||
41 | const page = await browser.newPage(); | 43 | const page = await browser.newPage(); |
42 | await page.setViewport({ | 44 | await page.setViewport({ |
43 | - width:1920, | 45 | + width: 1920, |
44 | - height:1080 | 46 | + height: 1080, |
45 | - }) | 47 | + }); |
46 | - 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'); | 48 | + await page.goto( |
49 | + "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" | ||
50 | + ); | ||
47 | await page.type("#login_user_id", id); | 51 | await page.type("#login_user_id", id); |
48 | await page.type("#login_user_password", pw); | 52 | await page.type("#login_user_password", pw); |
49 | - await page.click('#form1 > div > div.login_btn > a > span'); | 53 | + await page.click("#form1 > div > div.login_btn > a > span"); |
50 | await page.waitForTimeout(500); | 54 | await page.waitForTimeout(500); |
51 | await setTimeoutPromise(1000); | 55 | await setTimeoutPromise(1000); |
52 | 56 | ||
53 | - 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'){ | 57 | + if ( |
58 | + page.url() === | ||
59 | + "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" | ||
60 | + ) { | ||
54 | return ["", "", "Incorrect user ID or password."]; | 61 | return ["", "", "Incorrect user ID or password."]; |
55 | - } | 62 | + } else { |
56 | - else { | ||
57 | return [id, pw, `I got data { id:"${id}" pw:"${pw}" }`]; | 63 | return [id, pw, `I got data { id:"${id}" pw:"${pw}" }`]; |
58 | } | 64 | } |
59 | - }catch(err){ | 65 | + } catch (err) { |
60 | - console.log(err) | 66 | + console.log(err); |
61 | } | 67 | } |
62 | } | 68 | } |
63 | async function login(id, pw) { | 69 | async function login(id, pw) { |
... | @@ -65,10 +71,15 @@ async function login(id, pw) { | ... | @@ -65,10 +71,15 @@ async function login(id, pw) { |
65 | try { | 71 | try { |
66 | const value = await loglogin(id, pw); | 72 | const value = await loglogin(id, pw); |
67 | // promise1.then | 73 | // promise1.then |
68 | - console.log("Id : "+ value[0] +", password : " + value[1] + ", message : " + value[2]); | 74 | + console.log( |
75 | + "Id : " + | ||
76 | + value[0] + | ||
77 | + ", password : " + | ||
78 | + value[1] + | ||
79 | + ", message : " + | ||
80 | + value[2] | ||
81 | + ); | ||
69 | return value[2]; | 82 | return value[2]; |
70 | - | ||
71 | - | ||
72 | } catch (e) { | 83 | } catch (e) { |
73 | console.error(e); | 84 | console.error(e); |
74 | } | 85 | } |
... | @@ -76,4 +87,3 @@ async function login(id, pw) { | ... | @@ -76,4 +87,3 @@ async function login(id, pw) { |
76 | 87 | ||
77 | login(id, pw); | 88 | login(id, pw); |
78 | //[id : , pw : , message : "Incorrect user ID or password."] | 89 | //[id : , pw : , message : "Incorrect user ID or password."] |
79 | - | ... | ... |
1 | -import puppeteer from "puppeteer"; | 1 | +const puppeteer = require("puppeteer"); |
2 | //use puppeteer | 2 | //use puppeteer |
3 | 3 | ||
4 | function setTimeoutPromise(ms) { | 4 | function setTimeoutPromise(ms) { |
... | @@ -8,37 +8,46 @@ function setTimeoutPromise(ms) { | ... | @@ -8,37 +8,46 @@ function setTimeoutPromise(ms) { |
8 | } | 8 | } |
9 | 9 | ||
10 | async function loglogin(id, pw) { | 10 | async function loglogin(id, pw) { |
11 | - try{ | 11 | + try { |
12 | //for visibility, headless: false | 12 | //for visibility, headless: false |
13 | - const browser = await puppeteer.launch({headless: false, args:['--window-size=1920,1080']}); | 13 | + const browser = await puppeteer.launch({ |
14 | + headless: false, | ||
15 | + args: ["--window-size=1920,1080"], | ||
16 | + }); | ||
14 | 17 | ||
15 | const page = await browser.newPage(); | 18 | const page = await browser.newPage(); |
16 | await page.setViewport({ | 19 | await page.setViewport({ |
17 | - width:1920, | 20 | + width: 1920, |
18 | - height:1080 | 21 | + height: 1080, |
19 | - }) | 22 | + }); |
20 | - 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'); | 23 | + await page.goto( |
24 | + "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" | ||
25 | + ); | ||
21 | await page.type("#login_user_id", id); | 26 | await page.type("#login_user_id", id); |
22 | await page.type("#login_user_password", pw); | 27 | await page.type("#login_user_password", pw); |
23 | 28 | ||
24 | //press the login button on E_Campus homepage | 29 | //press the login button on E_Campus homepage |
25 | - await page.click('#form1 > div > div.login_btn > a > span'); | 30 | + await page.click("#form1 > div > div.login_btn > a > span"); |
26 | await page.waitForTimeout(500); | 31 | await page.waitForTimeout(500); |
27 | await setTimeoutPromise(1000); | 32 | await setTimeoutPromise(1000); |
28 | 33 | ||
29 | //When login is failed, return [id, pw, message] -> ["", "", "Incorrect~"] | 34 | //When login is failed, return [id, pw, message] -> ["", "", "Incorrect~"] |
30 | - 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'){ | 35 | + if ( |
36 | + page.url() === | ||
37 | + "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" | ||
38 | + ) { | ||
31 | return ["", "", "Incorrect user ID or password."]; | 39 | return ["", "", "Incorrect user ID or password."]; |
32 | - } | 40 | + } else { |
33 | - else { | ||
34 | // return [id, pw, `I got data { id:"${id}" pw:"${pw}" }`]; | 41 | // return [id, pw, `I got data { id:"${id}" pw:"${pw}" }`]; |
35 | await peedGet(page); | 42 | await peedGet(page); |
36 | await page.waitForTimeout(3000); | 43 | await page.waitForTimeout(3000); |
37 | - await page.click('body > div:nth-child(9) > div.ui-dialog-titlebar.ui-widget-header.ui-corner-all.ui-helper-clearfix > button > span'); | 44 | + await page.click( |
45 | + "body > div:nth-child(9) > div.ui-dialog-titlebar.ui-widget-header.ui-corner-all.ui-helper-clearfix > button > span" | ||
46 | + ); | ||
38 | await subjectGet(page); | 47 | await subjectGet(page); |
39 | } | 48 | } |
40 | - }catch(err){ | 49 | + } catch (err) { |
41 | - console.log(err) | 50 | + console.log(err); |
42 | } | 51 | } |
43 | } | 52 | } |
44 | 53 | ||
... | @@ -46,13 +55,17 @@ async function loglogin(id, pw) { | ... | @@ -46,13 +55,17 @@ async function loglogin(id, pw) { |
46 | async function peedGet(page) { | 55 | async function peedGet(page) { |
47 | await page.waitForTimeout(5000); | 56 | await page.waitForTimeout(5000); |
48 | // await page.goto('https://khcanvas.khu.ac.kr'); | 57 | // await page.goto('https://khcanvas.khu.ac.kr'); |
49 | - await page.click('#visual > div > div.xn-main-login-container > div:nth-child(2) > div.xn-main-link-wrap.xn-main-lms-link-wrap > a'); | 58 | + await page.click( |
59 | + "#visual > div > div.xn-main-login-container > div:nth-child(2) > div.xn-main-link-wrap.xn-main-lms-link-wrap > a" | ||
60 | + ); | ||
50 | await page.waitForTimeout(1000); | 61 | await page.waitForTimeout(1000); |
51 | - await page.click('#global_nav_calendar_link > div.menu-item-icon-container > svg'); | 62 | + await page.click( |
63 | + "#global_nav_calendar_link > div.menu-item-icon-container > svg" | ||
64 | + ); | ||
52 | await page.waitForTimeout(10000); | 65 | await page.waitForTimeout(10000); |
53 | - await page.click('#calendar-feed > button'); | 66 | + await page.click("#calendar-feed > button"); |
54 | let data = {}; | 67 | let data = {}; |
55 | - let temp = await page.$('#calendar-feed-box-lower > a'); | 68 | + let temp = await page.$("#calendar-feed-box-lower > a"); |
56 | data.name = await page.evaluate((data) => { | 69 | data.name = await page.evaluate((data) => { |
57 | return data.textContent; | 70 | return data.textContent; |
58 | }, temp); | 71 | }, temp); |
... | @@ -64,13 +77,17 @@ async function peedGet(page) { | ... | @@ -64,13 +77,17 @@ async function peedGet(page) { |
64 | return data; | 77 | return data; |
65 | } | 78 | } |
66 | 79 | ||
67 | - | ||
68 | async function subjectGet(page) { | 80 | async function subjectGet(page) { |
69 | await page.waitForTimeout(5000); | 81 | await page.waitForTimeout(5000); |
70 | - await page.click('#global_nav_courses_link > div.menu-item-icon-container > svg') | 82 | + await page.click( |
83 | + "#global_nav_courses_link > div.menu-item-icon-container > svg" | ||
84 | + ); | ||
71 | await page.waitForTimeout(5000); | 85 | await page.waitForTimeout(5000); |
72 | - let data= []; | 86 | + let data = []; |
73 | - const number = await page.$$eval("#nav-tray-portal > span > span > div > div > div > div > div > ul:nth-child(3) > li", (data) => data.length); | 87 | + const number = await page.$$eval( |
88 | + "#nav-tray-portal > span > span > div > div > div > div > div > ul:nth-child(3) > li", | ||
89 | + (data) => data.length | ||
90 | + ); | ||
74 | for (let index = 0; index < number; index++) { | 91 | for (let index = 0; index < number; index++) { |
75 | data.push(await getOne(page, index + 1)); | 92 | data.push(await getOne(page, index + 1)); |
76 | } | 93 | } |
... | @@ -79,7 +96,11 @@ async function subjectGet(page) { | ... | @@ -79,7 +96,11 @@ async function subjectGet(page) { |
79 | 96 | ||
80 | async function getOne(page, index) { | 97 | async function getOne(page, index) { |
81 | let data = {}; | 98 | let data = {}; |
82 | - let temp = await page.$("#nav-tray-portal > span > span > div > div > div > div > div > ul:nth-child(3) > li:nth-child(" + index + ") > a"); | 99 | + let temp = await page.$( |
100 | + "#nav-tray-portal > span > span > div > div > div > div > div > ul:nth-child(3) > li:nth-child(" + | ||
101 | + index + | ||
102 | + ") > a" | ||
103 | + ); | ||
83 | data.name = await page.evaluate((data) => { | 104 | data.name = await page.evaluate((data) => { |
84 | return data.textContent; | 105 | return data.textContent; |
85 | }, temp); | 106 | }, temp); |
... | @@ -91,7 +112,6 @@ async function getOne(page, index) { | ... | @@ -91,7 +112,6 @@ async function getOne(page, index) { |
91 | return data; | 112 | return data; |
92 | } | 113 | } |
93 | 114 | ||
94 | - | ||
95 | // function that contact E_Campus | 115 | // function that contact E_Campus |
96 | // if your account is correct, value = Promise { | 116 | // if your account is correct, value = Promise { |
97 | // { name: '캘린더 피드를 보려면 클릭하세요.', | 117 | // { name: '캘린더 피드를 보려면 클릭하세요.', |
... | @@ -101,11 +121,11 @@ async function getOne(page, index) { | ... | @@ -101,11 +121,11 @@ async function getOne(page, index) { |
101 | // if your account is incorrect, value = [ '', '', 'Incorrect user ID or password.' ] | 121 | // if your account is incorrect, value = [ '', '', 'Incorrect user ID or password.' ] |
102 | async function login(id, pw) { | 122 | async function login(id, pw) { |
103 | await setTimeoutPromise(1000); | 123 | await setTimeoutPromise(1000); |
104 | -try { | 124 | + try { |
105 | // value has the list ["","","Incorrect~"] | 125 | // value has the list ["","","Incorrect~"] |
106 | const value = await loglogin(id, pw); | 126 | const value = await loglogin(id, pw); |
107 | console.log(value); | 127 | console.log(value); |
108 | -} catch (e) { | 128 | + } catch (e) { |
109 | console.error(e); | 129 | console.error(e); |
110 | } | 130 | } |
111 | } | 131 | } | ... | ... |
1 | -import express from "express"; | 1 | +const express = require("express"); |
2 | -import { getPeed, getSubjects, login } from "./libs/E_Campus.js"; | 2 | +const { getPeed, getSubjects, login } = require("./libs/E_Campus.js"); |
3 | -import fs from "fs"; | 3 | +const fs = require("fs"); |
4 | 4 | ||
5 | const app = express(); | 5 | const app = express(); |
6 | const router = express.Router(); | 6 | const router = express.Router(); |
7 | 7 | ||
8 | router.get("/", (req, res) => { | 8 | router.get("/", (req, res) => { |
9 | + console.log(req); | ||
9 | res.header("Access-Control-Allow-Origin", "http://localhost:3000"); | 10 | res.header("Access-Control-Allow-Origin", "http://localhost:3000"); |
10 | res.send({ test: "Hi" }); | 11 | res.send({ test: "Hi" }); |
11 | console.log("listened /"); | 12 | console.log("listened /"); |
... | @@ -30,3 +31,7 @@ fs.readFile("server/libs/login.pvdata", "utf8", async (err, data) => { | ... | @@ -30,3 +31,7 @@ fs.readFile("server/libs/login.pvdata", "utf8", async (err, data) => { |
30 | }); | 31 | }); |
31 | } else console.log("Incorrect user ID or password."); | 32 | } else console.log("Incorrect user ID or password."); |
32 | }); | 33 | }); |
34 | + | ||
35 | +// app.listen(3001, function () { | ||
36 | +// console.log("listening on 3001"); | ||
37 | +// }); | ... | ... |
-
Please register or login to post a comment