From 9566e39b5ae8661afc3260216d1575c9bd41de0e Mon Sep 17 00:00:00 2001 From: chin Date: Mon, 11 Jan 2021 22:17:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=94=B9selenium=E7=82=BAfetc?= =?UTF-8?q?h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/learnMap/controller/get_learnMap.js | 115 ++++++++---------- 1 file changed, 53 insertions(+), 62 deletions(-) diff --git a/routes/api/learnMap/controller/get_learnMap.js b/routes/api/learnMap/controller/get_learnMap.js index 778b5ea..1251ec2 100644 --- a/routes/api/learnMap/controller/get_learnMap.js +++ b/routes/api/learnMap/controller/get_learnMap.js @@ -1,52 +1,31 @@ const myFunc = require("../../../My_Func"); const fetch = require('node-fetch'); const cheerio = require('cheerio'); -const chrome = require('selenium-webdriver/chrome'); -const webdriver = require('selenium-webdriver'); //导入selenium 库 const { courseProgram } = require('../../../../models/NTUNHS/CourseProgram/CourseProgram'); +const { getCourseJson } = require('../../Course/controller/get_Course'); const _ = require('lodash'); -const fs = require('fs'); module.exports = async function (req, res) { let sessionLearnMap = req.session.learnMap; + let sessionStuInfo = req.session.stuInfo; if (sessionLearnMap) { return res.send(sessionLearnMap); } - let opt = new chrome.Options(); - opt.addArguments('--headless'); - opt.addArguments('--disable-gpu'); - opt.addArguments('--incognito'); - opt.setUserPreferences({ "download.default_directory": __dirname }); - let driver = await new webdriver.Builder().forBrowser('chrome').setChromeOptions(opt).build(); let j = myFunc.getJar(req); - let cookieStr = j.getCookieStringSync("http://system8.ntunhs.edu.tw"); - let aspSessionID = cookieStr.split("=")[1]; - await driver.navigate().to("http://system8.ntunhs.edu.tw/myNTUNHS_student/Modules/Map/tab/Map_tab_14.aspx?dept="); - if (aspSessionID) { - await driver.manage().addCookie({ - name: "ASP.NET_SessionId", - value: aspSessionID - }); - } - await driver.navigate().to("http://system8.ntunhs.edu.tw/myNTUNHS_student/Modules/Map/qry/Map_qry_20.aspx"); - try { - await driver.wait(webdriver.until.elementLocated(webdriver.By.id('pAlertMsg')), 2000); - let $ = cheerio.load(await driver.getPageSource()); - let alertMsg = $("#pAlertMsg").text(); - if (alertMsg.includes("請先登入系統")) { - req.logout(); - req.flash("閒置過久,請重新登入"); - return res.status(401).send(); - } - } catch (e) { - /* $.post('Map_qry_20.aspx', { 'action': 'htmlCreditHis', 'group': $('select[id*=sltDeptGroup]').find('option:selected').attr('groupno'), 'deptno': $('select[id*=sltDeptGroup]').find('option:selected').attr('deptno'), 'year': $('input:hidden[id*=hidEnterYear]').val(), 'semno': $('input:hidden[id$=hidSEMNO]').val(), 'edutype': $('select[id*=sltDeptGroup]').find('option:selected').attr('edutype') }, function(data) { $('#divCreditHistory').html(data); setFrameSize(); return false; }, 'html');*/ - await driver.sleep(500); - let $ = cheerio.load(await driver.getPageSource()); + let fetchCookie = require('fetch-cookie')(fetch , j); + let htmlCreditRes = await fetchCookie(`https://system8.ntunhs.edu.tw/myNTUNHS_student/Modules/Map/qry/Map_qry_20.aspx?action=htmlCredit&group=${sessionStuInfo.groupno}&deptno=${sessionStuInfo.deptno}&year=${sessionStuInfo.entryYear}&semno=${sessionStuInfo.lastSem}&edutype=${sessionStuInfo.edutypeCode}`); + let htmlCreditText = await htmlCreditRes.text(); + let $ = cheerio.load(htmlCreditText); + $("table").addClass("table table-bordered table-sm"); + $("table").attr('style', ''); + $("span").remove(); + let tablehtml = $.html(); - $("#divCreditInfo table").addClass("table table-bordered table-sm"); - $("#divCreditInfo table").attr('style', ''); - $("#divCreditInfo").find("span").remove(); - let passCourse = []; + //#region 獲取學習地圖修過的課 + let htmlCourseRes = await fetchCookie(`https://system8.ntunhs.edu.tw/myNTUNHS_student/Modules/Map/qry/Map_qry_20.aspx?action=htmlCourse&group=${sessionStuInfo.groupno}&deptno=${sessionStuInfo.deptno}&year=${sessionStuInfo.entryYear}&semno=${sessionStuInfo.lastSem}&edutype=${sessionStuInfo.edutypeCode}`); + let htmlCourseText = await htmlCourseRes.text(); + $ = cheerio.load(htmlCourseText); + let passCourse = []; $(".tdCourse input").each(function (index, element) { let isChecked = $(element).attr("defaultchecked") == "true"; if (isChecked) { @@ -56,36 +35,48 @@ module.exports = async function (req, res) { let course = $(element).parent().parent().children().text(); passCourse.push(course); } - }); - let tablehtml = $("#divCreditInfo").html(); - await driver.quit(); - let faculty = req.session.stuInfo.stuFaculty; - let myProgram = []; + }); + let faculty = req.session.stuInfo.stuFaculty; + let myProgram = []; - for (let key in courseProgram[faculty]) { - programObj = { - key : key , - course : [] , - credit : 0 - }; - let program = courseProgram[faculty][key].course; - for (let i = 0 ; i < program.length ; i++) { - let course = program[i]; - if (passCourse.findIndex(v => v == course.name) >=0 ) { - programObj.credit += course.credit; - programObj.course.push(course); - } + for (let key in courseProgram[faculty]) { + programObj = { + key : key , + course : [] , + credit : 0 + }; + let program = courseProgram[faculty][key].course; + for (let i = 0 ; i < program.length ; i++) { + let course = program[i]; + if (passCourse.findIndex(v => v == course.name) >=0 ) { + programObj.credit += course.credit; + programObj.course.push(course); } - myProgram.push(programObj); } - req.session.learnMap = { - hitCredit : tablehtml , - program : myProgram - }; - return res.send(req.session.learnMap); + myProgram.push(programObj); + } + //#endregion + + //獲取所有修課明細 + /*let allSemno = []; + let entryYearInt = parseInt(sessionStuInfo.entryYear); + let semno = sessionStuInfo.lastSem.substr(0 , 3); + let lastSemInt = parseInt(semno); + for (let i = entryYearInt ; i <= lastSemInt ; i++) { + allSemno.push(`${i}1`); + allSemno.push(`${i}2`); + } + let allOverCourse = []; + for (sem of allSemno) { + let nowCourse = await getCourseJson(req , sem); + allOverCourse.push(nowCourse); } - /* let el = await driver.findElement(webdriver.By.id(`pAlertMsg`)); - await driver.wait(webdriver.until.elementIsVisible(el),1000);*/ + console.log(allOverCourse);*/ + req.session.learnMap = { + hitCredit : tablehtml , + program : myProgram + }; + return res.send(req.session.learnMap); }