dsh-ustc-campus 0.2.14 → 0.2.15

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/src/feed.mjs CHANGED
@@ -7,6 +7,22 @@ import {
7
7
  sanitizeText,
8
8
  shanghaiEpoch
9
9
  } from './public-fetch.mjs';
10
+ import { createRecommender } from './recommender.mjs';
11
+
12
+ export const FEED_CATEGORY_LABELS = Object.freeze({
13
+ campus: '学校要闻与综合公告',
14
+ academic: '本科教务与考试',
15
+ research: '科研进展与成果',
16
+ student: '学工资助与学生事务',
17
+ activity: '团学文化与第二课堂',
18
+ lecture: '学术讲座与图书馆培训',
19
+ tech: '开源社区与技术活动',
20
+ service: '校园网络、医疗、财务与安保',
21
+ exchange: '国际交流与校际访学',
22
+ college: '学院通知与学部动态'
23
+ });
24
+
25
+ export const FEED_CATEGORIES = Object.freeze(Object.keys(FEED_CATEGORY_LABELS));
10
26
 
11
27
  /**
12
28
  * Fixed public sources. No caller-supplied URL is ever fetched.
@@ -20,6 +36,7 @@ export const FEED_SOURCES = Object.freeze({
20
36
  id: 'news',
21
37
  label: '中国科大新闻网',
22
38
  kind: 'news',
39
+ category: 'campus',
23
40
  parser: 'news',
24
41
  url: 'https://news.ustc.edu.cn/xwbl.htm',
25
42
  origin: 'https://news.ustc.edu.cn/',
@@ -29,6 +46,7 @@ export const FEED_SOURCES = Object.freeze({
29
46
  id: 'teach',
30
47
  label: '教务处教学通知',
31
48
  kind: 'notice',
49
+ category: 'academic',
32
50
  parser: 'list',
33
51
  url: 'https://www.teach.ustc.edu.cn/category/notice/notice-teaching',
34
52
  origin: 'https://www.teach.ustc.edu.cn/',
@@ -38,6 +56,7 @@ export const FEED_SOURCES = Object.freeze({
38
56
  id: 'teach-exam',
39
57
  label: '教务处考试通知',
40
58
  kind: 'notice',
59
+ category: 'academic',
41
60
  parser: 'list',
42
61
  url: 'https://www.teach.ustc.edu.cn/category/notice/notice-exam',
43
62
  origin: 'https://www.teach.ustc.edu.cn/',
@@ -47,6 +66,7 @@ export const FEED_SOURCES = Object.freeze({
47
66
  id: 'ustc',
48
67
  label: '科大通知公告',
49
68
  kind: 'notice',
69
+ category: 'campus',
50
70
  parser: 'ustc-notice',
51
71
  url: 'https://www.ustc.edu.cn/tzgg.htm',
52
72
  origin: 'https://www.ustc.edu.cn/',
@@ -56,6 +76,7 @@ export const FEED_SOURCES = Object.freeze({
56
76
  id: 'youth',
57
77
  label: '校团委通知公告',
58
78
  kind: 'notice',
79
+ category: 'activity',
59
80
  parser: 'list',
60
81
  url: 'https://young.ustc.edu.cn/15063/list.htm',
61
82
  origin: 'https://young.ustc.edu.cn/',
@@ -65,6 +86,7 @@ export const FEED_SOURCES = Object.freeze({
65
86
  id: 'stuhome',
66
87
  label: '学工动态',
67
88
  kind: 'news',
89
+ category: 'student',
68
90
  parser: 'list',
69
91
  url: 'https://stuhome.ustc.edu.cn/2315/list.htm',
70
92
  origin: 'https://stuhome.ustc.edu.cn/',
@@ -74,6 +96,7 @@ export const FEED_SOURCES = Object.freeze({
74
96
  id: 'library',
75
97
  label: '图书馆服务公告',
76
98
  kind: 'news',
99
+ category: 'service',
77
100
  parser: 'list',
78
101
  url: 'https://lib.ustc.edu.cn/category/%E6%9C%8D%E5%8A%A1%E5%85%AC%E5%91%8A/',
79
102
  origin: 'https://lib.ustc.edu.cn/',
@@ -83,6 +106,7 @@ export const FEED_SOURCES = Object.freeze({
83
106
  id: 'library-lecture',
84
107
  label: '图书馆讲座培训',
85
108
  kind: 'event',
109
+ category: 'lecture',
86
110
  parser: 'library-lecture',
87
111
  url: 'https://lib.ustc.edu.cn/category/%E8%AE%B2%E5%BA%A7%E5%9F%B9%E8%AE%AD/',
88
112
  origin: 'https://lib.ustc.edu.cn/',
@@ -92,39 +116,213 @@ export const FEED_SOURCES = Object.freeze({
92
116
  id: 'lug',
93
117
  label: 'USTC LUG 活动',
94
118
  kind: 'event',
119
+ category: 'tech',
95
120
  parser: 'lug',
96
121
  url: 'https://lug.ustc.edu.cn/wiki/lug/events/',
97
122
  origin: 'https://lug.ustc.edu.cn/',
98
123
  hosts: Object.freeze(['lug.ustc.edu.cn'])
124
+ }),
125
+ 'news-research': Object.freeze({
126
+ id: 'news-research',
127
+ label: '中国科大科研进展',
128
+ kind: 'news',
129
+ category: 'research',
130
+ parser: 'news',
131
+ url: 'https://news.ustc.edu.cn/kyjz.htm',
132
+ origin: 'https://news.ustc.edu.cn/',
133
+ hosts: Object.freeze(['news.ustc.edu.cn'])
134
+ }),
135
+ 'teach-exchange': Object.freeze({
136
+ id: 'teach-exchange',
137
+ label: '教务处交流交换',
138
+ kind: 'notice',
139
+ category: 'exchange',
140
+ parser: 'list',
141
+ url: 'https://www.teach.ustc.edu.cn/category/notice/notice-exchange',
142
+ origin: 'https://www.teach.ustc.edu.cn/',
143
+ hosts: Object.freeze(['www.teach.ustc.edu.cn', 'teach.ustc.edu.cn'])
144
+ }),
145
+ 'teach-info': Object.freeze({
146
+ id: 'teach-info',
147
+ label: '教务处信息通知',
148
+ kind: 'notice',
149
+ category: 'academic',
150
+ parser: 'list',
151
+ url: 'https://www.teach.ustc.edu.cn/category/notice/notice-info',
152
+ origin: 'https://www.teach.ustc.edu.cn/',
153
+ hosts: Object.freeze(['www.teach.ustc.edu.cn', 'teach.ustc.edu.cn'])
154
+ }),
155
+ 'stuhome-notice': Object.freeze({
156
+ id: 'stuhome-notice',
157
+ label: '学工部最新通知',
158
+ kind: 'notice',
159
+ category: 'student',
160
+ parser: 'list',
161
+ url: 'https://stuhome.ustc.edu.cn/2316/list.htm',
162
+ origin: 'https://stuhome.ustc.edu.cn/',
163
+ hosts: Object.freeze(['stuhome.ustc.edu.cn'])
164
+ }),
165
+ 'youth-news': Object.freeze({
166
+ id: 'youth-news',
167
+ label: '校团委校园要闻',
168
+ kind: 'news',
169
+ category: 'activity',
170
+ parser: 'list',
171
+ url: 'https://young.ustc.edu.cn/15062/list.htm',
172
+ origin: 'https://young.ustc.edu.cn/',
173
+ hosts: Object.freeze(['young.ustc.edu.cn'])
174
+ }),
175
+ ustcnet: Object.freeze({
176
+ id: 'ustcnet',
177
+ label: '网络信息中心公告',
178
+ kind: 'notice',
179
+ category: 'service',
180
+ parser: 'list',
181
+ url: 'https://ustcnet.ustc.edu.cn/33415/list.htm',
182
+ origin: 'https://ustcnet.ustc.edu.cn/',
183
+ hosts: Object.freeze(['ustcnet.ustc.edu.cn'])
184
+ }),
185
+ bwc: Object.freeze({
186
+ id: 'bwc',
187
+ label: '保卫与校园管理处公告',
188
+ kind: 'notice',
189
+ category: 'service',
190
+ parser: 'list',
191
+ url: 'https://bwc.ustc.edu.cn/5668/list.htm',
192
+ origin: 'https://bwc.ustc.edu.cn/',
193
+ hosts: Object.freeze(['bwc.ustc.edu.cn'])
194
+ }),
195
+ hospital: Object.freeze({
196
+ id: 'hospital',
197
+ label: '校医院信息公告',
198
+ kind: 'notice',
199
+ category: 'service',
200
+ parser: 'list',
201
+ url: 'https://hospital.ustc.edu.cn/xxgg/list.htm',
202
+ origin: 'https://hospital.ustc.edu.cn/',
203
+ hosts: Object.freeze(['hospital.ustc.edu.cn'])
204
+ }),
205
+ finance: Object.freeze({
206
+ id: 'finance',
207
+ label: '财务处通知公告',
208
+ kind: 'notice',
209
+ category: 'service',
210
+ parser: 'list',
211
+ url: 'https://finance.ustc.edu.cn/tzgg/list.htm',
212
+ origin: 'https://finance.ustc.edu.cn/',
213
+ hosts: Object.freeze(['finance.ustc.edu.cn'])
214
+ }),
215
+ oic: Object.freeze({
216
+ id: 'oic',
217
+ label: '国际合作与交流部通知',
218
+ kind: 'notice',
219
+ category: 'exchange',
220
+ parser: 'list',
221
+ url: 'https://oic.ustc.edu.cn/news-category/information/',
222
+ origin: 'https://oic.ustc.edu.cn/',
223
+ hosts: Object.freeze(['oic.ustc.edu.cn'])
224
+ }),
225
+ 'lug-news': Object.freeze({
226
+ id: 'lug-news',
227
+ label: 'USTC LUG 新闻与预告',
228
+ kind: 'news',
229
+ category: 'tech',
230
+ parser: 'list',
231
+ url: 'https://lug.ustc.edu.cn/news/',
232
+ origin: 'https://lug.ustc.edu.cn/',
233
+ hosts: Object.freeze(['lug.ustc.edu.cn'])
234
+ }),
235
+ sgy: Object.freeze({
236
+ id: 'sgy',
237
+ label: '少年班学院通知公告',
238
+ kind: 'notice',
239
+ category: 'college',
240
+ collegeTags: Object.freeze(['少院', '少年班']),
241
+ parser: 'sgy',
242
+ url: 'https://sgy.ustc.edu.cn/37821/list.htm',
243
+ origin: 'https://sgy.ustc.edu.cn/',
244
+ hosts: Object.freeze(['sgy.ustc.edu.cn'])
245
+ }),
246
+ saids: Object.freeze({
247
+ id: 'saids',
248
+ label: '人工智能与数据科学学院公告',
249
+ kind: 'notice',
250
+ category: 'college',
251
+ collegeTags: Object.freeze(['人工智能', '数据科学', 'AI', '机器学习', '深度学习']),
252
+ parser: 'list',
253
+ url: 'https://saids.ustc.edu.cn/15435/list.htm',
254
+ origin: 'https://saids.ustc.edu.cn/',
255
+ hosts: Object.freeze(['saids.ustc.edu.cn'])
256
+ }),
257
+ cybersec: Object.freeze({
258
+ id: 'cybersec',
259
+ label: '网络空间安全学院公告',
260
+ kind: 'notice',
261
+ category: 'college',
262
+ collegeTags: Object.freeze(['网安', '网络安全', '信息安全', '密码']),
263
+ parser: 'list',
264
+ url: 'https://cybersec.ustc.edu.cn/tzgg_23826/list.htm',
265
+ origin: 'https://cybersec.ustc.edu.cn/',
266
+ hosts: Object.freeze(['cybersec.ustc.edu.cn'])
267
+ }),
268
+ 'cs-dynamic': Object.freeze({
269
+ id: 'cs-dynamic',
270
+ label: '计算机学院动态公告',
271
+ kind: 'notice',
272
+ category: 'college',
273
+ collegeTags: Object.freeze(['计算机', '软件', '系统', '算法', '编译', '操作系统']),
274
+ parser: 'list',
275
+ url: 'https://cs.ustc.edu.cn/20159/list.htm',
276
+ origin: 'https://cs.ustc.edu.cn/',
277
+ hosts: Object.freeze(['cs.ustc.edu.cn'])
278
+ }),
279
+ sist: Object.freeze({
280
+ id: 'sist',
281
+ label: '信息科学技术学院通知公告',
282
+ kind: 'notice',
283
+ category: 'college',
284
+ collegeTags: Object.freeze(['信息', '电子', '通信', '信号', '自动化', '电路', '微电子']),
285
+ parser: 'list',
286
+ url: 'https://sist.ustc.edu.cn/5142/list.htm',
287
+ origin: 'https://sist.ustc.edu.cn/',
288
+ hosts: Object.freeze(['sist.ustc.edu.cn'])
99
289
  })
100
290
  });
101
291
 
292
+ const TEACH_SELECTORS = Object.freeze({
293
+ rows: 'ul.article-list > li',
294
+ link: 'span.post a[href]',
295
+ title: 'span.post a',
296
+ date: 'span.date'
297
+ });
298
+
299
+ const STUHOME_SELECTORS = Object.freeze({
300
+ rows: 'ul.news_list > li',
301
+ link: 'span.news_title a[href]',
302
+ title: 'span.news_title a',
303
+ date: 'span.news_meta'
304
+ });
305
+
306
+ const YOUTH_SELECTORS = Object.freeze({
307
+ rows: 'ul.text-ul > li',
308
+ link: 'a[href][title]',
309
+ title: 'a[href][title]',
310
+ date: 'i'
311
+ });
312
+
102
313
  /** Explicit DOM selectors for the shared list parser. */
