dsh-ustc-campus 0.2.14 → 0.3.0

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/youth.mjs CHANGED
@@ -32,6 +32,7 @@
32
32
 
33
33
  import { createCipheriv } from 'node:crypto';
34
34
  import { shanghaiEpoch } from './public-fetch.mjs';
35
+ import { mapLimited } from './concurrency.mjs';
35
36
 
36
37
  export const YOUTH_ORIGIN = 'https://young.ustc.edu.cn';
37
38
  export const YOUTH_LOGIN_URL = `${YOUTH_ORIGIN}/login`;
@@ -46,9 +47,9 @@ const DEFAULT_TOKEN = 'kPBNkx0sSO3aIBaKDt9d2GJURVJfzFuP';
46
47
  const MAX_REDIRECT_HOPS = 8;
47
48
  const MAX_BODY_CHARS = 2_000_000;
48
49
  const MAX_RESPONSE_ROWS = 5_000;
49
- const DEFAULT_PAGE_SIZE = 50;
50
- const DEFAULT_MAX_PAGES = 4;
51
- const MAX_DETAIL_CHECKS = 12;
50
+ const DEFAULT_PAGE_SIZE = 100;
51
+ const DEFAULT_MAX_PAGES = 20;
52
+ const MAX_DETAIL_CHECKS = 24;
52
53
  const DETAIL_TIMEOUT_MS = 2_000;
53
54
  const DEFAULT_MAX_TOTAL = 2_000;
54
55
  const DEFAULT_TIMEOUT_MS = 20_000;
@@ -554,7 +555,7 @@ function enrolledRow(record) {
554
555
  const title = cleanText(record.itemName ?? record.title, 200);
555
556
  if (!id || !title) return null;
556
557
 
557
- if (registration(record) !== true) return null;
558
+ if (registration(record) === false || String(record.itemCategory) === '1') return null;
558
559
 
559
560
  const start = localTime(record.st ?? record.startTime);
560
561
  const end = localTime(record.et ?? record.endTime);
@@ -572,6 +573,12 @@ function enrolledRow(record) {
572
573
  };
573
574
  }
574
575
 
