GS--使用小爱同学开发工具制作教务系统课程表
对于大学生来说每次打开课程表是一件麻烦的事情
刚好对于米粉的我,可以利用小爱同学自带的课程表系统,开发一套在桌面显示的课程表
开发环境:小爱核心开发工具(先去官方文档下载)
官方开发文档:https://ldtu0m3md0.feishu.cn/docs/doccnhZPl8KnswEthRXUz8ivnhb
1.我们打开学校的教务系统
2.可以看到,这个页面,由于我学校的教务系统很扯淡,所以我不打算使用官方的脚本呢,而是利用网络提交的方式来获取课程表的方式
function scheduleHtmlProvider(iframeContent = "", frameContent = "", dom = document) {
let request = new XMLHttpRequest()
//这里写自己的课程表链接地址
request.open('GET', '/studentportal.php/Jxxx/xskbxx/optype/1', false)
request.send()
//这里我利用了正则表达获取所有周课程表
var num = request.responseText.match(/selecttab\(\'(.+?)\'\)/);
zongurl = ClearBr(request.responseText)
zongurl = CTim(zongurl)
//这里我利用了正则表达获取所有周课程表
var urls = zongurl.match(/case\"\d+\":p.find\(\'iframe\'\).attr\(\'src\',\'(.+?)\'\)/g);
//这里就是获取到的列表,当前周数减去1就是这周的课程表
var urls2 = urls[num[1]-1]
var urls3 = urls2.match(/p.find\(\'iframe\'\).attr\(\'src\',\'(.+?)\'\)/);
//获取之后在发送,获取当前周页面返回给scheduleHtmlParser页面
request.open('GET', urls3[1], false)
request.send()
//这个是吧表格的都改成2,不然遍历的时候会变得有很多空课程
var str2 = request.responseText.replace(/rowspan="1"/g, 'rowspan="2"');//g是重点,如果替换的为‘/’,需要转义,吧/a/g替换为'/\//g'
return str2
}
function ClearBr(key) {
key = key.replace(/<\/?.+?>/g,"");
key = key.replace(/[\r\n]/g, "");
return key;
}
function CTim(str) {
return str.replace(/\s/g,'');
}
3.我们开始写另一个脚本(具体的意思写在了注释里面了),真的是很曲折,特别是这些思路
function scheduleHtmlParser(html) {//使用Cheerio,把每周的课已列表的方式输出
let $raw = $("td[class='b1 r1'],td[class='b1 r2']").toArray();
console.log($raw)
let courses = [];
let name = "";
let teacher = "";
let weeks = "";
let sections = "";
let position = "";
var valuenull = 0
var numindex = 0
var numzhou = 6
//遍历每个列表里的内容
for (index in $raw) {
console.log("当前"+numindex)
if(numindex != 0)
{
if(index % numzhou == 0)
{
//因为改列表多出来的表格会影响周数,所以这里用整除求余数为0的时候跳过获取
//第一周,因为列表都是从0开始算的所以第一周整除的话需要改成6,下次直接除7即可
console.log("当前除数"+numzhou)
if(index==6)
{
console.log("第一次等于6")
console.log("整除"+index % 6 )
data = $raw[numindex]
console.log(data)
numindex = numindex+valuenull //valuenull这个变量是获取多余的表格然后跳过个数
numzhou = numzhou+1
console.log(numindex)
valuenull = 0
}
else
{
console.log("整除"+index % 7 )
data = $raw[numindex]
console.log(data)
numindex = numindex+valuenull+1
console.log(valuenull)
valuenull = 0
}
}
else
{
console.log(numindex)
data = $raw[numindex]
console.log(data)
numindex = numindex+1
}
}
else
{
data = $raw[numindex]
console.log(data)
console.log(numindex)
numindex = numindex+1
}
var msg = "";
//try是异常捕获防止出现报错就停止的代码
try{
if (typeof data.children[0].children[0].children[0] == "undefined"){
name="没课"
valuenull = valuenull+1
}
else{
name=data.children[0].children[0].children[0].data}
}catch(err){
//因为没有课所以异常了,就在这里加1,跳过多出来的课程(因为没课的列表才会多出,所以可以用这个方法)
valuenull = valuenull+1
name=""
}
try{
//获取课程名字
//please notice these data are from object, therefore please check whether they are existed.
//for rigorous, please check undefined
if (data.children.length == 12) {
name = name + '[待定]';
}
console.info(name)
}catch(err)
{
}
//获取任课老师的名字
try{
teacher = data.children[0].children[1].children[0].data
console.info(teacher)
}catch(err)
{
teacher=""
}
//获取周数
try{
weeks = html.match(/第(.+?)周/)[0];
console.info(weeks)
}catch(err)
{
weeks=""
}try{
position=data.children[0].children[2].children[0].children[0].data
console.info(position)
}catch(err)
{
//获取上课地点
position = ""
}try{
sections = data.children[0].children[3].data;
console.info(sections)
}catch(err)
{
//保存在sections这个变量,运行成功的话上传小爱,等待审核即可
sections = ""
}try{
//replace for creating the arry
//correct structure.
let courseInfo = {
"name": name,
"position": position,
"day": _get_day(index),
"teacher": teacher,
"sections":sections,
"weeks": weeks
};
courses.push(courseInfo);
}catch(err)
{
}
}
//打印最终的数据
console.info(courses);
finalResult = {
"courseInfos": courses,
"sectionTimes": createSectionTimes()
};
return finalResult;
}
//这个是遍历列表给每一节课加星期
function _get_day(num_index) {
//day will be calculated because the index it self is formatted.
return (num_index) % 7 + 1
}
function _clear_null(arr) {
//to clean the char such as '&nbsp;' or ' '
let result = []
for (i in arr) {
if (arr[i].length != 0) {
result.push(arr[i]);
}
}
return result
}
//这个是每节课的时间
function createSectionTimes() {
//this is the HEU standard section time.
//get it on the official website.
let sectionTimes = [{
"section": 1,
"startTime": "08:10",
"endTime": "08:55"
}, {
"section": 2,
"startTime": "09:05",
"endTime": "09:50"
}, {
"section": 3,
"startTime": "10:10",
"endTime": "10:55"
}, {
"section": 4,
"startTime": "11:05",
"endTime": "11:50"
}, {
"section": 5,
"startTime": "12:10",
"endTime": "12:55"
}, {
"section": 6,
"startTime": "13:05",
"endTime": "13:50"
}, {
"section": 7,
"startTime": "14:00",
"endTime": "14:45"
}, {
"section": 8,
"startTime": "14:55",
"endTime": "15:40"
}, {
"section": 9,
"startTime": "15:50",
"endTime": "16:35"
}, {
"section": 10,
"startTime": "16:45",
"endTime": "17:30"
}, {
"section": 11,
"startTime": "18:30",
"endTime": "19:15"
}, {
"section": 12,
"startTime": "19:25",
"endTime": "20:10"
}, {
"section": 13,
"startTime": "20:20",
"endTime": "21:05"
}, {
"section": 14,
"startTime": "21:15",
"endTime": "22:00"
}
]
return sectionTimes
}
4.由于一些小问题会导致,多出来一列,所以该脚本还是有点缺陷,等有空的时候再来改改吧,如果能获取到表格的列数是多少就可以直接跳过多余表格,但是博主技术不行交给你们了,有办法的话可以加我跟我交流一下哦!
评论 (0)