-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTHUAuditDataCrawler.ts
More file actions
91 lines (81 loc) · 2.97 KB
/
Copy pathTHUAuditDataCrawler.ts
File metadata and controls
91 lines (81 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// ==UserScript==
// @name 東海課程資訊網資料爬蟲
// @name:zh-tw 東海課程資訊網資料爬蟲
// @namespace com.sherryyue.THUAuditDataCrawler
// @version 0.1
// @description 東海課程資訊網資料爬蟲
// @description:zh-tw 東海課程資訊網資料爬蟲
// @author SherryYue
// @copyright SherryYue
// @license MIT
// @match *://course.thu.edu.tw/*
// @contributionURL https://sherryyuechiu.github.io/card
// @supportURL sherryyue.c@protonmail.com
// @icon https://sherryyuechiu.github.io/card/images/logo/maskable_icon_x96.png
// @supportURL "https://github.com/sherryyuechiu/GreasyMonkeyScripts/issues"
// @homepage "https://github.com/sherryyuechiu/GreasyMonkeyScripts"
// @grant none
// ==/UserScript==
(function () {
const crawler = (): void => {
const classBlock = document.querySelectorAll<HTMLTableRowElement>("#no-more-tables>tbody>tr");
const classCnt = classBlock.length;
let json = '';
for (let i = 0; i < classCnt; i++) {
let ccode = '',
cname = '',
croom: string[] = [],
ctime: string[] = [];
// 選課代碼
ccode = classBlock[i].querySelector<HTMLElement>('[data-title=選課代碼]')?.textContent?.trim() || '';
// 課程名稱
const courseNameElement = classBlock[i].querySelector<HTMLElement>('[data-title=課程名稱]');
if (courseNameElement?.innerHTML.includes("strike")) continue;
else {
cname = courseNameElement?.textContent?.trim() || '';
}
// 地點
const roomText = classBlock[i].querySelector<HTMLElement>("[data-title=時間地點]")?.textContent
?.replace('無資料', '')
.trim()
.match(/\[([^\[\]]*)\]/g);
if (roomText) {
// 去除[ ]
croom = roomText.map(str => str.slice(1, -1));
} else {
console.warn(`${ccode}地點格式異常 ${roomText}`);
}
// 時間
let timeRaw = classBlock[i].querySelector<HTMLElement>("[data-title=時間地點]")?.textContent
?.replace('無資料', '')
.trim() || '';
// 從原始資料去除地點資訊
roomText?.forEach(r => {
timeRaw = timeRaw.replace(r, '');
});
timeRaw = timeRaw.replaceAll('星期', '');
ctime = timeRaw.split(/[\s,\/]/);
if (!ctime) {
console.warn('時間格式異常', ctime);
}
// 時間目標格式: ["三", "6", "7", "五", "6", "7"]
json += `{
"code": "${ccode || ''}",
"name": "${cname || ''}",
"room": ["${croom.join("\",\"") || ''}"],
"time": ["${ctime.join("\",\"") || ''}"]
},`;
}
console.log(json);
}
const observer = new MutationObserver((mutations, obs) => {
if (document.querySelector("#no-more-tables>tbody>tr")) {
crawler();
observer.disconnect();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
})();