HyeonJun Jeon

[Modify] Puppeteer dynamic waitTime

......@@ -7,79 +7,107 @@ function setTimeoutPromise(ms) {
});
}
async function loglogin(id, pw) {
try{
async function login(id, pw) {
try {
//for visibility, headless: false
const browser = await puppeteer.launch({headless: false, args:['--window-size=1920,1080']});
const browser = await puppeteer.launch({
headless: false,
args: ["--window-size=1920,1080"],
});
const page = await browser.newPage();
await page.setViewport({
width:1920,
height:1080
})
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');
width: 1920,
height: 1080,
});
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"
);
await page.type("#login_user_id", id);
await page.type("#login_user_password", pw);
//press the login button on E_Campus homepage
await page.click('#form1 > div > div.login_btn > a > span');
await page.waitForTimeout(500);
await setTimeoutPromise(1000);
await Promise.all([
page.click("#form1 > div > div.login_btn > a > span"),
page.waitForNavigation(),
]);
//When login is failed, return [id, pw, message] -> ["", "", "Incorrect~"]
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'){
return ["", "", "Incorrect user ID or password."];
//When login is failed, return {err: "Incorrect~"}
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"
) {
return false;
} else {
return page;
}
else {
// return [id, pw, `I got data { id:"${id}" pw:"${pw}" }`];
await peedGet(page);
await page.waitForTimeout(3000);
await page.click('body > div:nth-child(9) > div.ui-dialog-titlebar.ui-widget-header.ui-corner-all.ui-helper-clearfix > button > span');
await subjectGet(page);
}
}catch(err){
console.log(err)
} catch (err) {
console.log(err);
}
return result;
}
//function that get your peed for your Ecampus calendar
async function peedGet(page) {
await page.waitForTimeout(5000);
async function getPeed(page) {
console.log("getPeed");
// await page.goto('https://khcanvas.khu.ac.kr');
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');
await page.waitForTimeout(1000);
await page.click('#global_nav_calendar_link > div.menu-item-icon-container > svg');
await page.waitForTimeout(10000);
await page.click('#calendar-feed > button');
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 selector =
"#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 page.click(selector);
console.log(Promise.resolve(data));
return Promise.resolve(data);
selector = "#global_nav_calendar_link > div.menu-item-icon-container > svg";
await page.waitForSelector(selector);
page.click(selector);
selector = "#calendar-feed > button";
await page.waitForSelector(selector);
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 temp = await page.$("#calendar-feed-box-lower > a");
const feed = await page.evaluate((data) => data.href, temp);
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) {
console.log("getSubjects");
await page.click(
"#global_nav_courses_link > div.menu-item-icon-container > svg"
);
async function subjectGet(page) {
await page.waitForTimeout(5000);
await page.click('#global_nav_courses_link > div.menu-item-icon-container > svg')
await page.waitForTimeout(5000);
let data= [];
const number = await page.$$eval("#nav-tray-portal > span > span > div > div > div > div > div > ul:nth-child(3) > li", (data) => data.length);
let data = [];
let selector =
"#nav-tray-portal > span > span > div > div > div > div > div > ul:nth-child(3) > li";
await page.waitForSelector(selector);
const number = await page.$$eval(selector, (data) => data.length);
for (let index = 0; index < number; index++) {
data.push(await getOne(page, index + 1));
}
console.log(Promise.resolve(data));
return data;
}
async function getOne(page, index) {
let data = {};
let temp = await page.$("#nav-tray-portal > span > span > div > div > div > div > div > ul:nth-child(3) > li:nth-child(" + index + ") > a");
let temp = await page.$(
"#nav-tray-portal > span > span > div > div > div > div > div > ul:nth-child(3) > li:nth-child(" +
index +
") > a"
);
data.name = await page.evaluate((data) => {
return data.textContent;
}, temp);
......@@ -91,7 +119,6 @@ async function getOne(page, index) {
return Promise.resolve(data);
}
// function that contact E_Campus
// if your account is correct, value = Promise {
// { name: '캘린더 피드를 보려면 클릭하세요.',
......@@ -99,17 +126,5 @@ async function getOne(page, index) {
// }
//}
// if your account is incorrect, value = [ '', '', 'Incorrect user ID or password.' ]
async function login(id, pw) {
await setTimeoutPromise(1000);
try {
// value has the list ["","","Incorrect~"]
const value = await loglogin(id, pw);
console.log(value);
} catch (e) {
console.error(e);
}
}
// login("2021105253", "qtw@@04271");
export { login };
export { login, getPeed, getSubjects };
......
import express from "express";
import { login } from "./libs/E_Campus.js";
import { getPeed, getSubjects, login } from "./libs/E_Campus.js";
import fs from "fs";
const app = express();
......@@ -13,15 +13,20 @@ router.get("/", (req, res) => {
app.use("/", router);
fs.readFile("server/login.pvdata", "utf8", (err, data) => {
fs.readFile("server/libs/login.pvdata", "utf8", async (err, data) => {
if (err) {
console.error(err);
console.log("server/login.pvdata에 ID\\nPW를 작성");
console.log("server/libs/login.pvdata에 ID\\nPW를 작성");
return;
}
login(...data.split("\r\n"));
const page = await login(...data.split("\r\n"));
if (page) {
const feed = await getPeed(page);
const subjects = await getSubjects(page);
console.log({ feed, subjects });
app.listen(3001, function () {
console.log("listening on 3001");
});
} else console.log("Incorrect user ID or password.");
});
......