dsh-ustc-campus 0.2.15 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -0
- package/README.md +105 -72
- package/dist/client.js +6037 -1061
- package/package.json +63 -60
- package/src/account-data.mjs +279 -0
- package/src/bb-actions.mjs +3 -1
- package/src/bb.mjs +94 -21
- package/src/campus-transit.mjs +58 -52
- package/src/client.mjs +74 -633
- package/src/concurrency.mjs +13 -0
- package/src/custom-feeds.mjs +3 -0
- package/src/exams.mjs +21 -7
- package/src/feed.mjs +10 -72
- package/src/grades.mjs +30 -16
- package/src/host.mjs +59 -179
- package/src/hs-library.mjs +41 -0
- package/src/library-booking.mjs +33 -5
- package/src/library-spaces.mjs +39 -35
- package/src/material.mjs +2320 -65
- package/src/mcp-host.mjs +18 -23
- package/src/mcp.mjs +4 -4
- package/src/recommender.mjs +64 -24
- package/src/school.mjs +218 -154
- package/src/snapshot-cache.mjs +98 -0
- package/src/state.mjs +67 -17
- package/src/week-layout.mjs +10 -1
- package/src/youth-actions.mjs +52 -58
- package/src/youth.mjs +68 -34
package/src/campus-transit.mjs
CHANGED
|
@@ -18,13 +18,13 @@
|
|
|
18
18
|
* - 中区 (中校区) has no standalone stop; queries map to nearest 东区/西区 hub stops with explicit guidance.
|
|
19
19
|
* - 国金院 is not available on the mobile timetable selector.
|
|
20
20
|
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* (day type, origin), which is what makes offline reads honest: the result
|
|
24
|
-
* says that it is cached and when it was fetched.
|
|
21
|
+
* Only fixed official URLs are read. The last good timetable for each
|
|
22
|
+
* (day type, origin) persists locally when the Host supplies a data directory.
|
|
25
23
|
*/
|
|
26
24
|
|
|
27
25
|
import { load } from 'cheerio';
|
|
26
|
+
import { join } from 'node:path';
|
|
27
|
+
import { createSnapshotCache } from './snapshot-cache.mjs';
|
|
28
28
|
|
|
29
29
|
import { errorText, fetchText, sanitizeText } from './public-fetch.mjs';
|
|
30
30
|
|
|
@@ -48,7 +48,7 @@ export const TRANSIT_SOURCE = Object.freeze({
|
|
|
48
48
|
});
|
|
49
49
|
|
|
50
50
|
const SOURCE_HOSTS = [TRANSIT_SOURCE.host];
|
|
51
|
-
const FRESH_MS =
|
|
51
|
+
const FRESH_MS = 24 * 60 * 60 * 1000;
|
|
52
52
|
const FETCH_TIMEOUT_MS = 15_000;
|
|
53
53
|
const MAX_BYTES = 512 * 1024;
|
|
54
54
|
const MAX_CACHE_ENTRIES = 16;
|
|
@@ -63,6 +63,27 @@ const SCHEDULE_DATE_PATTERN =
|
|
|
63
63
|
/(\d{4})\s*年\s*(\d{1,2})\s*月\s*(\d{1,2})\s*日\s*运行/;
|
|
64
64
|
const TIME_PATTERN = /^(\d{1,2}):(\d{2})(?:\s*(#))?$/;
|
|
65
65
|
|
|
66
|
+
// 国办发明电〔2025〕7号、〔2026〕10号;日期用通知正文,不推算农历。
|
|
67
|
+
const HOLIDAYS = {
|
|
68
|
+
'2026': [['01-01', '01-03', '元旦'], ['02-15', '02-23', '春节'], ['04-04', '04-06', '清明节'],
|
|
69
|
+
['05-01', '05-05', '劳动节'], ['06-19', '06-21', '端午节'], ['09-25', '09-27', '中秋节'], ['10-01', '10-07', '国庆节']],
|
|
70
|
+
'2027': [['01-01', '01-03', '元旦'], ['02-05', '02-13', '春节'], ['04-03', '04-05', '清明节'],
|
|
71
|
+
['05-01', '05-05', '劳动节'], ['06-09', '06-09', '端午节'], ['09-15', '09-15', '中秋节'], ['10-01', '10-07', '国庆节']]
|
|
72
|
+
};
|
|
73
|
+
const MAKEUP_WORKDAYS = new Set([
|
|
74
|
+
'2026-01-04', '2026-02-14', '2026-02-28', '2026-05-09', '2026-09-20', '2026-10-10',
|
|
75
|
+
'2027-01-31', '2027-02-14', '2027-05-08', '2027-09-26', '2027-10-09'
|
|
76
|
+
]);
|
|
77
|
+
|
|
78
|
+
function calendarDayType({ date, weekdayRuleDayType }) {
|
|
79
|
+
const monthDay = date.slice(5);
|
|
80
|
+
const holiday = HOLIDAYS[date.slice(0, 4)]?.find(([from, through]) => monthDay >= from && monthDay <= through);
|
|
81
|
+
const isVacationMonth = ['01', '02', '07', '08'].includes(date.slice(5, 7));
|
|
82
|
+
if (holiday) return { autoScheduleType: 'holiday', dayBadge: holiday[2], dayBasis: 'holiday_calendar', isVacationMonth };
|
|
83
|
+
if (MAKEUP_WORKDAYS.has(date)) return { autoScheduleType: 'workday', dayBadge: '调休班', dayBasis: 'makeup_workday', isVacationMonth };
|
|
84
|
+
return { autoScheduleType: weekdayRuleDayType, dayBadge: weekdayRuleDayType === 'holiday' ? '节假日' : '工作日',
|
|
85
|
+
dayBasis: 'weekend_rule', isVacationMonth };
|
|
86
|
+
}
|
|
66
87
|
const WEEKDAY_LABELS = Object.freeze([
|
|
67
88
|
'星期日',
|
|
68
89
|
'星期一',
|
|
@@ -691,16 +712,10 @@ export function createCampusTransit(options = {}) {
|
|
|
691
712
|
const opts = options && typeof options === 'object' ? options : {};
|
|
692
713
|
const fetchImpl = typeof opts.fetch === 'function' ? opts.fetch : globalThis.fetch;
|
|
693
714
|
const now = typeof opts.now === 'function' ? opts.now : Date.now;
|
|
694
|
-
const cache =
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
if (cache.size >= MAX_CACHE_ENTRIES && !cache.has(key)) {
|
|
699
|
-
const oldest = cache.keys().next().value;
|
|
700
|
-
if (oldest !== undefined) cache.delete(oldest);
|
|
701
|
-
}
|
|
702
|
-
cache.set(key, entry);
|
|
703
|
-
}
|
|
715
|
+
const cache = createSnapshotCache(opts.dir && join(opts.dir, 'transit-cache.json'), {
|
|
716
|
+
now: () => nowValue(now), limit: MAX_CACHE_ENTRIES, retryMs: FRESH_MS
|
|
717
|
+
});
|
|
718
|
+
const failures = new Map();
|
|
704
719
|
|
|
705
720
|
async function fetchSchedule(url, dayType, parser = 'campus') {
|
|
706
721
|
const html = await fetchText(url, {
|
|
@@ -749,29 +764,16 @@ export function createCampusTransit(options = {}) {
|
|
|
749
764
|
}
|
|
750
765
|
|
|
751
766
|
async function load(url, dayType, key, parser = 'campus') {
|
|
752
|
-
const cached = cache.get(key);
|
|
753
|
-
if (cached
|
|
754
|
-
|
|
767
|
+
const cached = cache.get(key)?.value;
|
|
768
|
+
if (cached) {
|
|
769
|
+
if (cache.due(key, FRESH_MS)) {
|
|
770
|
+
cache.refresh(key, () => fetchSchedule(url, dayType, parser)).then(() => failures.delete(key), error => failures.set(key, error));
|
|
771
|
+
}
|
|
772
|
+
return { entry: cached, cached: true, stale: nowValue(now) - cached.fetchedAt >= FRESH_MS, error: failures.get(key) ?? null };
|
|
755
773
|
}
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
const request = fetchSchedule(url, dayType, parser)
|
|
760
|
-
.then((entry) => {
|
|
761
|
-
remember(key, entry);
|
|
762
|
-
return { entry, cached: false, stale: false, error: null };
|
|
763
|
-
})
|
|
764
|
-
.catch((error) => {
|
|
765
|
-
if (cached) {
|
|
766
|
-
return { entry: cached, cached: true, stale: true, error };
|
|
767
|
-
}
|
|
768
|
-
throw error;
|
|
769
|
-
})
|
|
770
|
-
.finally(() => {
|
|
771
|
-
inflight.delete(key);
|
|
772
|
-
});
|
|
773
|
-
inflight.set(key, request);
|
|
774
|
-
return request;
|
|
774
|
+
// With no usable snapshot, report the actual upstream error to this caller.
|
|
775
|
+
const entry = await cache.refresh(key, () => fetchSchedule(url, dayType, parser), { force: true });
|
|
776
|
+
return { entry, cached: false, stale: false, error: null };
|
|
775
777
|
}
|
|
776
778
|
|
|
777
779
|
function dedupeGaoxinRoutes(routes) {
|
|
@@ -819,13 +821,9 @@ export function createCampusTransit(options = {}) {
|
|
|
819
821
|
if (stale) {
|
|
820
822
|
warnings.push({
|
|
821
823
|
code: 'stale_cache',
|
|
822
|
-
message:
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
if (explicit && entry.dayType !== weekdayRuleDayType) {
|
|
826
|
-
warnings.push({
|
|
827
|
-
code: 'explicit_overrides_weekday',
|
|
828
|
-
message: `指定 ${entry.dayType} 与按星期给出的 ${weekdayRuleDayType} 提示不同,已按指定类型返回`
|
|
824
|
+
message: error
|
|
825
|
+
? `无法刷新官方时刻表,显示 ${isoFromEpoch(entry.fetchedAt)} 读取的缓存:${errorText(error)}`
|
|
826
|
+
: '正在后台校对官方时刻表,暂显示上次成功缓存'
|
|
829
827
|
});
|
|
830
828
|
}
|
|
831
829
|
|
|
@@ -846,8 +844,8 @@ export function createCampusTransit(options = {}) {
|
|
|
846
844
|
source: sourceFromEntry(entry, basis),
|
|
847
845
|
cached,
|
|
848
846
|
stale,
|
|
849
|
-
errorCode:
|
|
850
|
-
error:
|
|
847
|
+
errorCode: error?.code ?? null,
|
|
848
|
+
error: error ? errorText(error) : null,
|
|
851
849
|
warnings,
|
|
852
850
|
...(transferNotice ? { transferNotice } : {})
|
|
853
851
|
};
|
|
@@ -1131,6 +1129,14 @@ export function createCampusTransit(options = {}) {
|
|
|
1131
1129
|
if (!parsedDate) {
|
|
1132
1130
|
return invalid('invalid_date', '日期必须是有效的 YYYY-MM-DD 公历日期');
|
|
1133
1131
|
}
|
|
1132
|
+
const day = calendarDayType(parsedDate);
|
|
1133
|
+
const requested = scheduleTypeFrom(query?.scheduleType ?? query?.dayType);
|
|
1134
|
+
const manual = requested != null && requested !== day.autoScheduleType;
|
|
1135
|
+
const out = await readDate(query, parsedDate, requested, day.autoScheduleType);
|
|
1136
|
+
return { ...out, ...day, ...(manual ? { dayBasis: 'manual_override', dayBadge: requested === 'holiday' ? '节假日' : '工作日' } : {}) };
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
async function readDate(query, parsedDate, requested, autoScheduleType) {
|
|
1134
1140
|
const { date, weekday, weekdayLabel, weekdayRuleDayType } = parsedDate;
|
|
1135
1141
|
const weekdayHint = {
|
|
1136
1142
|
dayType: weekdayRuleDayType,
|
|
@@ -1165,7 +1171,7 @@ export function createCampusTransit(options = {}) {
|
|
|
1165
1171
|
toCampus
|
|
1166
1172
|
});
|
|
1167
1173
|
}
|
|
1168
|
-
const
|
|
1174
|
+
const selected = requested ?? (query.autoResolve === true ? autoScheduleType : null);
|
|
1169
1175
|
if (requested === undefined) {
|
|
1170
1176
|
return invalid(
|
|
1171
1177
|
'invalid_schedule_type',
|
|
@@ -1189,21 +1195,21 @@ export function createCampusTransit(options = {}) {
|
|
|
1189
1195
|
date,
|
|
1190
1196
|
weekday,
|
|
1191
1197
|
weekdayLabel,
|
|
1192
|
-
dayType:
|
|
1198
|
+
dayType: selected,
|
|
1193
1199
|
weekdayRuleDayType,
|
|
1194
1200
|
weekdayHint,
|
|
1195
|
-
scheduleTypeBasis: requested ? 'explicit' : 'unspecified',
|
|
1201
|
+
scheduleTypeBasis: requested ? 'explicit' : selected ? 'class' : 'unspecified',
|
|
1196
1202
|
fromCampus,
|
|
1197
1203
|
toCampus,
|
|
1198
|
-
source: emptySource(TRANSIT_SOURCE.baseUrl,
|
|
1204
|
+
source: emptySource(TRANSIT_SOURCE.baseUrl, selected, requested ? 'explicit' : selected ? 'class' : 'unspecified'),
|
|
1199
1205
|
reason: 'adjacent_campus_walk',
|
|
1200
1206
|
transferNotice: strategy.transferNotice,
|
|
1201
1207
|
message: '中校区与东区/西区/北区相邻,可步行经通道直达,无需校车。'
|
|
1202
1208
|
});
|
|
1203
1209
|
}
|
|
1204
1210
|
|
|
1205
|
-
if (
|
|
1206
|
-
return evaluateStrategy(strategy,
|
|
1211
|
+
if (selected) {
|
|
1212
|
+
return evaluateStrategy(strategy, selected, parsedDate, fromCampus, toCampus, Boolean(requested));
|
|
1207
1213
|
}
|
|
1208
1214
|
|
|
1209
1215
|
const dayTypes = ['workday', 'holiday'];
|