HyeonJun Jeon

[Implement] Combine branches

const puppeteer = require("puppeteer");
//use puppeteer
Info = {
id : "null",
pw : "null",
peed : "null",
subjects : []
}
function setTimeoutPromise(ms) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(), ms);
});
}
async function login(id, pw) {
try {
//for visibility, headless: false
browser = await puppeteer.launch({
const browser = await puppeteer.launch({
headless: false,
args: [
"--window-size=1920x1080",
......@@ -27,7 +15,7 @@ async function login(id, pw) {
],
});
const page = await browser.newPage();
const [page] = await browser.pages();
await page.setViewport({
width: 1920,
height: 1080,
......@@ -44,7 +32,7 @@ async function login(id, pw) {
page.waitForNavigation(),
]);
//When login is failed, return {err: "Incorrect~"}
//When login is failed, return false
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,22 +45,28 @@ 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;
let selector = "#global_nav_profile_link";
await page.waitForSelector(selector);
await page.click(selector);
const peed = await getPeed(page);
const subjects = await getSubjects(page);
browser.close();
return { peed, subjects };
}
} catch (err) {
console.log(err);
return "error";
}
}
//function that get your peed for your Ecampus calendar
async function getPeed(page) {
selector = "#global_nav_calendar_link > div.menu-item-icon-container > svg";
let selector =
"#global_nav_calendar_link > div.menu-item-icon-container > svg";
await page.waitForSelector(selector, { timeout: 1000 });
await Promise.all([page.click(selector), page.waitForNavigation()]);
......@@ -88,12 +82,14 @@ async function getPeed(page) {
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);
//close the peed window to get subjects
selector =
"body > div:nth-child(9) > div.ui-dialog-titlebar.ui-widget-header.ui-corner-all.ui-helper-clearfix > button";
await page.waitForSelector(selector, { timeout: 1000 });
await page.keyboard.press("Escape");
return data.link;
// let temp = await page.$("#calendar-feed-box-lower > a");
// const feed = await page.evaluate((data) => data.href, temp);
......@@ -107,28 +103,19 @@ async function getPeed(page) {
}
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"
);
let selector =
"#global_nav_courses_link > div.menu-item-icon-container > svg";
await page.waitForSelector(selector);
await page.click(selector);
let data = [];
let selector =
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));
}
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;
}
......@@ -147,19 +134,7 @@ async function getOne(page, index) {
}, temp);
// data.subjectName = await page.$eval("#nav-tray-portal > span > span > div > div > div > div > div > ul:nth-child(3) > li:nth-child(" + index + ") > a", (data) => data.textContent);
// data.subjectLink = await page.$eval("#nav-tray-portal > span > span > div > div > div > div > div > ul:nth-child(3) > li:nth-child(" + index + ") > a", (data) => data.href);
return Promise.resolve(data);
}
async function close() {
browser.close();
return data;
}
exports.login = login;
exports.getPeed = getPeed;
exports.getSubjects = getSubjects;
exports.close = close;
......
......@@ -3,17 +3,22 @@ const mysql2 = require("mysql2/promise");
const fs = require("fs");
const parseICal = require("./ICal");
function jcalToSQL(jcal, userID) {
const fsql = fs.readFileSync("server/libs/sql.pvdata", "utf8");
const [id, pw] = fsql.split("\r\n");
const connection = mysql.createConnection({
const [id, pw] = fs
.readFileSync("server/libs/sql.pvdata", "utf8")
.split("\r\n");
const connectOption = {
host: "localhost",
user: id,
password: pw,
database: "db",
});
};
connection.connect();
async function jcalToSQL(jcal, userID) {
// const fsql = fs.readFileSync("server/libs/sql.pvdata", "utf8");
// const [id, pw] = fsql.split("\r\n");
const connection = await mysql2.createConnection(connectOption);
await connection.connect();
const commonCols = [
"userID",
......@@ -48,14 +53,11 @@ function jcalToSQL(jcal, userID) {
else console.log("unexpected scheType", sche);
}
connection.query(dateQueryString, [dateSchedules], (error, result) => {
if (error) console.log(error);
else console.log(`schedules_date | ${result.affectedRows} rows added`);
});
connection.query(timeQueryString, [timeSchedules], (error, result) => {
if (error) console.log(error);
else console.log(`schedules_time | ${result.affectedRows} rows added`);
});
let result = await connection.query(dateQueryString, [dateSchedules]);
console.log(`schedules_date | ${result}`);
result = await connection.query(timeQueryString, [timeSchedules]);
console.log(`schedules_time | ${result}`);
connection.end();
}
......@@ -67,14 +69,4 @@ function jcalToSQL(jcal, userID) {
// const jcal = parseICal(fdata);
// jcalToSQL(jcal, 1);
const [id, pw] = fs
.readFileSync("server/libs/sql.pvdata", "utf8")
.split("\r\n");
const connectOption = {
host: "localhost",
user: id,
password: pw,
database: "db",
};
module.exports = { jcalToSQL, connectOption };
......
const { default: axios } = require("axios");
const express = require("express");
const router = express.Router();
const { getPeed, getSubjects, login, close } = require("./E_Campus.js");
const { login } = require("./E_Campus.js");
const parseICal = require("./ICal.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 { data: ical } = await axios({
url: "https://khcanvas.khu.ac.kr/feeds/calendars/user_YzxoryVh1hAQTeUJRljeaYavgmw1OXEGKIzNVpnI.ics",
method: "GET",
responseType: "blob",
});
const Jcal = parseICal(ical);
console.log(Jcal);
}
runrun();
......
......@@ -20,6 +20,27 @@ async function route() {
res.end();
}
});
// [{name, link}, ...]
subjectsRouter.post("/s", async (req, res) => {
console.log("post /db/subjects/s");
try {
const subList = [];
for (const s of req.body.list) {
const subjectID = s.link.split("courses/")[1];
const queryString = `
INSERT IGNORE INTO subjects
(ID, name)
VALUES (${subjectID},' ${s.name}')`;
await connection.query(queryString);
subList.push({ name: s.name, subjectID });
}
res.send(subList);
} catch (e) {
console.log(e);
res.end();
}
});
}
route();
......
......@@ -58,6 +58,36 @@ async function route() {
res.end();
}
});
// (userID, [subjectID, ...])
userSubjectRouter.post("/s", async (req, res) => {
console.log("post /db/user-subject/s");
const colors = [
"3ADF00",
"0040FF",
"FF0000",
"FFFF00",
"FF00FF",
"FF8000",
"6E6E6E",
"8000FF",
"B40431",
"2EFEF7",
];
try {
for (const i in req.body.subList) {
const queryString = `
INSERT INTO \`user-subject\`
(userID, subjectID, status, color)
VALUES (${req.body.userID}, ${req.body.subList[i].subjectID}, 1, '${colors[i]}')`;
await connection.query(queryString);
}
res.end();
} catch (e) {
console.log(e);
res.end();
}
});
}
route();
......
const cryptoJs = require("crypto-js");
const express = require("express");
const mysql2 = require("mysql2/promise");
const { connectOption } = require("../libs/MySQL");
......@@ -35,6 +36,26 @@ async function route() {
res.end();
}
});
// (loginID, loginPW, name, ical) => ID
usersRouter.post("/", async (req, res) => {
console.log("post /db/users/");
try {
const hashpw = cryptoJs.SHA256(req.body.loginPW).toString();
let queryString = `
INSERT INTO users
(loginID, loginPW, name, ical)
VALUES ('${req.body.loginID}', '${hashpw}', '${req.body.name}', '${req.body.ical}')`;
await connection.query(queryString);
queryString = `SELECT LAST_INSERT_ID() ID`;
const [results] = await connection.query(queryString);
res.send(results[0]);
} catch (e) {
console.log(e);
res.end();
}
});
}
route();
......
const axios = require("axios");
const express = require("express");
const router = express.Router();
const { getPeed, getSubjects, login, close } = require("../libs/E_Campus.js");
const { login } = require("../libs/E_Campus.js");
const parseICal = require("../libs/ICal.js");
const { jcalToSQL } = require("../libs/MySQL.js");
// (id, pw) => failed | error | {peed, subjects, name, ID}
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) {
const result = await login(req.body.id, req.body.pw);
if (result && result !== "error") {
try {
res.send("success");
const { peed, subjects } = result;
const resUser = await axios.post("http://localhost:3001/db/users", {
loginID: req.body.id,
loginPW: req.body.pw,
ical: peed,
});
const userID = resUser.data.ID;
const { data: subList } = await axios.post(
"http://localhost:3001/db/subjects/s",
{
list: subjects,
}
);
await axios.post("http://localhost:3001/db/user-subject/s", {
userID,
subList,
});
const { data: ical } = await axios({
url: peed,
method: "GET",
responseType: "blob",
});
await jcalToSQL(parseICal(ical), userID);
res.send({ userID });
} catch (e) {
console.log(e);
res.send("error");
}
} else res.send("failed");
} else res.send(result ? "failed" : "error");
});
module.exports = router;
......
......@@ -45,7 +45,9 @@ const Login = () => {
if (isCorrectPW) await localforage.setItem("userID", Number(userDBID));
else {
setState({ ...state, btn: "Login" });
alert("ID/PW를 확인해주세요");
alert(
"등록된 ID/PW와 다릅니다\n(비밀번호가 변경되었다면 관리자에게 문의하세요)"
);
return;
}
} else {
......@@ -57,14 +59,17 @@ const Login = () => {
pw: state.pw,
}
);
if (loginResult === "login failed") {
if (loginResult === "failed") {
setState({ ...state, btn: "Login" });
alert("ID/PW를 확인해주세요");
return;
} else if (loginResult === "error") {
setState({ ...state, btn: "Login" });
alert("다시 시도해 주세요");
return;
}
// + else (성공시) localforage에 userID추가
await localforage.setItem("userID", loginResult.userID);
}
// + localforage에 id pw 추가
await localforage.setItem("id", state.id);
await localforage.setItem("pw", state.pw);
await localforage.setItem("session", true);
......