576
+ function explicitlyEnded(record, now) {
577
+ const end = localTime(record.et ?? record.endTime);
578
+ return (end && end.epoch < now) || /已结束|已终止|已取消|已完结/.test(record.itemStatus_dictText ?? record.applyStatus_dictText ?? '')
579
+ || String(record.examineStatus) === '-3';
580
+ }
581
+
575
582
  function sortRows(rows) {
576
583
  return rows.sort((left, right) => (
577
584
  left.start - right.start
@@ -589,11 +596,10 @@ function sortRows(rows) {
589
596
  * never accepts a token, cookie or username from its caller.
590
597
  *
591
598
  * @param {object} options
592
- * @param {object} options.context dedicated authenticated browser context
593
599
  * @param {Function} [options.now] clock seam returning epoch milliseconds
594
600
  * @param {number} [options.requestTimeoutMs]
595
- * @param {number} [options.pageSize] 1..100, default 50
596
- * @param {number} [options.maxPages] 1..10, default 4
601
+ * @param {number} [options.pageSize] 1..100, default 100
602
+ * @param {number} [options.maxPages] 1..100, default 20
597
603
  * @param {number} [options.maxTotal] safety cap for upstream `total`
598
604
  * @param {string} [options.from] inclusive Shanghai date of the first requested day
599
605
  * @param {string} [options.through] inclusive Shanghai date of the last requested day
@@ -613,7 +619,7 @@ export async function readEnrolled({
613
619
  now: typeof now === 'function' ? now : Date.now,
614
620
  timeout: boundedInteger(requestTimeoutMs, DEFAULT_TIMEOUT_MS, 1_000, 120_000),
615
621
  pageSize: boundedInteger(pageSize, DEFAULT_PAGE_SIZE, 1, 100),
616
- maxPages: boundedInteger(maxPages, DEFAULT_MAX_PAGES, 1, 10),
622
+ maxPages: boundedInteger(maxPages, DEFAULT_MAX_PAGES, 1, 100),
617
623
  maxTotal: boundedInteger(maxTotal, DEFAULT_MAX_TOTAL, 1, 10_000)
618
624
  };
619
625
 
@@ -639,7 +645,11 @@ export async function readEnrolled({
639
645
  let lastPage = 1;
640
646
  let complete = true;
641
647
  let page = 1;
642
- let detailChecks = 0;
648
+ const missingTimes = [];
649
+ const nowMs = clockValue(options.now);
650
+ const collect = row => {
651
+ if (row && (!from || row.date >= from) && (!through || row.date <= through)) entries.push(row);
652
+ };
643
653
 
644
654
  while (page <= options.maxPages) {
645
655
  let pageResult;
@@ -650,8 +660,9 @@ export async function readEnrolled({
650
660
  }
651
661
 
652
662
  if (pageResult.kind !== 'page') {
653
- if (entries.length > 0) return result('ok', sortRows(entries), false);
654
- return result(pageResult.kind === 'not_connected' ? 'not_connected' : 'failure');
663
+ if (total === null) return result(pageResult.kind === 'not_connected' ? 'not_connected' : 'failure');
664
+ complete = false;
665
+ break;
655
666
  }
656
667
 
657
668
  if (total === null) {
@@ -663,25 +674,17 @@ export async function readEnrolled({
663
674
  lastPage = Math.max(lastPage, Math.ceil(pageResult.total / options.pageSize));
664
675
  if (pageResult.records.length !== Math.min(options.pageSize, Math.max(0, pageResult.total - (page - 1) * options.pageSize))) complete = false;
665
676
 
666
- for (let record of pageResult.records) {
677
+ for (const record of pageResult.records) {
667
678
  const id = cleanId(record?.id ?? record?.itemId);
668
679
  if (!id || seen.has(id)) { complete = false; continue; }
669
680
  seen.add(id);
681
+ if (registration(record) === false || String(record.itemCategory) === '1') continue;
670
682
  const start = localTime(record.st ?? record.startTime);
671
683
  if (start && ((from && start.date < from) || (through && start.date > through))) continue;
672
- if (registration(record) === false) continue;
673
- if (registration(record) == null && detailChecks < MAX_DETAIL_CHECKS) {
674
- detailChecks += 1;
675
- const detail = await youthQuery(context, token, 'item/scItem/queryById', {id}, detailOptions);
676
- if (detail.kind === 'ok' && cleanId(detail.value.id) === id && registration(detail.value) !== null) {
677
- record = { ...record, ...detail.value };
678
- }
679
- }
680
- if (registration(record) === false) continue;
681
684
  const row = enrolledRow(record);
682
- if (!row) { complete = false; continue; }
683
- if ((from && row.date < from) || (through && row.date > through)) continue;
684
- entries.push(row);
685
+ if (row) collect(row);
686
+ else if (missingTimes.length < MAX_DETAIL_CHECKS && !explicitlyEnded(record, nowMs)
687
+ && (!start || !localTime(record.et ?? record.endTime))) missingTimes.push({ id, record });
685
688
  }
686
689
 
687
690
  if (page >= lastPage) break;
@@ -692,9 +695,15 @@ export async function readEnrolled({
692
695
  page += 1;
693
696
  }
694
697
 
698
+ const details = await mapLimited(missingTimes, 4, async ({ id, record }) => {
699
+ const detail = await youthQuery(context, token, 'item/scItem/queryById', { id }, detailOptions);
700
+ if (detail.kind === 'ok' && cleanId(detail.value.id ?? detail.value.itemId) === id) {
701
+ return enrolledRow({ ...record, ...detail.value });
702
+ }
703
+ });
704
+ for (const detail of details) if (detail.status === 'fulfilled') collect(detail.value);
695
705
  complete &&= seen.size === total;
696
706
  if (total === 0) return complete ? result('empty') : result('failure');
697
- if (entries.length === 0 && !complete) return result('failure');
698
707
  return result('ok', sortRows(entries), complete);
699
708
  }
700
709
 
@@ -845,6 +854,10 @@ export async function listYouthActivities({
845
854
  const opts = { now, timeout: boundedInteger(requestTimeoutMs, DEFAULT_TIMEOUT_MS, 1_000, 120_000) };
846
855
 
847
856
  const session = await obtainYouthSession(context, opts);
857
+ return listActivityPage(context, session, normalizedMode, kw, pageNo, size, opts);
858
+ }
859
+
860
+ async function listActivityPage(context, session, normalizedMode, kw, pageNo, size, opts) {
848
861
  if (session.kind !== 'token') {
849
862
  const isNotConn = session.kind === 'not_connected';
850
863
  return {
@@ -886,7 +899,7 @@ export async function listYouthActivities({
886
899
  pageSize: size,
887
900
  total: total ?? items.length,
888
901
  items,
889
- note: '列表中的 applyStart/applyEnd 为报名起止时间;若未含举办时间,查看详情可获取具体时间。',
902
+ note: 'applyStart/applyEnd 为报名起止时间;举办时间缺失时请看详情。公开列表的 enrolledCount / remainingSlots 为上游缓存,余量以详情或报名草稿的实时校验为准。',
890
903
  untrustedContent: true
891
904
  };
892
905
  }
@@ -1144,6 +1157,19 @@ export function createYouth({ withSchoolContext, acquireContext, ...options } =
1144
1157
  return fn(context);
1145
1158
  }
1146
1159
 
1160
+ async function enrollmentPages(context) {
1161
+ const opts = { now: options.now ?? Date.now, timeout: options.requestTimeoutMs ?? DEFAULT_TIMEOUT_MS };
1162
+ const session = await obtainYouthSession(context, opts);
1163
+ const size = 50;
1164
+ const read = page => listActivityPage(context, session, 'enrolling', '', page, size, opts);
1165
+ const first = await read(1);
1166
+ if (first.state !== 'ok' || first.total <= size) return first;
1167
+ const pages = Array.from({ length: Math.min(50, Math.ceil(first.total / size)) - 1 }, (_, i) => i + 2);
1168
+ const results = await mapLimited(pages, 4, read);
1169
+ if (results.some(r => r.status !== 'fulfilled' || !['ok', 'empty'].includes(r.value.state))) return result('failure');
1170
+ return { ...first, items: first.items.concat(...results.map(r => r.value.items)) };
1171
+ }
1172
+
1147
1173
  return Object.freeze({
1148
1174
  read({ from, through } = {}) {
1149
1175
  return runScoped(
@@ -1151,6 +1177,9 @@ export function createYouth({ withSchoolContext, acquireContext, ...options } =
1151
1177
  (state) => result(state)
1152
1178
  );
1153
1179
  },
1180
+ enrollingFeed() {
1181
+ return runScoped(enrollmentPages, state => result(state));
1182
+ },
1154
1183
  listActivities(args = {}) {
1155
1184
  return runScoped(
1156
1185
  (context) => listYouthActivities({ context, ...options, ...args }),