来自对https://www.bilibili.com/video/BV1eW421A7Pr/?vd_source=57eca0496546a5d0fcb88a070015290b的修改。
up主的代码在选择国科大时会显示“此教务的适配将在下一版本提供”/(ㄒoㄒ)/~~,故在up主的基础上扩展了个能下载csv版本的,csv导入时不需要选学校,导入方法同HTML~
代码如下:
//ref:https://www.bilibili.com/video/BV1eW421A7Pr/?vd_source=57eca0496546a5d0fcb88a070015290b
//revised: zb 20240304
function convertArrayToCSV(data) {
//from:https://www.zhihu.com/question/588722642/answer/2934862302
var csv = '';
for (var i = 0; i < data.length; i++) {
var row = data[i];
for (var j = 0; j < row.length; j++) {
var val = row[j] === null ? '' : row[j].toString();
if (val.indexOf(',') !== -1 || val.indexOf('"') !== -1 || val.indexOf('\n') !== -1) {
val = '"' + val.replace(/"/g, '""') + '"';
}
if (j > 0) {
csv += ',';
}
csv += val;
}
csv += '\n';
}
return csv;
}
function downloadCSV(csv) {
//from:https://www.zhihu.com/question/588722642/answer/2934862302
//这样生成的中文是乱码呀
var link = document.createElement('a');
link.href = 'data:text/csv;charset=utf-8,' + encodeURIComponent(csv);
link.download = 'data.csv';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
function downloadCSV2(csv) {
var blob = new Blob([csv], {type: "text/html"});
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = '课表.csv';
document.body.appendChild(a);
a.click();
}
function weekHanzi2num(str){
var res
if (str.includes("星期一")){
res = str.replace("星期一","星期1");
}
else if(str.includes("星期二")){
res = str.replace("星期二","星期2");
}
else if(str.includes("星期三")){
res = str.replace("星期三","星期3");
}
else if(str.includes("星期四")){
res = str.replace("星期四","星期4");
}
else if(str.includes("星期五")){
res = str.replace("星期五","星期5");
}
else if(str.includes("星期六")){
res = str.replace("星期六","星期6");
}
else if(str.includes("星期日")){
res = str.replace("星期日","星期7");
}
return res;
}
function GetData(dom){
var courseInfo = {};
// 获取课程名称
courseInfo['课程名称'] = dom.querySelector('p').textContent.trim().replace("课程名称:","");
var courseInfoElement = dom.querySelector('.table.table-striped.table-bordered.table-advance.table-hover');
var tableRows = courseInfoElement.querySelectorAll('tr');
var rowNum = tableRows.length;
console.log("一周时间点数:",rowNum/3);
for(let i=0;i<rowNum/3;++i){
var classTime = courseInfoElement.querySelectorAll('td')[0+3*i].textContent.trim();
//console.log(classTime);
var classTime = weekHanzi2num(classTime);
// 使用正则表达式提取星期、开始节数和结束节数
var regex = /星期(\S+): 第(\d+).*、(\d+)节。/;
var regex2 = /星期(\S+): 第(\d+)节。/; //如果只有一节课的话
var matches = classTime.match(regex);
if(matches!=null){ //默认应该存在两节及以上的课,如果只有一节课则跳到else
// 创建包含解析结果的新对象
courseInfo["星期"] = parseInt(matches[1]);
courseInfo["开始节数"] = parseInt(matches[2]);
courseInfo["结束节数"] = parseInt(matches[3]);
}
else{
matches = classTime.match(regex2);
// 创建包含解析结果的新对象
courseInfo["星期"] = parseInt(matches[1]);
courseInfo["开始节数"] = parseInt(matches[2]);
courseInfo["结束节数"] = parseInt(matches[2]);
}
//console.log(courseInfo);
courseInfo["老师"] = "";
courseInfo["地点"] = courseInfoElement.querySelectorAll('td')[1+3*i].textContent.trim();
courseInfo["周数"] = courseInfoElement.querySelectorAll('td')[2+3*i].textContent.trim();
// // 打印解析结果
// console.log(courseInfo);
// 转为数组
var courseInfoArray = [courseInfo["课程名称"],
courseInfo["星期"],
courseInfo["开始节数"],
courseInfo["结束节数"],
courseInfo["老师"],
courseInfo["地点"],
courseInfo["周数"] ];
globalCourseInfoArray.push(courseInfoArray);
console.log(courseInfoArray);
}
return 1;
}
//main()
console.log("开始运行");
var urls = new Set();
var globalCourseInfoArray = []; //用数组来存储全局的数据
//插入表头
var tempArray = ["课程名称","星期","开始节数","结束节数","老师","地点","周数"];
globalCourseInfoArray.push(tempArray);
document.querySelectorAll('a[href^="/course/coursetime/"]').forEach(
function(link) {
urls.add('https://xkcts.ucas.ac.cn' + link.getAttribute('href'));
}
);
setTimeout(function() {
var promises = Array.from(urls).map(url =>
fetch(url)
.then(response => response.text())
.then(data => {
var tempDiv = document.createElement('div');
tempDiv.innerHTML = data;
var mcBody = tempDiv.querySelector('.mc-body');
GetData(mcBody);
return mcBody.outerHTML;
})
);
Promise.all(promises).then(
codes => {
var combinedCode = codes.join('');
var blob = new Blob([combinedCode], {type: "text/html"});
var url = URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = '课表.html';
document.body.appendChild(a);
a.click();
var csv = convertArrayToCSV(globalCourseInfoArray);
downloadCSV2(csv)
}
);
console.log("运行结束");
}, 5000);