HyeonJun Jeon

Merge branch 'info_manage' into 'master'

const puppeteer = require("puppeteer");
//use puppeteer
Info = {
id : "null",
pw : "null",
peed : "null",
subjects : []
}
function setTimeoutPromise(ms) {
return new Promise((resolve, reject) => {
......@@ -10,8 +16,8 @@ function setTimeoutPromise(ms) {
async function login(id, pw) {
try {
//for visibility, headless: false
const browser = await puppeteer.launch({
headless: true,
browser = await puppeteer.launch({
headless: false,
args: [
"--window-size=1920x1080",
"--start-maximized",
......@@ -51,6 +57,11 @@ async function login(id, pw) {
// "#visual > div > div.xn-main-login-container > div:nth-child(2) > div.xn-main-link-wrap.xn-main-lms-link-wrap > a";
// await page.waitForSelector(selector);
// await Promise.all([page.click(selector), page.waitForNavigation()]);
const promise1 = Promise.resolve();
promise1.then(value => {
Info.id = id;
Info.pw = pw;
})
await page.goto("https://khcanvas.khu.ac.kr/");
return page;
}
......@@ -69,27 +80,36 @@ async function getPeed(page) {
await page.waitForSelector(selector);
await page.click(selector);
// let data = {};
// let temp = await page.$("#calendar-feed-box-lower > a");
// data.name = await page.evaluate((data) => {
// return data.textContent;
// }, temp);
// data.link = await page.evaluate((data) => {
// return data.href;
// }, temp);
let data = {};
let temp = await page.$("#calendar-feed-box-lower > a");
const feed = await page.evaluate((data) => data.href, temp);
data.name = await page.evaluate((data) => {
return data.textContent;
}, temp);
data.link = await page.evaluate((data) => {
return data.href;
}, temp);
// console.log(Promise.resolve(data));
const promise2 = Promise.resolve(data.link);
promise2.then(value => {
Info.peed = value;
})
return Promise.resolve(data);
selector =
"body > div:nth-child(9) > div.ui-dialog-titlebar.ui-widget-header.ui-corner-all.ui-helper-clearfix > button > span";
await page.waitForSelector(selector, { timeout: 1000 });
await page.click(selector);
// let temp = await page.$("#calendar-feed-box-lower > a");
// const feed = await page.evaluate((data) => data.href, temp);
return feed;
// selector =
// "body > div:nth-child(9) > div.ui-dialog-titlebar.ui-widget-header.ui-corner-all.ui-helper-clearfix > button > span";
// await page.waitForSelector(selector, { timeout: 1000 });
// await page.click(selector);
// return feed;
}
async function getSubjects(page) {
//close the peed window to get subjects
await page.click('body > div:nth-child(9) > div.ui-dialog-titlebar.ui-widget-header.ui-corner-all.ui-helper-clearfix > button > span');
await page.waitForTimeout(500);
await page.click(
"#global_nav_courses_link > div.menu-item-icon-container > svg"
);
......@@ -102,6 +122,13 @@ async function getSubjects(page) {
for (let index = 0; index < number; index++) {
data.push(await getOne(page, index + 1));
}
const promise3 = Promise.resolve(data);
promise3.then(value => {
// Info.subjects = value;
for (let index = 0; index < value.length; index++) {
Info.subjects.push(value[index].name)
}
})
return data;
}
......@@ -123,14 +150,16 @@ async function getOne(page, index) {
return Promise.resolve(data);
}
// function that contact E_Campus
// if your account is correct, value = Promise {
// { name: '캘린더 피드를 보려면 클릭하세요.',
// link: 'https://khcanvas.khu.ac.kr/feeds/calendars/user_~~~.ics'
// }
//}
// if your account is incorrect, value = [ '', '', 'Incorrect user ID or password.' ]
async function close() {
browser.close();
}
exports.login = login;
exports.getPeed = getPeed;
exports.getSubjects = getSubjects;
exports.close = close;
......
const express = require("express");
const router = express.Router();
const { getPeed, getSubjects, login, close } = require("./E_Campus.js");
async function runrun() {
page = await login("lorem", "lorem");
await getPeed(page);
await getSubjects(page);
await close();
console.log(Info);
console.log(Info.subjects)
}
runrun();
\ No newline at end of file
const express = require("express");
const router = express.Router();
const { getPeed, getSubjects, login } = require("../libs/E_Campus.js");
const { getPeed, getSubjects, login, close } = require("../libs/E_Campus.js");
router.post("/", async (req, res) => {
console.log(`listened /login ${req.body.id} ${req.body.pw}`);
const page = await login(req.body.id, req.body.pw);
if (page) {
try {
const peed = await getPeed(page);
res.send(peed);
res.send("success");
} catch (e) {
console.log(e);
res.send("error");
}
} else res.send("login failed");
} else res.send("failed");
});
module.exports = router;
......