Skip to content

Commit

Permalink
feat: 新增歷年修過的課程API
Browse files Browse the repository at this point in the history
feat(getSchedule): 新增回傳逾時提醒
  • Loading branch information
Chinlinlee committed Jan 25, 2021
1 parent b5e1d34 commit 1566f70
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions routes/api/Schedule/controller/getSchedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const _ = require("lodash");
module.exports = async function(req , res) {
let course =await getCourse(req);
if (!course) {
req.flash('error' , '學校系統逾時,請重新登入');
req.logout();
return res.status(401).send();
}
Expand Down
42 changes: 42 additions & 0 deletions routes/api/learnMap/controller/get_tookCourse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const fetch = require('node-fetch');
const {URLSearchParams} =require('url');
const { getILMSJar } = require('../../../My_Func');
const cherrio = require('cheerio');
const _ = require('lodash');
const { eq } = require('lodash');

module.exports = async function (req , res) {
let stuAllCourse = [];
let j = getILMSJar(req);
let fetchCookie = require('fetch-cookie')(fetch , j);
let ilmsAllCourseRes = await fetchCookie('https://ilms.ntunhs.edu.tw/home.php?f=allcourse');
let ilmsAllCourseResText = await ilmsAllCourseRes.text();
let $ = cherrio.load(ilmsAllCourseResText);
$('#copyright').remove();
$('.tblTitle').each(function (index , element) {
let title = $(element).text().replace(/ /gm , '');
let titleSplit = title.split(/\n/);
let titleClean = _.compact(titleSplit);
console.log(titleClean);
});
let staticTitle = ["課程編號" , "標題" , "老師" , "學分" , "班級"];
$('td').each(function(index , td) {
let tdText = $(td).text().trim();
if (staticTitle.indexOf(tdText)<=-1) {
if (index % 5 == 4) {
let courseObj = {
courseId : $('td').eq(index-4).text() ,
courseName : $('td').eq(index-3).text() ,
courseURL : `https://ilms.ntunhs.edu.tw${$('td').eq(index-3).find('a').eq(0).attr('href')}` ,
courseTeacher : $('td').eq(index-2).text() ,
courseCredit : $('td').eq(index-1).text() ,
courseClass : $(td).text()
}
_.set(courseObj , 'courseSem' , courseObj.courseId.substr(0 , 4));
_.set(courseObj, 'courseNormalId' , courseObj.courseId.substring(4));
stuAllCourse.push(courseObj);
}
}
});
res.send(stuAllCourse);
}
2 changes: 2 additions & 0 deletions routes/api/learnMap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ const express = require('express');
const router = express.Router();

router.get('/' , require('./controller/get_learnMap'));
router.get('/tookcourse' , require('./controller/get_tookCourse'));

module.exports = router;

0 comments on commit 1566f70

Please sign in to comment.