青果教务系统小爱课程表适配代码开源

2021年4月10日 17:48 阅读 1.79k 评论 2

前阵子适配了本校的小爱课程表(青果教务系统),但是当时因为只有单周课所以在开发中忽略了双周的情况,这学期开学后发现了这个问题,今天修复后顺便将代码开源出来,方便更多的人适配自己的学校

首先是获取课表页面中的课表对应的iframe的源代码,若没有iframe则返回table的父元素即可

function scheduleHtmlProvider(iframeContent = "", frameContent = "", dom = document) { 
    return dom.getElementsByName("frmRpt")[0].contentDocument.body.innerHTML  
} 

拿到源代码后,我们找到其中课表对应的table,与此同时,遍历其中的tr标签

具体操作很简单没什么说的,就是提取每个tr中的课程信息然后push到数组中返回就可以了

function scheduleHtmlParser(html) { 
    let $ = cheerio.load(html, {decodeEntities: false}); 
    let result = []; 
    let course_count = 0; 
    let weeks = {: 1, : 2, : 3, : 4, : 5}; 
    let course_times = {1: 1, 3: 2, 5: 3, 7: 4}; 
    $('table').find('tbody').find('tr').each(function (row, _) { // 遍历每一个tr提取其中的信息 
        if (row > 2) { 
            let course_info = {}; // 初始化一个课程信息对象 
            $(this).find('td').each(function (col, _) { 
                if (col === 1) { // 课程名 
                    if (!$(this).text()) { 
                        course_info['name'] = result[course_count - 1].name; 
                    } else { 
                        course_info['name'] = $(this).text().split(']')[1]; 
                    } 
                } 
                if (col === 4) { // 任课教师 
                    course_info['teacher'] = $(this).text(); 
                } 
                if (col === 5) { // 节次 
                    course_info['day'] = weeks[$(this).text().slice(0, 1)]; 
                    course_info['sections'] = [{section: course_times[$(this).text().slice(2, 3)]}]; 
                    if ($(this).text().slice(-1) === '单') { 
                        course_info['weeks'] = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]; 
                    } else if ($(this).text().slice(-1) === '双') { 
                        course_info['weeks'] = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]; 
                    } else { 
                        course_info['weeks'] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; 
                    } 
                } 
                if (col === 6) { // 地点 
                    course_info['position'] = $(this).text(); 
                } 
            }); 
            result.push(course_info); 
            course_count++; 
        } 
    }); 
    result['sectionTimes'] = [ // 此处为学校的上课时间 
        { 
            "section": 1, 
            "startTime": "08:05", 
            "endTime": "09:55" 
        }, 
        { 
            "section": 2, 
            "startTime": "10:20", 
            "endTime": "12:10" 
        }, 
        { 
            "section": 3, 
            "startTime": "14:30", 
            "endTime": "16:20" 
        }, 
        { 
            "section": 4, 
            "startTime": "16:45", 
            "endTime": "18:35" 
        } 
    ]; 
    return {courseInfos: result} 
} 

如果你在适配中遇到了问题可以在评论区留言

最后修改于2021年4月10日 18:00
©允许规范转载

版权声明:如无特殊说明,文章均为本站原创,转载请注明出处

本文链接:https://www.yangyingqi.com/45.html

JavaScript
微信
支付宝
登录后即可进行评论/回复