E_Campus.js 4.57 KB
const puppeteer = require("puppeteer");
//use puppeteer

async function login(id, pw) {
  try {
    //for visibility, headless: false
    const browser = await puppeteer.launch({
      headless: true,
      args: [
        "--window-size=1920x1080",
        "--start-maximized",
        "--no-sandbox",
        "--disable-dev-shm-usage",
        "--lang=ko",
      ],
    });

    const [page] = await browser.pages();
    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"
    );
    await page.type("#login_user_id", id);
    await page.type("#login_user_password", pw);

    //press the login button on E_Campus homepage
    await Promise.all([
      page.click("#form1 > div > div.login_btn > a > span"),
      page.waitForNavigation(),
    ]);

    //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"
    ) {
      return false;
    } else {
      // 내 강의실

      // 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 Promise.all([page.click(selector), page.waitForNavigation()]);

      await page.goto("https://khcanvas.khu.ac.kr/");

      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) {
  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()]);

  selector = "#calendar-feed > button";
  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);

  //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);

  // 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) {
  let selector =
    "#global_nav_courses_link > div.menu-item-icon-container > svg";
  await page.waitForSelector(selector);
  await page.click(selector);

  let data = [];
  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));
  }
  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"
  );
  data.name = await page.evaluate((data) => {
    return data.textContent;
  }, temp);
  data.link = await page.evaluate((data) => {
    return data.href;
  }, 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 data;
}

exports.login = login;