103
314
  const SOURCE_SELECTORS = Object.freeze({
104
- teach: Object.freeze({
105
- rows: 'ul.article-list > li',
106
- link: 'span.post a[href]',
107
- title: 'span.post a',
108
- date: 'span.date'
109
- }),
110
- 'teach-exam': Object.freeze({
111
- rows: 'ul.article-list > li',
112
- link: 'span.post a[href]',
113
- title: 'span.post a',
114
- date: 'span.date'
115
- }),
116
- stuhome: Object.freeze({
117
- rows: 'ul.news_list > li',
118
- link: 'span.news_title a[href]',
119
- title: 'span.news_title a',
120
- date: 'span.news_meta'
121
- }),
122
- youth: Object.freeze({
123
- rows: 'ul.text-ul > li',
124
- link: 'a[href][title]',
125
- title: 'a[href][title]',
126
- date: 'i'
127
- }),
315
+ teach: TEACH_SELECTORS,
316
+ 'teach-exam': TEACH_SELECTORS,
317
+ 'teach-exchange': TEACH_SELECTORS,
318
+ 'teach-info': TEACH_SELECTORS,
319
+ stuhome: STUHOME_SELECTORS,
320
+ 'stuhome-notice': STUHOME_SELECTORS,
321
+ ustcnet: STUHOME_SELECTORS,
322
+ finance: STUHOME_SELECTORS,
323
+ 'cs-dynamic': STUHOME_SELECTORS,
324
+ youth: YOUTH_SELECTORS,
325
+ 'youth-news': YOUTH_SELECTORS,
128
326
  library: Object.freeze({
129
327
  rows: 'ul.lists > li',
130
328
  link: 'a.item[href]',
@@ -136,6 +334,50 @@ const SOURCE_SELECTORS = Object.freeze({
136
334
  link: 'a.item[href]',
137
335
  title: 'p',
138
336
  date: 'span.date'
337
+ }),
338
+ bwc: Object.freeze({
339
+ rows: 'div.jzlb',
340
+ link: 'a[href]',
341
+ title: 'a[href]',
342
+ date: 'div.sj, span, .time'
343
+ }),
344
+ hospital: Object.freeze({
345
+ rows: 'ul.list > li',
346
+ link: 'a[href]',
347
+ title: 'a[href]',
348
+ date: 'span, .time, .date'
349
+ }),
350
+ oic: Object.freeze({
351
+ rows: 'article',
352
+ link: 'a[href]',
353
+ title: 'h2, h3, a[href]',
354
+ date: 'time, .date, .meta',
355
+ preferTitleNode: true
356
+ }),
357
+ 'lug-news': Object.freeze({
358
+ rows: 'article.archive__item',
359
+ link: 'h2 a[href]',
360
+ title: 'h2 a',
361
+ date: 'time, p.page__meta'
362
+ }),
363
+ saids: Object.freeze({
364
+ rows: 'ul.wp_article_list > li',
365
+ link: 'a[href]',
366
+ title: 'a[href]',
367
+ date: 'span.Article_PublishDate, span.news_meta'
368
+ }),
369
+ cybersec: Object.freeze({
370
+ rows: 'li.cle',
371
+ link: 'a[href]',
372
+ title: 'div.text',
373
+ date: 'div.time',
374
+ preferTitleNode: true
375
+ }),
376
+ sist: Object.freeze({
377
+ rows: 'div.card',
378
+ link: 'h5 a[href]',
379
+ title: 'h5 a[href]',
380
+ date: '.card-footer, .text-muted, small, span'
139
381
  })
140
382
  });
141
383
 
@@ -406,7 +648,7 @@ function loadCache(read) {
406
648
  return cache;
407
649
  }
408
650
 
409
- function parseNewsHtml(html) {
651
+ function parseNewsHtml(html, source = FEED_SOURCES.news) {
410
652
  const $ = load(String(html ?? ''));
411
653
  let rows = $('ul.info-list').first().children('li').toArray();
412
654
  if (rows.length === 0) rows = $('ul.info-list li').toArray();
@@ -423,18 +665,18 @@ function parseNewsHtml(html) {
423
665
  if (!title) continue;
424
666
 
425
667
  const href = String(link.attr('href') ?? '').trim();
426
- const url = href.startsWith('#') ? null : sourceItemUrl(href, FEED_SOURCES.news);
668
+ const url = href.startsWith('#') ? null : sourceItemUrl(href, source);
427
669
  const rawDate = element.find('.date').first().text();
428
670
  const dateInfo = datePartsFromText(rawDate);
429
671
  const date = dateInfo?.date ?? (sanitizeText(rawDate, 40) || null);
430
672
 
431
- const id = makeItemId('news', url, `${date ?? ''}|${title}`);
673
+ const id = makeItemId(source.id, url, `${date ?? ''}|${title}`);
432
674
  if (seen.has(id)) continue;
433
675
  seen.add(id);
434
676
 
435
677
  items.push({
436
678
  id,
437
- source: 'news',
679
+ source: source.id,
438
680
  title,
439
681
  url,
440
682
  date,
@@ -545,14 +787,78 @@ function parseNoticeList(html, source, nowMs) {
545
787
  if (link.length === 0) continue;
546
788
 
547
789
  const titleNode = selectors.title ? element.find(selectors.title).first() : link;
548
- const title = sanitizeText(
549
- link.attr('title') || titleNode.text() || link.text(),
550
- 500
551
- );
790
+ const primaryTitle = selectors.preferTitleNode
791
+ ? (titleNode.attr('title') || titleNode.text() || link.attr('title') || link.text())
792
+ : (link.attr('title') || titleNode.text() || link.text());
793
+ const title = sanitizeText(primaryTitle, 500);
552
794
  if (!title) continue;
553
795
 
554
- const dateRaw = sanitizeText(element.find(selectors.date).first().text(), 80);
555
- const dateInfo = datePartsFromText(dateRaw) ?? monthDayDate(dateRaw, nowMs);
796
+ let dateInfo = null;
797
+ for (const dateEl of element.find(selectors.date).toArray()) {
798
+ const dateRaw = sanitizeText($(dateEl).text(), 80);
799
+ dateInfo = datePartsFromText(dateRaw) ?? monthDayDate(dateRaw, nowMs);
800
+ if (dateInfo) break;
801
+ }
802
+ if (!dateInfo) {
803
+ const fallbackRaw = sanitizeText(element.text(), 240);
804
+ dateInfo = datePartsFromText(fallbackRaw);
805
+ }
806
+ if (!dateInfo) continue;
807
+
808
+ const href = String(link.attr('href') ?? '').trim();
809
+ const url = href && !href.startsWith('#') ? sourceItemUrl(href, source) : null;
810
+
811
+ const id = makeItemId(source.id, url, `${dateInfo.date}|${title}`);
812
+ if (seen.has(id)) continue;
813
+ seen.add(id);
814
+
815
+ items.push({
816
+ id,
817
+ source: source.id,
818
+ title,
819
+ url,
820
+ date: dateInfo.date,
821
+ time: null,
822
+ location: null,
823
+ start: null,
824
+ end: null,
825
+ secondClassroom: false,
826
+ reason: '',
827
+ past: false
828
+ });
829
+ if (items.length >= MAX_ITEMS_PER_SOURCE) break;
830
+ }
831
+ return items;
832
+ }
833
+
834
+ function parseSgyNoticeHtml(html, source) {
835
+ const $ = load(String(html ?? ''));
836
+ const rows = $('ul.notice_list > li').toArray();
837
+ const items = [];
838
+ const seen = new Set();
839
+
840
+ for (const row of rows) {
841
+ const element = $(row);
842
+ const link = element.find('a[href]').first();
843
+ if (link.length === 0) continue;
844
+
845
+ const titleNode = element.find('h2.ellipsis3, h2, .notice_list_font').first();
846
+ const title = sanitizeText(link.attr('title') || titleNode.text() || link.text(), 500);
847
+ if (!title) continue;
848
+
849
+ const dateParts = element
850
+ .find('div.notice_list_date p')
851
+ .toArray()
852
+ .map((node) => sanitizeText($(node).text(), 30))
853
+ .filter(Boolean);
854
+ let dateInfo = null;
855
+ if (dateParts.length >= 2) {
856
+ const combined = `${dateParts[1]}/${dateParts[0]}`;
857
+ dateInfo = datePartsFromText(combined);
858
+ }
859
+ if (!dateInfo) {
860
+ dateInfo = datePartsFromText(sanitizeText(element.text(), 120));
861
+ }
556
862
  if (!dateInfo) continue;
557
863
 
558
864
  const href = String(link.attr('href') ?? '').trim();
@@ -736,11 +1042,13 @@ async function parseLibraryLectures(html, source, nowMs, context) {
736
1042
  async function parseSourceHtml(html, source, nowMs, context) {
737
1043
  switch (source.parser) {
738
1044
  case 'news':
739
- return parseNewsHtml(html);
1045
+ return parseNewsHtml(html, source);
740
1046
  case 'lug':
741
1047
  return parseLugHtml(html);
742
1048
  case 'ustc-notice':
743
1049
  return parseUstcNotices(html, source, nowMs);
1050
+ case 'sgy':
1051
+ return parseSgyNoticeHtml(html, source);
744
1052
  case 'list':
745
1053
  return parseNoticeList(html, source, nowMs);
746
1054
  case 'library-lecture':
@@ -750,17 +1058,25 @@ async function parseSourceHtml(html, source, nowMs, context) {
750
1058
  }
751
1059
  }
752
1060
 
753
- function normalizeSourceFilter(source) {
754
- if (source == null || source === '' || source === 'all') return [...SOURCE_IDS];
755
- const values = (Array.isArray(source) ? source : [source])
756
- .flatMap((value) => String(value).split(','))
757
- .map((value) => value.trim().toLowerCase())
758
- .filter(Boolean);
759
- const selected = [];
760
- for (const id of SOURCE_IDS) {
761
- if (values.includes(id)) selected.push(id);
1061
+ function normalizeSourceFilter(source, category) {
1062
+ let ids;
1063
+ if (source == null || source === '' || source === 'all') {
1064
+ ids = [...SOURCE_IDS];
1065
+ } else {
1066
+ const values = (Array.isArray(source) ? source : [source])
1067
+ .flatMap((value) => String(value).split(','))
1068
+ .map((value) => value.trim().toLowerCase())
1069
+ .filter(Boolean);
1070
+ ids = SOURCE_IDS.filter((id) => values.includes(id));
1071
+ }
1072
+ if (category != null && category !== '' && category !== 'all') {
1073
+ const cats = (Array.isArray(category) ? category : [category])
1074
+ .flatMap((v) => String(v).split(','))
1075
+ .map((v) => v.trim().toLowerCase())
1076
+ .filter(Boolean);
1077
+ ids = ids.filter((id) => cats.includes(FEED_SOURCES[id].category));
762
1078
  }
763
- return selected;
1079
+ return ids;
764
1080
  }
765
1081
 
766
1082
  function normalizeInterests(interests) {
@@ -855,12 +1171,42 @@ function itemDateEpoch(item) {
855
1171
  return item.start ?? dayEpoch(item.date);
856
1172
  }
857
1173
 
858
- function sourceKind(sourceId) {
859
- return FEED_SOURCES[sourceId]?.kind ?? 'news';
1174
+ const ACTIVITY_CUE_RE = /(?:讲座|学术报告|报告会|论坛|沙龙|宣讲|宣介会|培训|工作坊|研讨会|大赛|竞赛|挑战杯|比武|比赛|运动会|音乐会|晚会|演出|放映会|观影|桌游|小聚|招新|招募|夏令营|寒期学校|暑期学校|打卡|马拉松|体验课|开放日|征集)/;
1175
+ const POST_SUMMARY_RE = /(?:圆满举办|顺利召开|取得佳绩|获奖名单|总结|回顾|初评结果|拟推荐|名单公示|公示$)/;
1176
+ const REG_CUE_RE = /(?:报名|申报|预约|招募|征集|参赛|抢票)/;
1177
+
1178
+ /**
1179
+ * Separate actionable activities (events, second-classroom activities, lectures,
1180
+ * competitions, club meetups) from general notices and campus news.
1181
+ */
1182
+ export function classifyItemIntent(item, sourceMeta = null) {
1183
+ const title = typeof item?.title === 'string' ? item.title : '';
1184
+ let intent;
1185
+ if (item?.intent === 'activity' || item?.intent === 'notice') {
1186
+ intent = item.intent;
1187
+ } else if (sourceMeta?.kind === 'event' || item?.source === 'youth-enrolling') {
1188
+ intent = 'activity';
1189
+ } else if (ACTIVITY_CUE_RE.test(title) && !POST_SUMMARY_RE.test(title)) {
1190
+ intent = 'activity';
1191
+ } else {
1192
+ intent = 'notice';
1193
+ }
1194
+
1195
+ const requiresRegistration = item?.requiresRegistration === true
1196
+ || item?.source === 'youth-enrolling'
1197
+ || (intent === 'activity' && REG_CUE_RE.test(title));
1198
+
1199
+ return { intent, requiresRegistration };
1200
+ }
1201
+
1202
+ function itemKindOf(item) {
1203
+ if (item?.source && FEED_SOURCES[item.source]) return FEED_SOURCES[item.source].kind;
1204
+ if (item?.intent === 'activity' || item?.start != null) return 'event';
1205
+ return 'news';
860
1206
  }
861
1207
 
862
1208
  function computePast(item, nowMs) {
863
- if (sourceKind(item.source) !== 'event') return false;
1209
+ if (itemKindOf(item) !== 'event') return false;
864
1210
  if (item.end != null) return item.end < nowMs;
865
1211
  if (item.start != null) return item.start < nowMs;
866
1212
  const day = dayEpoch(item.date);
@@ -869,7 +1215,7 @@ function computePast(item, nowMs) {
869
1215
  }
870
1216
 
871
1217
  function chronologicalRank(item) {
872
- if (sourceKind(item.source) !== 'event') {
1218
+ if (itemKindOf(item) !== 'event') {
873
1219
  return item.date && dayEpoch(item.date) != null ? 2 : 4;
874
1220
  }
875
1221
  if (item.start != null) return item.past ? 3 : 0;
@@ -902,7 +1248,7 @@ function buildReason(item, interests, agenda, coverage, nowMs) {
902
1248
  const reasons = [];
903
1249
  if (hits.length > 0) reasons.push(`兴趣匹配:${hits.slice(0, 3).join('、')}`);
904
1250
 
905
- const kind = sourceKind(item.source);
1251
+ const kind = itemKindOf(item);
906
1252
  if (kind === 'notice') {
907
1253
  reasons.push('学校通知');
908
1254
  } else if (kind === 'news') {
@@ -934,7 +1280,7 @@ function scoreItem(item, interests, agenda, nowMs) {
934
1280
  if (haystack.includes(term.toLowerCase())) score += 30;
935
1281
  }
936
1282
 
937
- if (sourceKind(item.source) === 'event') {
1283
+ if (itemKindOf(item) === 'event') {
938
1284
  if (item.past) {
939
1285
  score -= 100;
940
1286
  } else if (item.start != null) {
@@ -966,22 +1312,34 @@ function sortItems(items, sort, interests, agenda, nowMs) {
966
1312
  return [...items].sort(compareChronological);
967
1313
  }
968
1314
 
969
- function publicItem(item, interests, agenda, nowMs, coverage) {
1315
+ function publicItem(item, interests, agenda, nowMs, coverage, sourceMeta = null) {
970
1316
  const past = computePast(item, nowMs);
971
- return {
1317
+ const meta = sourceMeta ?? FEED_SOURCES[item.source] ?? null;
1318
+ const { intent, requiresRegistration } = classifyItemIntent(item, meta);
1319
+ const projected = {
972
1320
  id: item.id,
973
1321
  source: item.source,
1322
+ category: item.category ?? meta?.category ?? 'campus',
1323
+ intent,
1324
+ requiresRegistration,
974
1325
  title: item.title,
975
1326
  url: item.url,
976
- date: item.date,
977
- time: item.time,
978
- location: item.location,
979
- start: item.start,
980
- end: item.end,
1327
+ date: item.date ?? null,
1328
+ time: item.time ?? null,
1329
+ location: item.location ?? null,
1330
+ start: item.start ?? null,
1331
+ end: item.end ?? null,
981
1332
  secondClassroom: item.secondClassroom === true,
982
1333
  reason: buildReason({ ...item, past }, interests, agenda, coverage, nowMs),
983
1334
  past
984
1335
  };
1336
+ if (item.organizer) projected.organizer = item.organizer;
1337
+ if (item.enrollmentRatio) projected.enrollmentRatio = item.enrollmentRatio;
1338
+ if (item.remainingSlots !== undefined && item.remainingSlots !== null) projected.remainingSlots = item.remainingSlots;
1339
+ if (item.hours !== undefined && item.hours !== null) projected.hours = item.hours;
1340
+ if (item.youthItemId) projected.youthItemId = item.youthItemId;
1341
+ if (item.registered !== undefined && item.registered !== null) projected.registered = item.registered;
1342
+ return projected;
985
1343
  }
986
1344
 
987
1345
  function memoryStore() {
@@ -992,6 +1350,28 @@ function memoryStore() {
992
1350
  };
993
1351
  }
994
1352
 
1353
+ const REFRESH_CONCURRENCY = 6;
1354
+
1355
+ async function mapWithConcurrency(items, concurrency, worker) {
1356
+ const results = new Array(items.length);
1357
+ let nextIndex = 0;
1358
+ async function runWorker() {
1359
+ while (nextIndex < items.length) {
1360
+ const idx = nextIndex;
1361
+ nextIndex += 1;
1362
+ try {
1363
+ const value = await worker(items[idx], idx);
1364
+ results[idx] = { status: 'fulfilled', value };
1365
+ } catch (reason) {
1366
+ results[idx] = { status: 'rejected', reason };
1367
+ }
1368
+ }
1369
+ }
1370
+ const workers = Array.from({ length: Math.min(concurrency, items.length) }, () => runWorker());
1371
+ await Promise.all(workers);
1372
+ return results;
1373
+ }
1374
+
995
1375
  /**
996
1376
  * createFeed({read, write, fetch, now})
997
1377
  *
@@ -1008,6 +1388,7 @@ export function createFeed(options = {}) {
1008
1388
  const now = typeof options.now === 'function' ? options.now : Date.now;
1009
1389
 
1010
1390
  const cache = loadCache(read);
1391
+ let fallbackRecommender = null;
1011
1392
  let refreshing = null;
1012
1393
  let lastAttempt = 0;
1013
1394
  let closed = false;
@@ -1022,13 +1403,17 @@ export function createFeed(options = {}) {
1022
1403
  }
1023
1404
  }
1024
1405
 
1025
- function statuses(ids) {
1026
- return ids.map((id) => ({
1027
- id,
1028
- label: FEED_SOURCES[id].label,
1029
- fetchedAt: cache.sources[id].fetchedAt ?? null,
1030
- error: cache.sources[id].error ?? null
1031
- }));
1406
+ function statuses(ids, extraSources = []) {
1407
+ return [
1408
+ ...ids.map((id) => ({
1409
+ id,
1410
+ label: FEED_SOURCES[id].label,
1411
+ category: FEED_SOURCES[id].category,
1412
+ fetchedAt: cache.sources[id].fetchedAt ?? null,
1413
+ error: cache.sources[id].error ?? null
1414
+ })),
1415
+ ...extraSources
1416
+ ];
1032
1417
  }
1033
1418
 
1034
1419
  function isStale(id) {
@@ -1053,24 +1438,22 @@ export function createFeed(options = {}) {
1053
1438
  lastAttempt = refreshNow;
1054
1439
  const signal = controller.signal;
1055
1440
 
1056
- const results = await Promise.allSettled(
1057
- SOURCE_IDS.map(async (id) => {
1058
- const source = FEED_SOURCES[id];
1059
- const html = await fetchText(source.url, {
1060
- fetch: fetchImpl,
1061
- allowHosts: source.hosts,
1062
- signal
1063
- });
1064
- const items = await parseSourceHtml(html, source, refreshNow, {
1065
- fetch: fetchImpl,
1066
- signal
1067
- });
1068
- if (items.length === 0) {
1069
- throw new Error('页面中没有解析到条目');
1070
- }
1071
- return { id, items };
1072
- })
1073
- );
1441
+ const results = await mapWithConcurrency(SOURCE_IDS, REFRESH_CONCURRENCY, async (id) => {
1442
+ const source = FEED_SOURCES[id];
1443
+ const html = await fetchText(source.url, {
1444
+ fetch: fetchImpl,
1445
+ allowHosts: source.hosts,
1446
+ signal
1447
+ });
1448
+ const items = await parseSourceHtml(html, source, refreshNow, {
1449
+ fetch: fetchImpl,
1450
+ signal
1451
+ });
1452
+ if (items.length === 0) {
1453
+ throw new Error('页面中没有解析到条目');
1454
+ }
1455
+ return { id, items };
1456
+ });
1074
1457
 
1075
1458
  if (closed) {
1076
1459
  return { sources: statuses(SOURCE_IDS), fetchedAt: latestFetchedAt(SOURCE_IDS) };
@@ -1154,10 +1537,31 @@ export function createFeed(options = {}) {
1154
1537
  });
1155
1538
  }
1156
1539
 
1540
+ function getItemById(id) {
1541
+ for (const srcId of SOURCE_IDS) {
1542
+ const found = cache.sources[srcId].items.find((entry) => entry.id === id);
1543
+ if (found) return found;
1544
+ }
1545
+ return null;
1546
+ }
1547
+
1548
+ function filterTokens(raw) {
1549
+ if (raw == null || raw === '' || raw === 'all') return null;
1550
+ return (Array.isArray(raw) ? raw : [raw])
1551
+ .flatMap((v) => String(v).split(','))
1552
+ .map((v) => v.trim().toLowerCase())
1553
+ .filter(Boolean);
1554
+ }
1555
+
1157
1556
  function query(queryOptions = {}) {
1158
- const selectedIds = normalizeSourceFilter(queryOptions?.source);
1557
+ const selectedIds = normalizeSourceFilter(queryOptions?.source, queryOptions?.category);
1159
1558
  maybeAutoRefresh(selectedIds);
1160
1559
 
1560
+ const sourceTokens = filterTokens(queryOptions?.source);
1561
+ const categoryTokens = filterTokens(queryOptions?.category);
1562
+ const intentFilter = queryOptions?.intent === 'activity' || queryOptions?.intent === 'notice'
1563
+ ? queryOptions.intent
1564
+ : null;
1161
1565
  const keyword = sanitizeText(String(queryOptions?.keyword ?? ''), 120).toLowerCase();
1162
1566
  const interests = normalizeInterests(queryOptions?.interests);
1163
1567
  const agenda = normalizeAgenda(queryOptions?.agenda);
@@ -1168,24 +1572,70 @@ export function createFeed(options = {}) {
1168
1572
  : 'time';
1169
1573
  const nowMs = now();
1170
1574
 
1575
+ const extraSourceMap = new Map();
1576
+ const matchedExtraSources = [];
1577
+ for (const src of Array.isArray(queryOptions?.extraSources) ? queryOptions.extraSources : []) {
1578
+ if (!src || typeof src !== 'object' || !src.id) continue;
1579
+ extraSourceMap.set(src.id, src);
1580
+ const srcOk = !sourceTokens || sourceTokens.includes(String(src.id).toLowerCase());
1581
+ const catOk = !categoryTokens || categoryTokens.includes(String(src.category ?? 'campus').toLowerCase());
1582
+ if (srcOk && catOk) matchedExtraSources.push(src);
1583
+ }
1584
+
1171
1585
  let items = [];
1172
1586
  for (const id of selectedIds) {
1173
1587
  items.push(...cache.sources[id].items);
1174
1588
  }
1589
+ for (const extra of Array.isArray(queryOptions?.extraItems) ? queryOptions.extraItems : []) {
1590
+ if (!extra || typeof extra !== 'object' || !extra.id || !extra.title) continue;
1591
+ const sid = String(extra.source ?? '').toLowerCase();
1592
+ const cat = String(extra.category ?? extraSourceMap.get(extra.source)?.category ?? 'activity').toLowerCase();
1593
+ if (sourceTokens && !sourceTokens.includes(sid)) continue;
1594
+ if (categoryTokens && !categoryTokens.includes(cat)) continue;
1595
+ items.push(extra);
1596
+ }
1175
1597
 
1176
1598
  if (keyword) {
1177
1599
  items = items.filter((item) => {
1178
- const haystack = `${item.title} ${item.location ?? ''} ${item.date ?? ''}`.toLowerCase();
1600
+ const haystack = `${item.title} ${item.location ?? ''} ${item.organizer ?? ''} ${item.date ?? ''}`.toLowerCase();
1179
1601
  return haystack.includes(keyword);
1180
1602
  });
1181
1603
  }
1182
1604
 
1183
- const projected = items.map((item) => publicItem(item, interests, agenda, nowMs, coverage));
1184
- const sorted = sortItems(projected, sort, interests, agenda, nowMs);
1605
+ let projected = items.map((item) => publicItem(item, interests, agenda, nowMs, coverage, extraSourceMap.get(item.source) ?? null));
1606
+ if (intentFilter) {
1607
+ projected = projected.filter((item) => item.intent === intentFilter);
1608
+ }
1609
+
1610
+ let sorted;
1611
+ if (sort === 'recommend') {
1612
+ if (!queryOptions?.recommender && !fallbackRecommender) {
1613
+ fallbackRecommender = createRecommender({ now });
1614
+ }
1615
+ const recommender = queryOptions?.recommender ?? fallbackRecommender;
1616
+ const explicitSourceFilter = Boolean(queryOptions?.source && queryOptions.source !== 'all');
1617
+ const explicitKeywordFilter = Boolean(keyword);
1618
+ sorted = recommender.rank(projected, {
1619
+ nowMs,
1620
+ interests,
1621
+ userContext: queryOptions?.userContext,
1622
+ explicitSourceFilter,
1623
+ explicitKeywordFilter,
1624
+ dedupe: queryOptions?.dedupe !== false,
1625
+ getSourceMeta: (id) => FEED_SOURCES[id] ?? extraSourceMap.get(id) ?? null,
1626
+ agendaInfo: {
1627
+ hasAgenda: agenda.length > 0,
1628
+ overlaps: (item) => overlapsAgenda(item, agenda),
1629
+ covered: (item) => coverageCovers(occupiedInterval(item.start, item.end), coverage)
1630
+ }
1631
+ });
1632
+ } else {
1633
+ sorted = sortItems(projected, sort, interests, agenda, nowMs);
1634
+ }
1185
1635
 
1186
1636
  return {
1187
1637
  items: sorted,
1188
- sources: statuses(selectedIds)
1638
+ sources: statuses(selectedIds, matchedExtraSources)
1189
1639
  };
1190
1640
  }
1191
1641
 
@@ -1200,5 +1650,5 @@ export function createFeed(options = {}) {
1200
1650
  refreshing = null;
1201
1651
  }
1202
1652
 
1203
- return { query, refresh, close };
1653
+ return { query, refresh, close, getItemById };
1204
1654
  }