Showing
3 changed files
with
72 additions
and
28 deletions
1 | const puppeteer = require("puppeteer"); | 1 | const puppeteer = require("puppeteer"); |
2 | //use puppeteer | 2 | //use puppeteer |
3 | +Info = { | ||
4 | + id : "null", | ||
5 | + pw : "null", | ||
6 | + peed : "null", | ||
7 | + subjects : [] | ||
8 | +} | ||
3 | 9 | ||
4 | function setTimeoutPromise(ms) { | 10 | function setTimeoutPromise(ms) { |
5 | return new Promise((resolve, reject) => { | 11 | return new Promise((resolve, reject) => { |
... | @@ -10,8 +16,8 @@ function setTimeoutPromise(ms) { | ... | @@ -10,8 +16,8 @@ function setTimeoutPromise(ms) { |
10 | async function login(id, pw) { | 16 | async function login(id, pw) { |
11 | try { | 17 | try { |
12 | //for visibility, headless: false | 18 | //for visibility, headless: false |
13 | - const browser = await puppeteer.launch({ | 19 | + browser = await puppeteer.launch({ |
14 | - headless: true, | 20 | + headless: false, |
15 | args: [ | 21 | args: [ |
16 | "--window-size=1920x1080", | 22 | "--window-size=1920x1080", |
17 | "--start-maximized", | 23 | "--start-maximized", |
... | @@ -51,6 +57,11 @@ async function login(id, pw) { | ... | @@ -51,6 +57,11 @@ async function login(id, pw) { |
51 | // "#visual > div > div.xn-main-login-container > div:nth-child(2) > div.xn-main-link-wrap.xn-main-lms-link-wrap > a"; | 57 | // "#visual > div > div.xn-main-login-container > div:nth-child(2) > div.xn-main-link-wrap.xn-main-lms-link-wrap > a"; |
52 | // await page.waitForSelector(selector); | 58 | // await page.waitForSelector(selector); |
53 | // await Promise.all([page.click(selector), page.waitForNavigation()]); | 59 | // await Promise.all([page.click(selector), page.waitForNavigation()]); |
60 | + const promise1 = Promise.resolve(); | ||
61 | + promise1.then(value => { | ||
62 | + Info.id = id; | ||
63 | + Info.pw = pw; | ||
64 | + }) | ||
54 | await page.goto("https://khcanvas.khu.ac.kr/"); | 65 | await page.goto("https://khcanvas.khu.ac.kr/"); |
55 | return page; | 66 | return page; |
56 | } | 67 | } |
... | @@ -69,27 +80,36 @@ async function getPeed(page) { | ... | @@ -69,27 +80,36 @@ async function getPeed(page) { |
69 | await page.waitForSelector(selector); | 80 | await page.waitForSelector(selector); |
70 | await page.click(selector); | 81 | await page.click(selector); |
71 | 82 | ||
72 | - // let data = {}; | 83 | + let data = {}; |
73 | - // let temp = await page.$("#calendar-feed-box-lower > a"); | ||
74 | - // data.name = await page.evaluate((data) => { | ||
75 | - // return data.textContent; | ||
76 | - // }, temp); | ||
77 | - // data.link = await page.evaluate((data) => { | ||
78 | - // return data.href; | ||
79 | - // }, temp); | ||
80 | - | ||
81 | let temp = await page.$("#calendar-feed-box-lower > a"); | 84 | let temp = await page.$("#calendar-feed-box-lower > a"); |
82 | - const feed = await page.evaluate((data) => data.href, temp); | 85 | + data.name = await page.evaluate((data) => { |
86 | + return data.textContent; | ||
87 | + }, temp); | ||
88 | + data.link = await page.evaluate((data) => { | ||
89 | + return data.href; | ||
90 | + }, temp); | ||
91 | + // console.log(Promise.resolve(data)); | ||
92 | + const promise2 = Promise.resolve(data.link); | ||
93 | + promise2.then(value => { | ||
94 | + Info.peed = value; | ||
95 | + }) | ||
96 | + return Promise.resolve(data); | ||
83 | 97 | ||
84 | - selector = | 98 | + // let temp = await page.$("#calendar-feed-box-lower > a"); |
85 | - "body > div:nth-child(9) > div.ui-dialog-titlebar.ui-widget-header.ui-corner-all.ui-helper-clearfix > button > span"; | 99 | + // const feed = await page.evaluate((data) => data.href, temp); |
86 | - await page.waitForSelector(selector, { timeout: 1000 }); | ||
87 | - await page.click(selector); | ||
88 | 100 | ||
89 | - return feed; | 101 | + // selector = |
102 | + // "body > div:nth-child(9) > div.ui-dialog-titlebar.ui-widget-header.ui-corner-all.ui-helper-clearfix > button > span"; | ||
103 | + // await page.waitForSelector(selector, { timeout: 1000 }); | ||
104 | + // await page.click(selector); | ||
105 | + | ||
106 | + // return feed; | ||
90 | } | 107 | } |
91 | 108 | ||
92 | async function getSubjects(page) { | 109 | async function getSubjects(page) { |
110 | + //close the peed window to get subjects | ||
111 | + await page.click('body > div:nth-child(9) > div.ui-dialog-titlebar.ui-widget-header.ui-corner-all.ui-helper-clearfix > button > span'); | ||
112 | + await page.waitForTimeout(500); | ||
93 | await page.click( | 113 | await page.click( |
94 | "#global_nav_courses_link > div.menu-item-icon-container > svg" | 114 | "#global_nav_courses_link > div.menu-item-icon-container > svg" |
95 | ); | 115 | ); |
... | @@ -102,6 +122,13 @@ async function getSubjects(page) { | ... | @@ -102,6 +122,13 @@ async function getSubjects(page) { |
102 | for (let index = 0; index < number; index++) { | 122 | for (let index = 0; index < number; index++) { |
103 | data.push(await getOne(page, index + 1)); | 123 | data.push(await getOne(page, index + 1)); |
104 | } | 124 | } |
125 | + const promise3 = Promise.resolve(data); | ||
126 | + promise3.then(value => { | ||
127 | + // Info.subjects = value; | ||
128 | + for (let index = 0; index < value.length; index++) { | ||
129 | + Info.subjects.push(value[index].name) | ||
130 | + } | ||
131 | + }) | ||
105 | return data; | 132 | return data; |
106 | } | 133 | } |
107 | 134 | ||
... | @@ -123,14 +150,16 @@ async function getOne(page, index) { | ... | @@ -123,14 +150,16 @@ async function getOne(page, index) { |
123 | return Promise.resolve(data); | 150 | return Promise.resolve(data); |
124 | } | 151 | } |
125 | 152 | ||
126 | -// function that contact E_Campus | 153 | +async function close() { |
127 | -// if your account is correct, value = Promise { | 154 | + browser.close(); |
128 | -// { name: '캘린더 피드를 보려면 클릭하세요.', | 155 | +} |
129 | -// link: 'https://khcanvas.khu.ac.kr/feeds/calendars/user_~~~.ics' | 156 | + |
130 | -// } | ||
131 | -//} | ||
132 | -// if your account is incorrect, value = [ '', '', 'Incorrect user ID or password.' ] | ||
133 | 157 | ||
134 | exports.login = login; | 158 | exports.login = login; |
135 | exports.getPeed = getPeed; | 159 | exports.getPeed = getPeed; |
136 | exports.getSubjects = getSubjects; | 160 | exports.getSubjects = getSubjects; |
161 | +exports.close = close; | ||
162 | + | ||
163 | + | ||
164 | + | ||
165 | + | ... | ... |
server/libs/test.js
0 → 100644
1 | +const express = require("express"); | ||
2 | +const router = express.Router(); | ||
3 | + | ||
4 | +const { getPeed, getSubjects, login, close } = require("./E_Campus.js"); | ||
5 | + | ||
6 | +async function runrun() { | ||
7 | + page = await login("lorem", "lorem"); | ||
8 | + await getPeed(page); | ||
9 | + await getSubjects(page); | ||
10 | + await close(); | ||
11 | + console.log(Info); | ||
12 | + console.log(Info.subjects) | ||
13 | + | ||
14 | + } | ||
15 | + runrun(); | ||
16 | + | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
1 | const express = require("express"); | 1 | const express = require("express"); |
2 | const router = express.Router(); | 2 | const router = express.Router(); |
3 | 3 | ||
4 | -const { getPeed, getSubjects, login } = require("../libs/E_Campus.js"); | 4 | +const { getPeed, getSubjects, login, close } = require("../libs/E_Campus.js"); |
5 | 5 | ||
6 | router.post("/", async (req, res) => { | 6 | router.post("/", async (req, res) => { |
7 | console.log(`listened /login ${req.body.id} ${req.body.pw}`); | 7 | console.log(`listened /login ${req.body.id} ${req.body.pw}`); |
8 | const page = await login(req.body.id, req.body.pw); | 8 | const page = await login(req.body.id, req.body.pw); |
9 | if (page) { | 9 | if (page) { |
10 | try { | 10 | try { |
11 | - const peed = await getPeed(page); | 11 | + res.send("success"); |
12 | - res.send(peed); | ||
13 | } catch (e) { | 12 | } catch (e) { |
14 | console.log(e); | 13 | console.log(e); |
15 | res.send("error"); | 14 | res.send("error"); |
16 | } | 15 | } |
17 | - } else res.send("login failed"); | 16 | + } else res.send("failed"); |
18 | }); | 17 | }); |
19 | 18 | ||
20 | module.exports = router; | 19 | module.exports = router; | ... | ... |
-
Please register or login to post a comment