papersearch 0.8.0 → 0.8.1
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/NOTICE +1 -1
- package/README.fr.md +5 -8
- package/README.ja.md +5 -8
- package/README.ko.md +5 -8
- package/README.md +148 -151
- package/README.zh-Hant.md +5 -8
- package/README.zh.md +265 -0
- package/dist/capabilities/download/batch/batch.d.ts +4 -0
- package/dist/capabilities/download/batch/batch.d.ts.map +1 -1
- package/dist/capabilities/download/batch/batch.js +714 -198
- package/dist/capabilities/download/batch/batch.js.map +1 -1
- package/dist/capabilities/download/batch/windowPlan.d.ts +96 -0
- package/dist/capabilities/download/batch/windowPlan.d.ts.map +1 -0
- package/dist/capabilities/download/batch/windowPlan.js +196 -0
- package/dist/capabilities/download/batch/windowPlan.js.map +1 -0
- package/dist/capabilities/download/content/markdown/step.d.ts.map +1 -1
- package/dist/capabilities/download/content/markdown/step.js +4 -1
- package/dist/capabilities/download/content/markdown/step.js.map +1 -1
- package/dist/capabilities/download/content/pdf/open/tiers/scihub.d.ts.map +1 -1
- package/dist/capabilities/download/content/pdf/open/tiers/scihub.js +3 -1
- package/dist/capabilities/download/content/pdf/open/tiers/scihub.js.map +1 -1
- package/dist/infrastructure/http/hostGate.d.ts +24 -0
- package/dist/infrastructure/http/hostGate.d.ts.map +1 -0
- package/dist/infrastructure/http/hostGate.js +72 -0
- package/dist/infrastructure/http/hostGate.js.map +1 -0
- package/dist/infrastructure/pdf/PdfDownload.d.ts +1 -0
- package/dist/infrastructure/pdf/PdfDownload.d.ts.map +1 -1
- package/dist/infrastructure/pdf/PdfDownload.js +6 -1
- package/dist/infrastructure/pdf/PdfDownload.js.map +1 -1
- package/dist/infrastructure/runlog/RunLedger.d.ts +7 -0
- package/dist/infrastructure/runlog/RunLedger.d.ts.map +1 -1
- package/dist/infrastructure/runlog/RunLedger.js +17 -4
- package/dist/infrastructure/runlog/RunLedger.js.map +1 -1
- package/dist/infrastructure/school/SchoolMcpClient.d.ts +12 -2
- package/dist/infrastructure/school/SchoolMcpClient.d.ts.map +1 -1
- package/dist/infrastructure/school/SchoolMcpClient.js +122 -11
- package/dist/infrastructure/school/SchoolMcpClient.js.map +1 -1
- package/dist/infrastructure/school/SchoolReadiness.d.ts +7 -0
- package/dist/infrastructure/school/SchoolReadiness.d.ts.map +1 -1
- package/dist/infrastructure/school/SchoolReadiness.js +8 -0
- package/dist/infrastructure/school/SchoolReadiness.js.map +1 -1
- package/dist/infrastructure/school/index.d.ts +1 -1
- package/dist/infrastructure/school/index.d.ts.map +1 -1
- package/dist/infrastructure/school/types.d.ts +31 -1
- package/dist/infrastructure/school/types.d.ts.map +1 -1
- package/dist/infrastructure/school/types.js.map +1 -1
- package/dist/infrastructure/security/SecurityUtils.d.ts.map +1 -1
- package/dist/infrastructure/security/SecurityUtils.js +6 -2
- package/dist/infrastructure/security/SecurityUtils.js.map +1 -1
- package/dist/management/failures/index.d.ts.map +1 -1
- package/dist/management/failures/index.js +5 -2
- package/dist/management/failures/index.js.map +1 -1
- package/dist/management/school/autoUpdate.d.ts.map +1 -1
- package/dist/management/school/autoUpdate.js +9 -1
- package/dist/management/school/autoUpdate.js.map +1 -1
- package/dist/management/school/cliProgress.d.ts.map +1 -1
- package/dist/management/school/cliProgress.js +3 -1
- package/dist/management/school/cliProgress.js.map +1 -1
- package/package.json +2 -2
- package/school/paper_search_school/adapters/base.py +3 -0
- package/school/paper_search_school/adapters/optica/__init__.py +50 -4
- package/school/paper_search_school/adapters/platforms/atypon/__init__.py +4 -3
- package/school/paper_search_school/adapters/platforms/figshare/__init__.py +23 -4
- package/school/paper_search_school/assets/capsule.html +380 -41
- package/school/paper_search_school/assets/capsule_en.json +72 -4
- package/school/paper_search_school/assets/capsule_fr.json +72 -4
- package/school/paper_search_school/assets/capsule_ja.json +72 -4
- package/school/paper_search_school/assets/capsule_ko.json +72 -4
- package/school/paper_search_school/assets/capsule_zh-Hant.json +66 -3
- package/school/paper_search_school/browser/backend.py +125 -4
- package/school/paper_search_school/browser/batch_session.py +188 -7
- package/school/paper_search_school/browser/challenge/continuation.py +19 -1
- package/school/paper_search_school/browser/challenge/handlers/text_captcha.py +38 -10
- package/school/paper_search_school/browser/challenge/pro.py +24 -0
- package/school/paper_search_school/browser/challenge/services/solver.py +24 -4
- package/school/paper_search_school/browser/engine.py +4 -1
- package/school/paper_search_school/browser/user_turn.py +126 -0
- package/school/paper_search_school/browser/watchdog.py +213 -60
- package/school/paper_search_school/config.py +5 -0
- package/school/paper_search_school/engine/batch.py +9 -7
- package/school/paper_search_school/engine/batch_access.py +27 -7
- package/school/paper_search_school/engine/batch_capture.py +45 -13
- package/school/paper_search_school/engine/batch_institution.py +14 -0
- package/school/paper_search_school/engine/batch_login.py +177 -22
- package/school/paper_search_school/engine/batch_user.py +48 -3
- package/school/paper_search_school/engine/batch_waits.py +42 -2
- package/school/paper_search_school/engine/bridge.py +1 -2
- package/school/paper_search_school/engine/fulltext_page.py +10 -1
- package/school/paper_search_school/engine/supplements.py +7 -1
- package/school/paper_search_school/entitlements.py +28 -0
- package/school/paper_search_school/progress_capsule.py +33 -5
- package/school/paper_search_school/progress_reporter.py +189 -6
- package/school/paper_search_school/progress_view.py +142 -48
- package/school/paper_search_school/rate_limit.py +24 -1
- package/school/paper_search_school/school_login.py +170 -8
- package/school/paper_search_school/server.py +164 -19
- package/school/paper_search_school/user_settings.py +33 -4
- package/skills/paper-search/SKILL.md +1 -1
- package/skills/paper-search/references/browser-download.md +2 -2
- package/LICENSE-paper-search-mcp-nodejs +0 -24
- package/README.en.md +0 -268
|
@@ -15,8 +15,11 @@ import { loadMarkdownPro, markdownBlocked } from "../content/markdown/pro.js";
|
|
|
15
15
|
import { PaperList } from "./batchPapers.js";
|
|
16
16
|
import { networkReachable, pastNetworkLoss } from "./networkLoss.js";
|
|
17
17
|
import { openBatchFolder } from "./batchFolder.js";
|
|
18
|
+
import { isParallel, publisherKey, WindowPlan } from "./windowPlan.js";
|
|
18
19
|
/** From this many items the progress window opens at the start, in the open-access stretch. */
|
|
19
20
|
export const OPEN_PROGRESS_MIN_ITEMS = 5;
|
|
21
|
+
/** The open-access lane's papers checked at once (user, 2026-11-29: fixed, not a setting). */
|
|
22
|
+
export const OPEN_AT_ONCE = 3;
|
|
20
23
|
/** Every source that needs no browser: open access, publisher APIs, Sci-Hub when on (the guide's 开放下载). */
|
|
21
24
|
const OPEN_CHANNEL = "开放下载";
|
|
22
25
|
/**
|
|
@@ -135,6 +138,8 @@ export const KEEP_SESSION_EVERY_MS = 10 * 60_000;
|
|
|
135
138
|
* 2026-11-19). Each ask reads the backend's settings and count only.
|
|
136
139
|
*/
|
|
137
140
|
export const HOURLY_RECHECK_EVERY_MS = 10_000;
|
|
141
|
+
/** How often a batch's first paper, starting the backend, has the batch ask it for 同时下载 (并行下载). */
|
|
142
|
+
export const PARALLEL_ASK_EVERY_MS = 1_000;
|
|
138
143
|
/**
|
|
139
144
|
* Runs the item again after each wait the publisher's hourly limit asks for. During a
|
|
140
145
|
* wait, the backend's keepSession runs every KEEP_SESSION_EVERY_MS.
|
|
@@ -220,6 +225,47 @@ function withoutAccess(result) {
|
|
|
220
225
|
const school = [...result.attempts].reverse().find((attempt) => attempt.stage === "entitled_access");
|
|
221
226
|
return school?.status === "error" && classifyFailureCode(school.code) === "no_access";
|
|
222
227
|
}
|
|
228
|
+
/** 没权限 for a paper's Markdown: the school's own reasons for no page (no route, refused, not enabled), or a page showing no access. */
|
|
229
|
+
function markdownWithoutAccess(result) {
|
|
230
|
+
return !!result?.markdown && result.markdown.status !== "ok" && markdownMissing(result.markdown).cls === "no_access";
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* 没权限 for a paper's supplementary files: none saved, the visit for them refused for the school (it fails for the page,
|
|
234
|
+
* not file by file; a list with some saved is kept as it is).
|
|
235
|
+
*/
|
|
236
|
+
function filesWithoutAccess(result) {
|
|
237
|
+
const files = result?.supplements ?? [];
|
|
238
|
+
return files.some((file) => classifyFailureCode(file.reason) === "no_access") && !files.some((file) => file.status === "saved");
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* A paper whose PDF stands (open access, Sci-Hub) still lacking its Markdown or supplementary files under 没权限: another
|
|
242
|
+
* saved school may have them, and the batch's end asks it for those alone, the PDF not downloaded again (user,
|
|
243
|
+
* 2026-11-29: five SAGE papers' PDFs from Sci-Hub, their Markdown 出版社没开通本校 with the default school, never tried
|
|
244
|
+
* with the two others saved). ``saved``: no PDF in the batch (the Markdown or the files alone), whatever its status.
|
|
245
|
+
*/
|
|
246
|
+
function partsWithoutAccess(result, saved = result?.status === "ok") {
|
|
247
|
+
return !!result && saved && !stoppedByUser(result) && (markdownWithoutAccess(result) || filesWithoutAccess(result));
|
|
248
|
+
}
|
|
249
|
+
/** What ``partsAgain`` asks for, as the log says it. */
|
|
250
|
+
function partsNamed(result) {
|
|
251
|
+
const files = filesWithoutAccess(result);
|
|
252
|
+
const markdown = markdownWithoutAccess(result);
|
|
253
|
+
return files && markdown ? "补充材料和 Markdown" : files ? "补充材料" : "Markdown";
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* A saved paper's supplementary files and Markdown that lacked access, asked for again with ``request``'s school, its PDF
|
|
257
|
+
* standing: the files' visit reads the page for the Markdown too when that is wanted again, as a paper's first visit
|
|
258
|
+
* does (``finishOpenItem``); else the Markdown's own visit (``visit``).
|
|
259
|
+
*/
|
|
260
|
+
async function partsAgain(item, request, backend, searchers, visit, watch) {
|
|
261
|
+
const markdown = markdownWithoutAccess(item);
|
|
262
|
+
if (filesWithoutAccess(item)) {
|
|
263
|
+
delete item.supplements;
|
|
264
|
+
await filesPastNetworkLoss(item, { ...request, fulltext: markdown && request.fulltext }, backend, searchers, watch);
|
|
265
|
+
}
|
|
266
|
+
if (markdown && !stoppedByUser(item))
|
|
267
|
+
await markdownPastNetworkLoss(item, request, searchers, visit, watch);
|
|
268
|
+
}
|
|
223
269
|
/**
|
|
224
270
|
* 原因不明 (FailureClass unexplained) at the school's stage: tried once more at the batch's end, with the school it had,
|
|
225
271
|
* before any other school (user, 2026-11-22: "原因不明的也最后再试一次"); not a paper the user stopped.
|
|
@@ -263,12 +309,17 @@ async function retrySetting(backend) {
|
|
|
263
309
|
* every paper (switching to and fro had a school's login ask for a picture code, Windows 2026-11-22). ``visit`` tries
|
|
264
310
|
* one paper with a school.
|
|
265
311
|
*/
|
|
266
|
-
async function laterSchools(schools, waiting, announce, visit, stopped) {
|
|
312
|
+
async function laterSchools(schools, waiting, announce, visit, stopped, together) {
|
|
267
313
|
for (const school of schools) {
|
|
268
314
|
const offsets = waiting();
|
|
269
315
|
if (!offsets.length || stopped())
|
|
270
316
|
return;
|
|
271
317
|
await announce(school, offsets.length);
|
|
318
|
+
// A batch whose papers ran at once: a school's papers in its windows (``together``).
|
|
319
|
+
if (together) {
|
|
320
|
+
await together(offsets, (offset, lane) => visit(offset, school, lane));
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
272
323
|
for (const offset of offsets) {
|
|
273
324
|
if (stopped())
|
|
274
325
|
return;
|
|
@@ -514,43 +565,79 @@ async function downloadSerialBatch(options, backend, log, searchers, createTiers
|
|
|
514
565
|
/** Papers whose Markdown lacks figures, recorded after their second try at the end (figuresAtTheEnd). */
|
|
515
566
|
const figuresLater = [];
|
|
516
567
|
let stopped = false;
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
568
|
+
// 没权限时换下一所学校 (PaperSearch Pro): papers whose Markdown or files lacked access wait for the other saved schools,
|
|
569
|
+
// tried at the end for those alone, one school at a time (``laterSchools``; user, 2026-11-29: the Markdown's 没权限
|
|
570
|
+
// said so and none was tried).
|
|
571
|
+
let turns;
|
|
572
|
+
const otherSchools = () => (turns ??= schoolsAfterDefault(backend, options));
|
|
573
|
+
const waiting = new Map();
|
|
574
|
+
const later = () => ({ noAccess: waiting.size, unexplained: 0, blocked: 0, login: 0,
|
|
575
|
+
papers: { noAccess: [...waiting.values()].map(({ request }) => request.doi ?? "").filter(Boolean), unexplained: [], blocked: [],
|
|
576
|
+
login: [] } });
|
|
577
|
+
/**
|
|
578
|
+
* A paper's files, its Markdown or both, with ``request``'s school. ``before``: what it had, at the batch's end with
|
|
579
|
+
* another school; only what lacked access is asked for again, the rest kept.
|
|
580
|
+
*/
|
|
581
|
+
const fetchOnce = async (item, request, entry, before) => {
|
|
522
582
|
// 接着下 (resume.ts): a paper whose PDF a batch saved before gets its Markdown beside it, not the PDF again.
|
|
523
|
-
// Done again after a network loss, from what was there before it (``
|
|
524
|
-
const markdown = (
|
|
525
|
-
const
|
|
583
|
+
// Done again after a network loss, from what was there before it (``from`` copied each time).
|
|
584
|
+
const markdown = (from) => pastNetworkLoss(() => {
|
|
585
|
+
const copy = { ...from, attempts: [...from.attempts] };
|
|
526
586
|
return item.mainPath
|
|
527
|
-
? markdownBeside(
|
|
528
|
-
: fulltextAlone(
|
|
587
|
+
? markdownBeside(copy, item.mainPath, recorded(request, ledger, entry), searchers, pageVisit(backend, request, log, pause))
|
|
588
|
+
: fulltextAlone(copy, recorded(request, ledger, entry), searchers, pageVisit(backend, request, log, pause));
|
|
529
589
|
}, watch);
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
590
|
+
if (content === "markdown")
|
|
591
|
+
return markdown(before ?? { status: "error", attempts: [] });
|
|
592
|
+
const markdownAgain = !before || markdownWithoutAccess(before);
|
|
593
|
+
let result = before ?? { status: "error", attempts: [] };
|
|
594
|
+
if (!before || filesWithoutAccess(before)) {
|
|
595
|
+
// The files' visit reads the page for the Markdown too, when that is asked for again.
|
|
596
|
+
const files = await pastNetworkLoss(() => downloadSupplementsOnly(recorded({ ...request, ...(markdownAgain ? {} : { fulltext: false }) }, ledger, entry), backend, searchers, item.mainPath), watch);
|
|
597
|
+
result = before ? { ...files, attempts: [...before.attempts, ...files.attempts], ...(before.markdown ? { markdown: before.markdown } : {}) }
|
|
598
|
+
: files;
|
|
538
599
|
}
|
|
539
|
-
if (
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
600
|
+
if (options.fulltext && markdownAgain && !stoppedByUser(result))
|
|
601
|
+
result = await markdown(result);
|
|
602
|
+
return result;
|
|
603
|
+
};
|
|
604
|
+
/** A paper finished: counted, shown, recorded (its figures once more at the end first). */
|
|
605
|
+
const finish = async (offset, entry, request, result) => {
|
|
606
|
+
const item = options.items[offset];
|
|
607
|
+
const done = succeeded + failed;
|
|
608
|
+
// The window's end when the last paper is finished, not the last in the list (some wait for the end).
|
|
609
|
+
await reportBatchItem({ ...request, batch: { id: batchId, index: done + 1, total, done, succeeded, failed } }, result, backend);
|
|
544
610
|
if (result.status === "ok")
|
|
545
611
|
succeeded += 1;
|
|
546
612
|
else
|
|
547
613
|
failed += 1;
|
|
548
|
-
items
|
|
614
|
+
items[offset] = itemResult(result, offset + 1, request);
|
|
549
615
|
if (figuresMissing(result))
|
|
550
616
|
figuresLater.push({ offset, entry, request, ...(item.mainPath ? { mainPath: item.mainPath } : {}) });
|
|
551
617
|
else
|
|
552
618
|
ledger?.item(entry, result);
|
|
553
|
-
papers.set(offset, batchPaper(request, items[
|
|
619
|
+
papers.set(offset, batchPaper(request, items[offset], undefined, content, item.mainPath));
|
|
620
|
+
};
|
|
621
|
+
for (const [offset, item] of options.items.entries()) {
|
|
622
|
+
// Papers waiting for the end are not done yet: the counts are of those finished.
|
|
623
|
+
const batch = { id: batchId, index: offset + 1, total, done: succeeded + failed, succeeded, failed };
|
|
624
|
+
const request = { ...itemOptions(item, options), batch, ...(away ? { shortUserWait: true } : {}) };
|
|
625
|
+
const entry = runItem(offset + 1, request);
|
|
626
|
+
log(`[批量] ${content === "markdown" ? "Markdown" : options.fulltext ? "补充材料和 Markdown" : "补充材料"} ${offset + 1}/${total}:${request.doi || request.paperId}`);
|
|
627
|
+
const result = await fetchOnce(item, request, entry);
|
|
628
|
+
if (!away && waitedOutOnUser(result)) {
|
|
629
|
+
away = true;
|
|
630
|
+
log(AWAY_NOTICE);
|
|
631
|
+
}
|
|
632
|
+
if (partsWithoutAccess(result, true) && (await otherSchools()).length) {
|
|
633
|
+
// Not finished: it waits for the other schools at the end.
|
|
634
|
+
items[offset] = itemResult(result, offset + 1, request);
|
|
635
|
+
waiting.set(offset, { entry, request });
|
|
636
|
+
papers.set(offset, batchPaper(request, items[offset], "school", content, item.mainPath));
|
|
637
|
+
await papers.send();
|
|
638
|
+
continue;
|
|
639
|
+
}
|
|
640
|
+
await finish(offset, entry, request, result);
|
|
554
641
|
if (stoppedByUser(result)) {
|
|
555
642
|
stopped = true;
|
|
556
643
|
for (const [rest, item] of options.items.entries()) {
|
|
@@ -559,7 +646,7 @@ async function downloadSerialBatch(options, backend, log, searchers, createTiers
|
|
|
559
646
|
const request = itemOptions(item, options);
|
|
560
647
|
const skipped = itemResult({ status: "error", attempts: [notReached()],
|
|
561
648
|
...(content === "supplements" ? { supplements: [{ status: "skipped", label: "", reason: "batch_stopped" }] } : {}) }, rest + 1, request);
|
|
562
|
-
items
|
|
649
|
+
items[rest] = skipped;
|
|
563
650
|
ledger?.item(runItem(rest + 1, request), skipped);
|
|
564
651
|
papers.set(rest, batchPaper(request, skipped, undefined, content, item.mainPath));
|
|
565
652
|
}
|
|
@@ -568,6 +655,35 @@ async function downloadSerialBatch(options, backend, log, searchers, createTiers
|
|
|
568
655
|
}
|
|
569
656
|
await papers.send();
|
|
570
657
|
}
|
|
658
|
+
// The papers whose Markdown or files lacked access go to the other saved schools, one school at a time.
|
|
659
|
+
if (waiting.size && !stopped) {
|
|
660
|
+
await laterSchools(await otherSchools(), () => [...waiting.keys()], async (school, count) => {
|
|
661
|
+
log(`[批量] 没权限的 ${count} 篇换${school.name}再试`);
|
|
662
|
+
ledger?.phase(`没权限的 ${count} 篇换${school.name}再试`);
|
|
663
|
+
await backend?.reportBatchProgress?.({ id: batchId, total, done: succeeded + failed, succeeded, failed, current: "",
|
|
664
|
+
finished: false, switchTo: school.name, laterOnly: true, later: later() }).catch(() => false);
|
|
665
|
+
}, async (offset, school) => {
|
|
666
|
+
const { entry } = waiting.get(offset);
|
|
667
|
+
const item = options.items[offset];
|
|
668
|
+
const batch = { id: batchId, index: offset + 1, total, done: succeeded + failed, succeeded, failed };
|
|
669
|
+
const request = { ...itemOptions(item, options), school: school.entityId, batch, ...(away ? { shortUserWait: true } : {}) };
|
|
670
|
+
log(`[批量] 换${school.name}补${partsNamed(items[offset])}:${request.doi || request.paperId}`);
|
|
671
|
+
const result = await fetchOnce(item, request, entry, items[offset]);
|
|
672
|
+
items[offset] = itemResult(result, offset + 1, request);
|
|
673
|
+
if (stoppedByUser(result))
|
|
674
|
+
stopped = true;
|
|
675
|
+
if (!stopped && partsWithoutAccess(result, true))
|
|
676
|
+
return;
|
|
677
|
+
const { request: asked } = waiting.get(offset);
|
|
678
|
+
waiting.delete(offset);
|
|
679
|
+
await finish(offset, entry, asked, result);
|
|
680
|
+
await papers.send();
|
|
681
|
+
}, () => stopped);
|
|
682
|
+
}
|
|
683
|
+
// Without access with every school (or the batch stopped first): as they were.
|
|
684
|
+
for (const [offset, { entry, request }] of waiting)
|
|
685
|
+
await finish(offset, entry, request, items[offset]);
|
|
686
|
+
waiting.clear();
|
|
571
687
|
// Markdown saved without some figures: those once more, unless the user stopped the batch (user, 2026-11-23).
|
|
572
688
|
if (!stopped) {
|
|
573
689
|
await figuresAtTheEnd(figuresLater.map(({ offset, entry, request }) => ({ result: items[offset], request: recorded(request, ledger, entry) })), log, ledger);
|
|
@@ -602,6 +718,8 @@ async function downloadPerItem(searchers, options, backend, log, createTiers, pa
|
|
|
602
718
|
let retryOn;
|
|
603
719
|
const retrying = () => (retryOn ??= retrySetting(backend));
|
|
604
720
|
const waiting = new Map();
|
|
721
|
+
/** Of the waiting papers, those whose PDF stands, waiting for their Markdown or files alone (partsWithoutAccess). */
|
|
722
|
+
const partsOnly = new Set();
|
|
605
723
|
const schoolsTried = new Map();
|
|
606
724
|
/** Papers failed for a reason not known, waiting for their one more try at the end (user, 2026-11-22). */
|
|
607
725
|
const unexplainedYet = new Map();
|
|
@@ -648,9 +766,12 @@ async function downloadPerItem(searchers, options, backend, log, createTiers, pa
|
|
|
648
766
|
await markdownPastNetworkLoss(finished, recorded({ ...request, batch }, ledger, entry), searchers, pageVisit(backend, { ...request, batch }, log, pause), watch);
|
|
649
767
|
}
|
|
650
768
|
items.push(finished);
|
|
651
|
-
|
|
652
|
-
|
|
769
|
+
const partsLacking = partsWithoutAccess(finished);
|
|
770
|
+
if ((withoutAccess(finished) && !stoppedByUser(finished) || partsLacking) && (await otherSchools()).length) {
|
|
771
|
+
// Not finished: it waits for the other schools at the end (its Markdown or files alone when its PDF stands).
|
|
653
772
|
waiting.set(offset, entry);
|
|
773
|
+
if (partsLacking)
|
|
774
|
+
partsOnly.add(offset);
|
|
654
775
|
papers.set(offset, batchPaper(request, finished, "school", content));
|
|
655
776
|
await papers.send();
|
|
656
777
|
await reportLater();
|
|
@@ -711,6 +832,18 @@ async function downloadPerItem(searchers, options, backend, log, createTiers, pa
|
|
|
711
832
|
stopped = true;
|
|
712
833
|
return finished;
|
|
713
834
|
};
|
|
835
|
+
/** A paper whose PDF stands, its Markdown or files without access, with another school: those alone. */
|
|
836
|
+
const partsAt = async (offset, entry, school) => {
|
|
837
|
+
await reportLater();
|
|
838
|
+
const request = { ...itemOptions(options.items[offset], options), school: school.entityId };
|
|
839
|
+
const batch = { id: batchId, index: offset + 1, total, done: succeeded + failed, succeeded, failed };
|
|
840
|
+
const item = items[offset];
|
|
841
|
+
log(`[批量] 换${school.name}补${partsNamed(item)}:${request.doi || request.paperId}`);
|
|
842
|
+
await partsAgain(item, recorded({ ...request, batch, ...(away ? { shortUserWait: true } : {}) }, ledger, entry), backend, searchers, pageVisit(backend, { ...request, batch, ...(away ? { shortUserWait: true } : {}) }, log, pause), watch);
|
|
843
|
+
if (stoppedByUser(item))
|
|
844
|
+
stopped = true;
|
|
845
|
+
return item;
|
|
846
|
+
};
|
|
714
847
|
/** A paper finished at the end: counted, recorded, shown. */
|
|
715
848
|
const finish = async (offset, entry) => {
|
|
716
849
|
const request = itemOptions(options.items[offset], options);
|
|
@@ -750,6 +883,13 @@ async function downloadPerItem(searchers, options, backend, log, createTiers, pa
|
|
|
750
883
|
await reportLater(school);
|
|
751
884
|
}, async (offset, school) => {
|
|
752
885
|
const entry = waiting.get(offset);
|
|
886
|
+
if (partsOnly.has(offset)) {
|
|
887
|
+
if (!stopped && partsWithoutAccess(await partsAt(offset, entry, school)))
|
|
888
|
+
return;
|
|
889
|
+
waiting.delete(offset);
|
|
890
|
+
await finish(offset, entry);
|
|
891
|
+
return;
|
|
892
|
+
}
|
|
753
893
|
const finished = await again(offset, entry, school);
|
|
754
894
|
schoolsTried.set(offset, (schoolsTried.get(offset) ?? 1) + 1);
|
|
755
895
|
if (!stopped && withoutAccess(finished)) {
|
|
@@ -878,54 +1018,15 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
878
1018
|
reports = reports.then(() => backend.reportBatchProgress(update)).catch(() => false);
|
|
879
1019
|
return reports;
|
|
880
1020
|
} };
|
|
1021
|
+
// The open-access lane checks OPEN_AT_ONCE papers at once (user, 2026-11-29), each site within its own limit
|
|
1022
|
+
// (infrastructure/http/hostGate.ts); a paper it misses goes to the browser's lane as soon as it is known.
|
|
1023
|
+
let nextOpen = 0;
|
|
881
1024
|
const openLane = async () => {
|
|
882
1025
|
try {
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
const tiers = createTiers(backend).filter((tier) => tier.stage !== "entitled_access" && tier.stage !== "preprint");
|
|
888
|
-
let result;
|
|
889
|
-
try {
|
|
890
|
-
result = await pastNetworkLoss(() => downloadWithFallback(searchers, recorded(request, ledger, entries[offset]), tiers), watch);
|
|
891
|
-
}
|
|
892
|
-
catch (error) {
|
|
893
|
-
result = laneError("open_access", error);
|
|
894
|
-
log(`[批量] 开放来源出错(${id(offset)}):${result.attempts[0].message};这一篇转机构下载`);
|
|
895
|
-
}
|
|
896
|
-
results[offset] = itemResult(result, offset + 1, request);
|
|
897
|
-
checked = offset + 1;
|
|
898
|
-
if (result.status === "ok") {
|
|
899
|
-
openFound += 1;
|
|
900
|
-
// Its Markdown from the open sources now; the browser's part, if any, in its queue.
|
|
901
|
-
const later = wantsFiles || wantsMarkdown &&
|
|
902
|
-
await fulltextForItem(results[offset], recorded(request, ledger, entries[offset]), searchers, "later") === "later";
|
|
903
|
-
// The window lists its PDF as saved now, as it did (its files' shortfall, if any, later).
|
|
904
|
-
settle(offset);
|
|
905
|
-
if (later) {
|
|
906
|
-
finishing.add(offset);
|
|
907
|
-
queue.push(offset);
|
|
908
|
-
notify();
|
|
909
|
-
}
|
|
910
|
-
else {
|
|
911
|
-
openSettled += 1;
|
|
912
|
-
record(offset);
|
|
913
|
-
}
|
|
914
|
-
}
|
|
915
|
-
else {
|
|
916
|
-
settle(offset, "school");
|
|
917
|
-
if (!missed.length) {
|
|
918
|
-
log(`[批量] 开放来源没有找到 ${id(offset)},机构下载同时开始`);
|
|
919
|
-
ledger?.phase("机构下载开始,开放来源同时进行");
|
|
920
|
-
}
|
|
921
|
-
missed.push(offset);
|
|
922
|
-
queue.push(offset);
|
|
923
|
-
notify();
|
|
924
|
-
}
|
|
925
|
-
await reportProgress(id(offset));
|
|
926
|
-
takeRequests();
|
|
927
|
-
await sendPapers();
|
|
928
|
-
}
|
|
1026
|
+
await Promise.all(Array.from({ length: Math.min(OPEN_AT_ONCE, total) }, async () => {
|
|
1027
|
+
for (let offset = nextOpen++; offset < total && !stopped; offset = nextOpen++)
|
|
1028
|
+
await openItem(offset);
|
|
1029
|
+
}));
|
|
929
1030
|
}
|
|
930
1031
|
finally {
|
|
931
1032
|
openRunning = false;
|
|
@@ -937,11 +1038,57 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
937
1038
|
ledger?.phase(`开放来源查完:${missed.length} 篇转机构下载`);
|
|
938
1039
|
}
|
|
939
1040
|
};
|
|
1041
|
+
/** One paper's open-access check, and where it goes then. */
|
|
1042
|
+
const openItem = async (offset) => {
|
|
1043
|
+
const request = requests[offset];
|
|
1044
|
+
log(`[批量] 开放来源 ${offset + 1}/${total}:${id(offset)}`);
|
|
1045
|
+
const tiers = createTiers(backend).filter((tier) => tier.stage !== "entitled_access" && tier.stage !== "preprint");
|
|
1046
|
+
let result;
|
|
1047
|
+
try {
|
|
1048
|
+
result = await pastNetworkLoss(() => downloadWithFallback(searchers, recorded(request, ledger, entries[offset]), tiers), watch);
|
|
1049
|
+
}
|
|
1050
|
+
catch (error) {
|
|
1051
|
+
result = laneError("open_access", error);
|
|
1052
|
+
log(`[批量] 开放来源出错(${id(offset)}):${result.attempts[0].message};这一篇转机构下载`);
|
|
1053
|
+
}
|
|
1054
|
+
results[offset] = itemResult(result, offset + 1, request);
|
|
1055
|
+
checked += 1;
|
|
1056
|
+
if (result.status === "ok") {
|
|
1057
|
+
openFound += 1;
|
|
1058
|
+
// Its Markdown from the open sources now; the browser's part, if any, in its queue.
|
|
1059
|
+
const later = wantsFiles || wantsMarkdown &&
|
|
1060
|
+
await fulltextForItem(results[offset], recorded(request, ledger, entries[offset]), searchers, "later") === "later";
|
|
1061
|
+
// The window lists its PDF as saved now, as it did (its files' shortfall, if any, later).
|
|
1062
|
+
settle(offset);
|
|
1063
|
+
if (later) {
|
|
1064
|
+
finishing.add(offset);
|
|
1065
|
+
queue.push(offset);
|
|
1066
|
+
notify();
|
|
1067
|
+
}
|
|
1068
|
+
else {
|
|
1069
|
+
openSettled += 1;
|
|
1070
|
+
record(offset);
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
else {
|
|
1074
|
+
settle(offset, "school");
|
|
1075
|
+
if (!missed.length) {
|
|
1076
|
+
log(`[批量] 开放来源没有找到 ${id(offset)},机构下载同时开始`);
|
|
1077
|
+
ledger?.phase("机构下载开始,开放来源同时进行");
|
|
1078
|
+
}
|
|
1079
|
+
missed.push(offset);
|
|
1080
|
+
queue.push(offset);
|
|
1081
|
+
notify();
|
|
1082
|
+
}
|
|
1083
|
+
await reportProgress(id(offset));
|
|
1084
|
+
takeRequests();
|
|
1085
|
+
await sendPapers();
|
|
1086
|
+
};
|
|
940
1087
|
/**
|
|
941
1088
|
* The browser's part of an open-access paper: its supplementary files (the visit also
|
|
942
1089
|
* saves the page when the Markdown is wanted), then its Markdown.
|
|
943
1090
|
*/
|
|
944
|
-
const finishOpenItem = async (offset) => {
|
|
1091
|
+
const finishOpenItem = async (offset, laneBackend = backend) => {
|
|
945
1092
|
finishing.delete(offset);
|
|
946
1093
|
const done = settledOk() + failed;
|
|
947
1094
|
const request = { ...requests[offset], batch: { id: batchId, index: done + 1, total, done, succeeded: settledOk(), failed },
|
|
@@ -953,10 +1100,10 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
953
1100
|
let pageWait;
|
|
954
1101
|
let markdownFrom = item.attempts.length;
|
|
955
1102
|
try {
|
|
956
|
-
extra = await filesPastNetworkLoss(item, recorded(request, ledger, entries[offset]),
|
|
1103
|
+
extra = await filesPastNetworkLoss(item, recorded(request, ledger, entries[offset]), laneBackend, searchers, watch);
|
|
957
1104
|
markdownFrom = item.attempts.length;
|
|
958
1105
|
if (wantsMarkdown && !stoppedByUser(item)) {
|
|
959
|
-
await markdownPastNetworkLoss(item, recorded(request, ledger, entries[offset]), searchers, pageVisit(
|
|
1106
|
+
await markdownPastNetworkLoss(item, recorded(request, ledger, entries[offset]), searchers, pageVisit(laneBackend, request, log, pause, (trace) => { pageWait = trace?.loginWaitPage; learnPublisher(offset, trace?.publisher); }), watch);
|
|
960
1107
|
}
|
|
961
1108
|
}
|
|
962
1109
|
catch (error) {
|
|
@@ -988,6 +1135,13 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
988
1135
|
return;
|
|
989
1136
|
}
|
|
990
1137
|
}
|
|
1138
|
+
// Its Markdown or files without access (partsWithoutAccess): not finished, it waits for the other schools at the end.
|
|
1139
|
+
if (!stopped && partsWithoutAccess(item) && (await otherSchools()).length) {
|
|
1140
|
+
partsYet.add(offset);
|
|
1141
|
+
settle(offset, "school");
|
|
1142
|
+
await sendPapers();
|
|
1143
|
+
return;
|
|
1144
|
+
}
|
|
991
1145
|
openSettled += 1;
|
|
992
1146
|
if (extra)
|
|
993
1147
|
await reportBatchItem(request, extra, backend);
|
|
@@ -1245,6 +1399,11 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
1245
1399
|
const retrying = () => (retryOn ??= retrySetting(backend));
|
|
1246
1400
|
/** Papers without access with the schools tried so far, waiting for the next one. */
|
|
1247
1401
|
const withoutAccessYet = new Set();
|
|
1402
|
+
/**
|
|
1403
|
+
* Open-access papers whose PDF stands, their Markdown or files without access with the schools tried so far
|
|
1404
|
+
* (partsWithoutAccess): waiting for the next one, for those alone.
|
|
1405
|
+
*/
|
|
1406
|
+
const partsYet = new Set();
|
|
1248
1407
|
/** Papers failed for a reason not known, waiting for their one more try at the end. */
|
|
1249
1408
|
const unexplainedYet = new Set();
|
|
1250
1409
|
/** Each waiting paper's schools tried, the default one counted. */
|
|
@@ -1257,9 +1416,10 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
1257
1416
|
const until = [...blockedUntil].filter(([key]) => held.get(key)?.length).map(([, at]) => at);
|
|
1258
1417
|
const dois = (offsets) => [...offsets].map((offset) => requests[offset].doi).filter(Boolean);
|
|
1259
1418
|
const blocked = waiting(true), login = waiting(false);
|
|
1260
|
-
return { noAccess: withoutAccessYet.size, unexplained: unexplainedYet.size, blocked: blocked.length,
|
|
1261
|
-
...(until.length ? { blockedAt: Math.min(...until) } : {}),
|
|
1262
|
-
papers: { noAccess: dois(withoutAccessYet), unexplained: dois(unexplainedYet), blocked: dois(blocked),
|
|
1419
|
+
return { noAccess: withoutAccessYet.size + partsYet.size, unexplained: unexplainedYet.size, blocked: blocked.length,
|
|
1420
|
+
login: login.length, ...(until.length ? { blockedAt: Math.min(...until) } : {}),
|
|
1421
|
+
papers: { noAccess: dois([...withoutAccessYet, ...partsYet]), unexplained: dois(unexplainedYet), blocked: dois(blocked),
|
|
1422
|
+
login: dois(login) } };
|
|
1263
1423
|
};
|
|
1264
1424
|
/** What the window was last told of them. */
|
|
1265
1425
|
let laterSaid = JSON.stringify(later());
|
|
@@ -1282,7 +1442,163 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
1282
1442
|
// 2026-11-21; kept with the switch at the end, user 2026-11-22). Asked until the backend has run a paper and can say.
|
|
1283
1443
|
let groupJournals;
|
|
1284
1444
|
let lastJournal = "";
|
|
1285
|
-
|
|
1445
|
+
/**
|
|
1446
|
+
* One paper's school visit (``offset``, not an open-access paper's browser part: ``finishOpenItem``), its Markdown,
|
|
1447
|
+
* and where it ends: saved, failed, held with its publisher, waiting for the end. ``lane``: a window of a batch whose
|
|
1448
|
+
* papers run at once (并行下载): its backend names the window, and a paper its publisher's hour holds goes back
|
|
1449
|
+
* (``lane.limited``) instead of waiting.
|
|
1450
|
+
*/
|
|
1451
|
+
/** Papers of the school lane being downloaded now (several in a batch whose papers run at once). */
|
|
1452
|
+
let inFlight = 0;
|
|
1453
|
+
const runOne = async (offset, lane, first) => {
|
|
1454
|
+
const laneBackend = lane ? lane.backend : first ?? backend;
|
|
1455
|
+
const request = requests[offset];
|
|
1456
|
+
// Papers waiting for the end (without access, 原因不明) counted too: the log goes 1/7, 2/7 … as the window does
|
|
1457
|
+
// (Windows 2026-11-22: every line said 1/7 while the papers waited); those downloading beside it too (并行下载: four
|
|
1458
|
+
// starting at once each said 2/21, user's Mac 2026-11-29).
|
|
1459
|
+
const step = succeeded + failed + withoutAccessYet.size + unexplainedYet.size + inFlight;
|
|
1460
|
+
log(openRunning
|
|
1461
|
+
? `[批量] 机构下载第 ${step} 篇(开放来源还在查):${id(offset)}`
|
|
1462
|
+
: `[批量] 机构下载 ${step}/${missed.length}:${id(offset)}`);
|
|
1463
|
+
const done = settledOk() + failed;
|
|
1464
|
+
// Its place among the papers gone through, those waiting for the end counted (the window's "k / N" goes on);
|
|
1465
|
+
// the counts are of those finished.
|
|
1466
|
+
const batch = { id: batchId, index: done + withoutAccessYet.size + partsYet.size + unexplainedYet.size + 1, total, done,
|
|
1467
|
+
succeeded: settledOk(), failed };
|
|
1468
|
+
let result;
|
|
1469
|
+
try {
|
|
1470
|
+
// Reported to the window once it is known whether the paper waits for the end (``reportBatchItem`` below).
|
|
1471
|
+
const visit = () => downloadWithFallback(searchers, watchingSchool(recorded({ ...request, batch, ...(away ? { shortUserWait: true } : {}) }, ledger, entries[offset]), offset), createTiers(laneBackend).filter((tier) => tier.stage === "entitled_access" || mode !== "entitled_only" && tier.stage === "preprint"));
|
|
1472
|
+
// A window of a batch whose papers run at once does not wait for its publisher's hour: the paper goes back and
|
|
1473
|
+
// the window takes another publisher (windowPlan.ts); one after another, the batch waits for it here.
|
|
1474
|
+
result = await pastNetworkLoss(() => lane ? visit() : pastHourlyLimit(visit, log, pause, backend), watch);
|
|
1475
|
+
}
|
|
1476
|
+
catch (error) {
|
|
1477
|
+
result = laneError("entitled_access", error);
|
|
1478
|
+
log(`[批量] 机构下载出错(${id(offset)}):${result.attempts[0].message}`);
|
|
1479
|
+
}
|
|
1480
|
+
const hourly = lane ? rateLimitWait(result) : undefined;
|
|
1481
|
+
if (lane && hourly) {
|
|
1482
|
+
lane.limited(offset, hourly.wait, hourly.publisher);
|
|
1483
|
+
return;
|
|
1484
|
+
}
|
|
1485
|
+
if (result.status === "ok" && result.version === "preprint")
|
|
1486
|
+
preprints += 1;
|
|
1487
|
+
if (!away && waitedOutOnUser(result)) {
|
|
1488
|
+
away = true;
|
|
1489
|
+
log(AWAY_NOTICE);
|
|
1490
|
+
}
|
|
1491
|
+
const open = results[offset];
|
|
1492
|
+
results[offset] = itemResult({
|
|
1493
|
+
...result,
|
|
1494
|
+
attempts: [...open.attempts, ...result.attempts],
|
|
1495
|
+
...(result.review ?? open.review ? { review: result.review ?? open.review } : {}),
|
|
1496
|
+
}, offset + 1, request);
|
|
1497
|
+
const school = result.attempts.find((attempt) => attempt.stage === "entitled_access");
|
|
1498
|
+
const holding = result.status !== "ok" && wentThrough(offset) && school?.code === LOGIN_LAPSED_CODE
|
|
1499
|
+
&& waitEnded.get(offset) === SCHOOL_SIGN_IN_PAGE && !stoppedByUser(result) && await canHold() && !heldAgain(offset);
|
|
1500
|
+
// Its publisher's site blocked it: it waits with the publisher's other papers, once (BLOCKED_PAUSE_MS).
|
|
1501
|
+
const blocked = !holding && siteBlocked(result);
|
|
1502
|
+
const pausing = blocked && !triedAgain.has(publisherOf(offset)) && await retrying();
|
|
1503
|
+
if (!blocked)
|
|
1504
|
+
blockedInRow.delete(publisherOf(offset));
|
|
1505
|
+
// Its Markdown now, from the page this visit saved, or the open sources (only these
|
|
1506
|
+
// while its school login is in doubt or its site blocks); a paused paper's when it is tried again.
|
|
1507
|
+
if (wantsMarkdown && !stoppedByUser(result) && !pausing) {
|
|
1508
|
+
await markdownPastNetworkLoss(results[offset], recorded({ ...request, batch }, ledger, entries[offset]), searchers, holding || blocked ? undefined
|
|
1509
|
+
: pageVisit(laneBackend, { ...request, batch, ...(away ? { shortUserWait: true } : {}) }, log, pause), watch);
|
|
1510
|
+
}
|
|
1511
|
+
if (result.status === "ok") {
|
|
1512
|
+
succeeded += 1;
|
|
1513
|
+
record(offset);
|
|
1514
|
+
settle(offset);
|
|
1515
|
+
if (school?.status === "ok")
|
|
1516
|
+
cameThrough(offset);
|
|
1517
|
+
// Saved, its files refused as too many requests (BMJ Open's, 2026-11-23, the paper before gut.bmj.com's check):
|
|
1518
|
+
// the publisher's other papers wait all the same.
|
|
1519
|
+
const key = publisherOf(offset);
|
|
1520
|
+
if (result.supplements?.some((file) => file.reason === "http_429") && !blockedUntil.has(key) && !triedAgain.has(key)) {
|
|
1521
|
+
// Without 失败后自动重试: no pause; one of BLOCKED_IN_ROW in a row (blockedOnce).
|
|
1522
|
+
if (await retrying())
|
|
1523
|
+
pauseBlocked(key, `${id(offset)} 的附录:请求太多`);
|
|
1524
|
+
else
|
|
1525
|
+
blockedOnce(offset);
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
else if (holding) {
|
|
1529
|
+
hold(offset);
|
|
1530
|
+
}
|
|
1531
|
+
else if (pausing) {
|
|
1532
|
+
pauseBlocked(publisherOf(offset), `${id(offset)}:人机验证没过或拦截页`, offset);
|
|
1533
|
+
}
|
|
1534
|
+
else if (blocked) {
|
|
1535
|
+
// Blocked again after its pause: this one fails 被网站拦截, its publisher's others are not tried. Without 失败后自动
|
|
1536
|
+
// 重试: this one fails, its publisher's others go on until BLOCKED_IN_ROW in a row (blockedOnce).
|
|
1537
|
+
if (triedAgain.has(publisherOf(offset)))
|
|
1538
|
+
giveUp(offset);
|
|
1539
|
+
else
|
|
1540
|
+
blockedOnce(offset);
|
|
1541
|
+
failed += 1;
|
|
1542
|
+
record(offset);
|
|
1543
|
+
settle(offset);
|
|
1544
|
+
}
|
|
1545
|
+
else if (withoutAccess(result) && !stoppedByUser(result) && (await otherSchools()).length) {
|
|
1546
|
+
withoutAccessYet.add(offset);
|
|
1547
|
+
settle(offset, "school");
|
|
1548
|
+
}
|
|
1549
|
+
else if (unexplainedFailure(result) && await retrying()) {
|
|
1550
|
+
unexplainedYet.add(offset);
|
|
1551
|
+
settle(offset, "school");
|
|
1552
|
+
}
|
|
1553
|
+
else {
|
|
1554
|
+
failed += 1;
|
|
1555
|
+
record(offset);
|
|
1556
|
+
settle(offset);
|
|
1557
|
+
}
|
|
1558
|
+
// The window's line, not for a paper that waits for the end or its publisher's pause (it would close the batch
|
|
1559
|
+
// early when last).
|
|
1560
|
+
if (!withoutAccessYet.has(offset) && !unexplainedYet.has(offset) && !pausing) {
|
|
1561
|
+
await reportBatchItem({ ...request, batch: { ...batch, index: batch.done + 1 } }, result, backend);
|
|
1562
|
+
}
|
|
1563
|
+
if (stoppedByUser(result)) {
|
|
1564
|
+
stopped = true;
|
|
1565
|
+
return;
|
|
1566
|
+
}
|
|
1567
|
+
// More to come: the lanes line goes on (after the last item the window has its end).
|
|
1568
|
+
if (openRunning || queue.length || lane)
|
|
1569
|
+
await reportProgress("");
|
|
1570
|
+
if (held.size)
|
|
1571
|
+
await reportHeld();
|
|
1572
|
+
takeRequests();
|
|
1573
|
+
await sendPapers();
|
|
1574
|
+
};
|
|
1575
|
+
const runEntitled = async (...args) => {
|
|
1576
|
+
inFlight += 1;
|
|
1577
|
+
try {
|
|
1578
|
+
return await runOne(...args);
|
|
1579
|
+
}
|
|
1580
|
+
finally {
|
|
1581
|
+
inFlight -= 1;
|
|
1582
|
+
}
|
|
1583
|
+
};
|
|
1584
|
+
/**
|
|
1585
|
+
* The batch's backend for a paper in window ``window`` (并行下载): its downloads name the window, and whether the batch
|
|
1586
|
+
* runs its papers at once (``parallel``: the first paper, before the batch knows, does not say).
|
|
1587
|
+
*/
|
|
1588
|
+
const windowBackend = (window, parallel = true) => backend && new Proxy(backend, {
|
|
1589
|
+
get(target, name) {
|
|
1590
|
+
if (name === "downloadLegalDoi") {
|
|
1591
|
+
return (request) => target.downloadLegalDoi({ ...request, window, ...(parallel ? { parallel: true } : {}) });
|
|
1592
|
+
}
|
|
1593
|
+
const value = Reflect.get(target, name, target);
|
|
1594
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
1595
|
+
},
|
|
1596
|
+
});
|
|
1597
|
+
/**
|
|
1598
|
+
* ``first``: the lane's first paper goes through it (window 0, the batch not knowing yet whether it runs papers at
|
|
1599
|
+
* once), ``taken`` told which paper that is.
|
|
1600
|
+
*/
|
|
1601
|
+
const entitledLane = async (first, taken) => {
|
|
1286
1602
|
for (;;) {
|
|
1287
1603
|
while (!queue.length && openRunning && !stopped)
|
|
1288
1604
|
await new Promise((resume) => { wake = resume; });
|
|
@@ -1311,126 +1627,195 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
1311
1627
|
continue;
|
|
1312
1628
|
}
|
|
1313
1629
|
await reportLater();
|
|
1630
|
+
taken?.(offset);
|
|
1314
1631
|
if (finishing.has(offset)) {
|
|
1315
|
-
await finishOpenItem(offset);
|
|
1632
|
+
await finishOpenItem(offset, first ?? backend);
|
|
1316
1633
|
if (stopped)
|
|
1317
1634
|
return;
|
|
1635
|
+
if (first)
|
|
1636
|
+
return;
|
|
1318
1637
|
if (held.size)
|
|
1319
1638
|
await reportHeld();
|
|
1320
1639
|
takeRequests();
|
|
1321
1640
|
continue;
|
|
1322
1641
|
}
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1642
|
+
await runEntitled(offset, undefined, first);
|
|
1643
|
+
if (stopped)
|
|
1644
|
+
return;
|
|
1645
|
+
if (first)
|
|
1646
|
+
return; // the first paper alone: the batch asks for 同时下载 now (``lane``)
|
|
1647
|
+
}
|
|
1648
|
+
};
|
|
1649
|
+
/**
|
|
1650
|
+
* 并行下载 (user, 2026-11-29): the browser lane with several papers at once, each in a window of its own of the one
|
|
1651
|
+
* browser, as many and of which publishers as windowPlan.ts says. A window set free takes the next paper at once;
|
|
1652
|
+
* one no longer needed closes. Each paper goes as one after another would (``runEntitled``, ``finishOpenItem``), but
|
|
1653
|
+
* one its publisher's hour holds goes back and the window takes another publisher; only when nothing else is left
|
|
1654
|
+
* does the batch wait for that hour.
|
|
1655
|
+
*/
|
|
1656
|
+
const parallelLane = async (setting, publishersOf, first) => {
|
|
1657
|
+
const keyOf = (offset) => publisherKey(requests[offset].doi, publishersOf) || `paper:${offset}`;
|
|
1658
|
+
/** Each publisher's last journal taken, for one journal's papers one after another (``groupJournals``). */
|
|
1659
|
+
const lastOf = new Map();
|
|
1660
|
+
const plan = new WindowPlan(setting, keyOf, now, (waiting, key) => {
|
|
1661
|
+
if (!groupJournals)
|
|
1662
|
+
return 0;
|
|
1663
|
+
const at = waiting.findIndex((offset) => journalOfDoi(requests[offset].doi) === lastOf.get(key));
|
|
1664
|
+
return at < 0 ? 0 : at;
|
|
1665
|
+
});
|
|
1666
|
+
/** Papers that went to a window before: one coming back (its publisher's pause over, 登录并继续) goes first. */
|
|
1667
|
+
const dispatched = new Set();
|
|
1668
|
+
/** Each publisher's name as its hour was counted (rate_limited's hourlyPublisher), for its wait. */
|
|
1669
|
+
const hourlyName = new Map();
|
|
1670
|
+
/** Windows downloading now, each its paper's run; the windows opened (closed again when no longer needed). */
|
|
1671
|
+
const running = new Map();
|
|
1672
|
+
const opened = new Set([0]);
|
|
1673
|
+
const limited = (offset, waitSec, publisher) => {
|
|
1674
|
+
const key = keyOf(offset);
|
|
1675
|
+
if (publisher)
|
|
1676
|
+
hourlyName.set(key, publisher);
|
|
1677
|
+
plan.limit(key, now() + waitSec * 1000);
|
|
1678
|
+
plan.add(offset, true);
|
|
1679
|
+
log(`[批量] ${publisher || key} 一小时内的下载已到上限(防止学校被暂停访问),${clockTime(now() + waitSec * 1000)} 前改下其他出版社`);
|
|
1680
|
+
};
|
|
1681
|
+
/** The papers that came in (the open-access lane's, those going on again) join the plan. */
|
|
1682
|
+
const feed = () => {
|
|
1683
|
+
while (queue.length) {
|
|
1684
|
+
const offset = queue.shift();
|
|
1685
|
+
plan.add(offset, dispatched.has(offset));
|
|
1686
|
+
}
|
|
1687
|
+
};
|
|
1688
|
+
/** After a paper: the window's counts and lines (the backend counts none of a window's papers itself). */
|
|
1689
|
+
const reportCounts = () => {
|
|
1690
|
+
if (!backend?.reportBatchProgress || settledOk() + failed >= total)
|
|
1691
|
+
return reports;
|
|
1692
|
+
const update = withLater({ id: batchId, total, done: settledOk() + failed, succeeded: settledOk(), failed, current: "",
|
|
1693
|
+
finished: false, lanes: lanes() });
|
|
1694
|
+
reports = reports.then(() => backend.reportBatchProgress(update)).catch(() => false);
|
|
1695
|
+
return reports;
|
|
1696
|
+
};
|
|
1697
|
+
/** One paper in ``window``: the paper's run, then the window is free for the next. */
|
|
1698
|
+
const inWindow = (window, offset, key) => (async () => {
|
|
1699
|
+
opened.add(window);
|
|
1700
|
+
dispatched.add(offset);
|
|
1701
|
+
lastOf.set(key, journalOfDoi(requests[offset].doi));
|
|
1336
1702
|
try {
|
|
1337
|
-
|
|
1338
|
-
|
|
1703
|
+
await reportLater();
|
|
1704
|
+
if (finishing.has(offset))
|
|
1705
|
+
await finishOpenItem(offset, windowBackend(window));
|
|
1706
|
+
else
|
|
1707
|
+
await runEntitled(offset, { backend: windowBackend(window), limited });
|
|
1339
1708
|
}
|
|
1340
1709
|
catch (error) {
|
|
1341
|
-
|
|
1342
|
-
log(`[批量] 机构下载出错(${id(offset)}):${result.attempts[0].message}`);
|
|
1343
|
-
}
|
|
1344
|
-
if (result.status === "ok" && result.version === "preprint")
|
|
1345
|
-
preprints += 1;
|
|
1346
|
-
if (!away && waitedOutOnUser(result)) {
|
|
1347
|
-
away = true;
|
|
1348
|
-
log(AWAY_NOTICE);
|
|
1710
|
+
log(`[批量] 机构下载出错(${id(offset)}):${error instanceof Error ? error.message : String(error)}`);
|
|
1349
1711
|
}
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
const holding = result.status !== "ok" && wentThrough(offset) && school?.code === LOGIN_LAPSED_CODE
|
|
1358
|
-
&& waitEnded.get(offset) === SCHOOL_SIGN_IN_PAGE && !stoppedByUser(result) && await canHold() && !heldAgain(offset);
|
|
1359
|
-
// Its publisher's site blocked it: it waits with the publisher's other papers, once (BLOCKED_PAUSE_MS).
|
|
1360
|
-
const blocked = !holding && siteBlocked(result);
|
|
1361
|
-
const pausing = blocked && !triedAgain.has(publisherOf(offset)) && await retrying();
|
|
1362
|
-
if (!blocked)
|
|
1363
|
-
blockedInRow.delete(publisherOf(offset));
|
|
1364
|
-
// Its Markdown now, from the page this visit saved, or the open sources (only these
|
|
1365
|
-
// while its school login is in doubt or its site blocks); a paused paper's when it is tried again.
|
|
1366
|
-
if (wantsMarkdown && !stoppedByUser(result) && !pausing) {
|
|
1367
|
-
await markdownPastNetworkLoss(results[offset], recorded({ ...request, batch }, ledger, entries[offset]), searchers, holding || blocked ? undefined
|
|
1368
|
-
: pageVisit(backend, { ...request, batch, ...(away ? { shortUserWait: true } : {}) }, log, pause), watch);
|
|
1369
|
-
}
|
|
1370
|
-
if (result.status === "ok") {
|
|
1371
|
-
succeeded += 1;
|
|
1372
|
-
record(offset);
|
|
1373
|
-
settle(offset);
|
|
1374
|
-
if (school?.status === "ok")
|
|
1375
|
-
cameThrough(offset);
|
|
1376
|
-
// Saved, its files refused as too many requests (BMJ Open's, 2026-11-23, the paper before gut.bmj.com's check):
|
|
1377
|
-
// the publisher's other papers wait all the same.
|
|
1378
|
-
const key = publisherOf(offset);
|
|
1379
|
-
if (result.supplements?.some((file) => file.reason === "http_429") && !blockedUntil.has(key) && !triedAgain.has(key)) {
|
|
1380
|
-
// Without 失败后自动重试: no pause; one of BLOCKED_IN_ROW in a row (blockedOnce).
|
|
1381
|
-
if (await retrying())
|
|
1382
|
-
pauseBlocked(key, `${id(offset)} 的附录:请求太多`);
|
|
1383
|
-
else
|
|
1384
|
-
blockedOnce(offset);
|
|
1385
|
-
}
|
|
1712
|
+
finally {
|
|
1713
|
+
plan.done(key);
|
|
1714
|
+
running.delete(window);
|
|
1715
|
+
await reportCounts();
|
|
1716
|
+
if (held.size)
|
|
1717
|
+
await reportHeld();
|
|
1718
|
+
notify();
|
|
1386
1719
|
}
|
|
1387
|
-
|
|
1388
|
-
|
|
1720
|
+
})();
|
|
1721
|
+
/** Windows no longer needed close (the first stays: it holds the browser). */
|
|
1722
|
+
const closeIdle = () => {
|
|
1723
|
+
const keep = Math.max(1, plan.target());
|
|
1724
|
+
for (const window of [...opened].sort((a, b) => b - a)) {
|
|
1725
|
+
if (opened.size <= keep)
|
|
1726
|
+
break;
|
|
1727
|
+
if (window === 0 || running.has(window))
|
|
1728
|
+
continue;
|
|
1729
|
+
opened.delete(window);
|
|
1730
|
+
void backend?.closeWindow?.(window);
|
|
1389
1731
|
}
|
|
1390
|
-
|
|
1391
|
-
|
|
1732
|
+
};
|
|
1733
|
+
/** A paper the plan gave a window that needs none: its publisher held or given up. True when it was one. */
|
|
1734
|
+
const noWindow = async (offset, key) => {
|
|
1735
|
+
if (held.has(publisherOf(offset))) {
|
|
1736
|
+
held.get(publisherOf(offset)).push(offset);
|
|
1737
|
+
plan.done(key);
|
|
1738
|
+
return true;
|
|
1392
1739
|
}
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
giveUp(offset);
|
|
1740
|
+
if (givenUp.has(publisherOf(offset))) {
|
|
1741
|
+
untried(offset, "blocked_paused");
|
|
1742
|
+
if (finishing.delete(offset))
|
|
1743
|
+
openSettled += 1;
|
|
1398
1744
|
else
|
|
1399
|
-
|
|
1400
|
-
failed += 1;
|
|
1401
|
-
record(offset);
|
|
1402
|
-
settle(offset);
|
|
1403
|
-
}
|
|
1404
|
-
else if (withoutAccess(result) && !stoppedByUser(result) && (await otherSchools()).length) {
|
|
1405
|
-
withoutAccessYet.add(offset);
|
|
1406
|
-
settle(offset, "school");
|
|
1407
|
-
}
|
|
1408
|
-
else if (unexplainedFailure(result) && await retrying()) {
|
|
1409
|
-
unexplainedYet.add(offset);
|
|
1410
|
-
settle(offset, "school");
|
|
1411
|
-
}
|
|
1412
|
-
else {
|
|
1413
|
-
failed += 1;
|
|
1745
|
+
failed += 1;
|
|
1414
1746
|
record(offset);
|
|
1415
1747
|
settle(offset);
|
|
1748
|
+
plan.done(key);
|
|
1749
|
+
await sendPapers();
|
|
1750
|
+
return true;
|
|
1416
1751
|
}
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1752
|
+
return false;
|
|
1753
|
+
};
|
|
1754
|
+
if (first) {
|
|
1755
|
+
// The batch's first paper, still downloading in window 0: its publisher has that window.
|
|
1756
|
+
const key = keyOf(first.offset);
|
|
1757
|
+
plan.begin(key);
|
|
1758
|
+
dispatched.add(first.offset);
|
|
1759
|
+
lastOf.set(key, journalOfDoi(requests[first.offset].doi));
|
|
1760
|
+
running.set(0, first.run.catch(() => undefined).finally(async () => {
|
|
1761
|
+
plan.done(key);
|
|
1762
|
+
running.delete(0);
|
|
1763
|
+
await reportCounts();
|
|
1764
|
+
notify();
|
|
1765
|
+
}));
|
|
1766
|
+
}
|
|
1767
|
+
for (;;) {
|
|
1768
|
+
takeRequests();
|
|
1769
|
+
feed();
|
|
1770
|
+
if (!stopped) {
|
|
1771
|
+
for (let next = plan.take(running.size); next; next = plan.take(running.size)) {
|
|
1772
|
+
if (await noWindow(next.item, next.key))
|
|
1773
|
+
continue;
|
|
1774
|
+
let window = 0;
|
|
1775
|
+
while (running.has(window))
|
|
1776
|
+
window += 1;
|
|
1777
|
+
running.set(window, inWindow(window, next.item, next.key));
|
|
1778
|
+
}
|
|
1421
1779
|
}
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1780
|
+
closeIdle();
|
|
1781
|
+
if (!running.size) {
|
|
1782
|
+
if (stopped || (!plan.waiting && !queue.length && !openRunning))
|
|
1783
|
+
break;
|
|
1784
|
+
if (queue.length)
|
|
1785
|
+
continue; // came in meanwhile
|
|
1786
|
+
const hour = plan.hourlyWait();
|
|
1787
|
+
if (hour) {
|
|
1788
|
+
// Only that publisher is left: the batch waits for its hour, going on at once when the setting lets it
|
|
1789
|
+
// or another paper comes in.
|
|
1790
|
+
const name = hourlyName.get(hour.key) ?? hour.key;
|
|
1791
|
+
log(`[批量] 只剩 ${name} 的篇目,一小时内的下载已到上限,${clockTime(hour.until)} 继续`);
|
|
1792
|
+
let sinceKeep = 0;
|
|
1793
|
+
let over = false;
|
|
1794
|
+
while (!over && !queue.length && !stopped) {
|
|
1795
|
+
const step = Math.min(HOURLY_RECHECK_EVERY_MS, Math.max(1, hour.until - now()));
|
|
1796
|
+
await pause(step);
|
|
1797
|
+
sinceKeep += step;
|
|
1798
|
+
const asked = backend?.hourlyWait ? await backend.hourlyWait(name).catch(() => undefined) : undefined;
|
|
1799
|
+
over = now() >= hour.until || asked?.waitSec === 0;
|
|
1800
|
+
if (sinceKeep >= KEEP_SESSION_EVERY_MS) {
|
|
1801
|
+
sinceKeep = 0;
|
|
1802
|
+
await backend?.keepSession?.().catch(() => false);
|
|
1803
|
+
}
|
|
1804
|
+
takeRequests();
|
|
1805
|
+
}
|
|
1806
|
+
// The hour over, or the setting lets it go on now; a paper come in meanwhile goes first, the limit kept.
|
|
1807
|
+
if (over)
|
|
1808
|
+
plan.lift(hour.key);
|
|
1809
|
+
continue;
|
|
1810
|
+
}
|
|
1425
1811
|
}
|
|
1426
|
-
|
|
1427
|
-
if (openRunning || queue.length)
|
|
1428
|
-
await reportProgress("");
|
|
1429
|
-
if (held.size)
|
|
1430
|
-
await reportHeld();
|
|
1431
|
-
takeRequests();
|
|
1432
|
-
await sendPapers();
|
|
1812
|
+
await new Promise((resume) => { wake = resume; });
|
|
1433
1813
|
}
|
|
1814
|
+
await Promise.all(running.values());
|
|
1815
|
+
// Stopped: the papers no window took are as the lane left them (not reached).
|
|
1816
|
+
for (const offset of plan.waitingItems())
|
|
1817
|
+
if (!queue.includes(offset))
|
|
1818
|
+
queue.push(offset);
|
|
1434
1819
|
};
|
|
1435
1820
|
if (mode === "entitled_only") {
|
|
1436
1821
|
for (const [offset, request] of requests.entries()) {
|
|
@@ -1444,7 +1829,59 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
1444
1829
|
else
|
|
1445
1830
|
ledger?.phase(`开放来源 ${total} 篇`);
|
|
1446
1831
|
void sendPapers();
|
|
1447
|
-
|
|
1832
|
+
// 同时下载 (the settings' 下载 page), asked once the browser has a first paper: its papers at once when the backend
|
|
1833
|
+
// can and the user set it, else one after another.
|
|
1834
|
+
let parallel;
|
|
1835
|
+
/** 同时下载 as the batch's backend says it, once it can (a backend process the batch has); undefined until then. */
|
|
1836
|
+
const askParallel = async () => {
|
|
1837
|
+
const said = backend?.parallelSetting ? await backend.parallelSetting().catch(() => undefined) : undefined;
|
|
1838
|
+
if (said === undefined)
|
|
1839
|
+
return;
|
|
1840
|
+
parallel = isParallel(said) ? said : null;
|
|
1841
|
+
if (parallel) {
|
|
1842
|
+
log(`[批量] 同时下载:不同出版社 ${parallel.publishers} 家 × 同一出版社 ${parallel.perPublisher} 篇`);
|
|
1843
|
+
ledger?.phase(`同时下载:${parallel.publishers} 家 × ${parallel.perPublisher} 篇`);
|
|
1844
|
+
}
|
|
1845
|
+
};
|
|
1846
|
+
const lane = async () => {
|
|
1847
|
+
while (!queue.length && openRunning && !stopped)
|
|
1848
|
+
await new Promise((resume) => { wake = resume; });
|
|
1849
|
+
if (!queue.length || stopped)
|
|
1850
|
+
return;
|
|
1851
|
+
if (parallel === undefined)
|
|
1852
|
+
await askParallel();
|
|
1853
|
+
if (parallel === undefined && backend?.parallelSetting) {
|
|
1854
|
+
// No backend process to ask yet (none is started for it): the first paper goes in window 0 and starts it, the
|
|
1855
|
+
// backend launching the browser as its own settings say. As soon as the backend is up the batch asks, and papers
|
|
1856
|
+
// at once open the other windows beside the first while it goes on (user, 2026-11-29: "为什么只有一篇在开始呢?",
|
|
1857
|
+
// they waited for it to end, 1 min 45 s on a school sign-in); one after another, the lane goes on after it. The
|
|
1858
|
+
// school's sign-in asks the user in one window at a time whatever the windows (user_turn.py).
|
|
1859
|
+
let firstOffset;
|
|
1860
|
+
let firstDone = false;
|
|
1861
|
+
const firstRun = entitledLane(windowBackend(0, false), (offset) => { firstOffset = offset; })
|
|
1862
|
+
.finally(() => { firstDone = true; });
|
|
1863
|
+
// ``parallel`` as askParallel left it (set there, not here).
|
|
1864
|
+
const said = () => parallel;
|
|
1865
|
+
while (!firstDone && said() === undefined && !stopped) {
|
|
1866
|
+
await Promise.race([firstRun.catch(() => undefined), pause(PARALLEL_ASK_EVERY_MS)]);
|
|
1867
|
+
if (!firstDone)
|
|
1868
|
+
await askParallel();
|
|
1869
|
+
}
|
|
1870
|
+
const setting = said();
|
|
1871
|
+
if (setting && !firstDone && firstOffset !== undefined) {
|
|
1872
|
+
return parallelLane(setting, setting.prefixes, { offset: firstOffset, run: firstRun });
|
|
1873
|
+
}
|
|
1874
|
+
await firstRun;
|
|
1875
|
+
if (stopped)
|
|
1876
|
+
return;
|
|
1877
|
+
if (parallel === undefined)
|
|
1878
|
+
await askParallel();
|
|
1879
|
+
parallel ??= null;
|
|
1880
|
+
return lane();
|
|
1881
|
+
}
|
|
1882
|
+
return parallel ? parallelLane(parallel, parallel.prefixes) : entitledLane();
|
|
1883
|
+
};
|
|
1884
|
+
await Promise.all([mode === "entitled_only" ? undefined : openLane(), lane()]);
|
|
1448
1885
|
// The held publishers wait for the user as long as it takes (user, 2026-11-15): the
|
|
1449
1886
|
// window shows 登录并继续 and 终止. 登录并继续 downloads them, the school's login asking
|
|
1450
1887
|
// the user where it lapsed; held again, the wait starts over. 终止 ends them as paused.
|
|
@@ -1467,12 +1904,13 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
1467
1904
|
await pause(HOLD_POLL_MS);
|
|
1468
1905
|
takeRequests();
|
|
1469
1906
|
if (queue.length && !stopped) {
|
|
1470
|
-
await
|
|
1907
|
+
await lane();
|
|
1471
1908
|
}
|
|
1472
1909
|
dropEmptyPauses();
|
|
1473
1910
|
}
|
|
1474
1911
|
/** A paper's school stage once more, with another saved school when given; merged into its result. */
|
|
1475
|
-
const visitAgain = async (offset, school) => {
|
|
1912
|
+
const visitAgain = async (offset, school, lane) => {
|
|
1913
|
+
const visitBackend = lane ?? backend;
|
|
1476
1914
|
await reportLater();
|
|
1477
1915
|
const request = { ...requests[offset], ...(school ? { school: school.entityId } : {}) };
|
|
1478
1916
|
const done = settledOk() + failed;
|
|
@@ -1480,7 +1918,7 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
1480
1918
|
log(school ? `[批量] 换${school.name}:${id(offset)}` : `[批量] 再试一次:${id(offset)}`);
|
|
1481
1919
|
let result;
|
|
1482
1920
|
try {
|
|
1483
|
-
result = await pastNetworkLoss(() => pastHourlyLimit(() => downloadWithFallback(searchers, watchingSchool(recorded({ ...request, batch, ...(away ? { shortUserWait: true } : {}) }, ledger, entries[offset]), offset), createTiers(
|
|
1921
|
+
result = await pastNetworkLoss(() => pastHourlyLimit(() => downloadWithFallback(searchers, watchingSchool(recorded({ ...request, batch, ...(away ? { shortUserWait: true } : {}) }, ledger, entries[offset]), offset), createTiers(visitBackend).filter((tier) => tier.stage === "entitled_access")), log, pause, backend), watch);
|
|
1484
1922
|
}
|
|
1485
1923
|
catch (error) {
|
|
1486
1924
|
result = laneError("entitled_access", error);
|
|
@@ -1497,7 +1935,7 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
1497
1935
|
const item = results[offset];
|
|
1498
1936
|
// Its Markdown from the page this visit saved, unless the open sources gave it already.
|
|
1499
1937
|
if (wantsMarkdown && result.status === "ok" && item.markdown?.status !== "ok") {
|
|
1500
|
-
await markdownPastNetworkLoss(item, recorded({ ...request, batch }, ledger, entries[offset]), searchers, pageVisit(
|
|
1938
|
+
await markdownPastNetworkLoss(item, recorded({ ...request, batch }, ledger, entries[offset]), searchers, pageVisit(visitBackend, { ...request, batch, ...(away ? { shortUserWait: true } : {}) }, log, pause), watch);
|
|
1501
1939
|
}
|
|
1502
1940
|
if (stoppedByUser(result))
|
|
1503
1941
|
stopped = true;
|
|
@@ -1513,18 +1951,62 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
1513
1951
|
const done = settledOk() + failed;
|
|
1514
1952
|
await reportBatchItem({ ...requests[offset], batch: { id: batchId, index: done + 1, total, done, succeeded: settledOk(), failed } }, results[offset], backend);
|
|
1515
1953
|
};
|
|
1954
|
+
/**
|
|
1955
|
+
* The batch's end (原因不明 once more, the papers without access with another school): in windows by the lane's
|
|
1956
|
+
* rules when its papers ran at once (user, 2026-11-29: "重试的内容不会继续并行吗?", they went one after another),
|
|
1957
|
+
* else one after another. ``anotherSchool``: the first paper alone in window 0, the others after it: going to another
|
|
1958
|
+
* school starts the browser again (the last school's sign-in forgotten), and a paper beginning meanwhile in another
|
|
1959
|
+
* window lost its browser (user's Mac, 2026-11-29: 浏览器被关闭, 2.5 s into it, in both rounds).
|
|
1960
|
+
*/
|
|
1961
|
+
const atTheEnd = async (offsets, visit, anotherSchool = false) => {
|
|
1962
|
+
if (!parallel) {
|
|
1963
|
+
for (const offset of offsets) {
|
|
1964
|
+
if (stopped)
|
|
1965
|
+
return;
|
|
1966
|
+
await visit(offset);
|
|
1967
|
+
}
|
|
1968
|
+
return;
|
|
1969
|
+
}
|
|
1970
|
+
if (anotherSchool && offsets.length > 1) {
|
|
1971
|
+
await visit(offsets[0], windowBackend(0)).catch((error) => {
|
|
1972
|
+
log(`[批量] 机构下载出错(${id(offsets[0])}):${error instanceof Error ? error.message : String(error)}`);
|
|
1973
|
+
});
|
|
1974
|
+
offsets = offsets.slice(1);
|
|
1975
|
+
}
|
|
1976
|
+
const prefixes = parallel.prefixes;
|
|
1977
|
+
const plan = new WindowPlan(parallel, (offset) => publisherKey(requests[offset].doi, prefixes) || `paper:${offset}`, now);
|
|
1978
|
+
for (const offset of offsets)
|
|
1979
|
+
plan.add(offset);
|
|
1980
|
+
const running = new Map();
|
|
1981
|
+
while (!stopped) {
|
|
1982
|
+
for (let next = plan.take(running.size); next && !stopped; next = plan.take(running.size)) {
|
|
1983
|
+
const { item, key } = next;
|
|
1984
|
+
let window = 0;
|
|
1985
|
+
while (running.has(window))
|
|
1986
|
+
window += 1;
|
|
1987
|
+
running.set(window, visit(item, windowBackend(window)).catch((error) => {
|
|
1988
|
+
log(`[批量] 机构下载出错(${id(item)}):${error instanceof Error ? error.message : String(error)}`);
|
|
1989
|
+
}).finally(() => {
|
|
1990
|
+
plan.done(key);
|
|
1991
|
+
running.delete(window);
|
|
1992
|
+
}));
|
|
1993
|
+
}
|
|
1994
|
+
if (!running.size)
|
|
1995
|
+
break;
|
|
1996
|
+
await Promise.race(running.values());
|
|
1997
|
+
}
|
|
1998
|
+
await Promise.all(running.values());
|
|
1999
|
+
};
|
|
1516
2000
|
// 原因不明 once more, the default school still signed in, before any other school (user, 2026-11-22). Not where the
|
|
1517
2001
|
// site still blocks: those end as they were.
|
|
1518
2002
|
const unexplainedAgain = [...unexplainedYet].filter(visitable);
|
|
1519
2003
|
if (unexplainedAgain.length && !stopped) {
|
|
1520
2004
|
log(`[批量] 原因不明的 ${unexplainedAgain.length} 篇再试一次`);
|
|
1521
2005
|
ledger?.phase(`原因不明的 ${unexplainedAgain.length} 篇再试一次`);
|
|
1522
|
-
|
|
1523
|
-
if (stopped)
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
continue;
|
|
1527
|
-
const item = await visitAgain(offset);
|
|
2006
|
+
await atTheEnd(unexplainedAgain, async (offset, lane) => {
|
|
2007
|
+
if (stopped || !visitable(offset))
|
|
2008
|
+
return;
|
|
2009
|
+
const item = await visitAgain(offset, undefined, lane);
|
|
1528
2010
|
unexplainedYet.delete(offset);
|
|
1529
2011
|
if (!stopped && item.status !== "ok" && withoutAccess(item) && (await otherSchools()).length) {
|
|
1530
2012
|
withoutAccessYet.add(offset);
|
|
@@ -1539,7 +2021,7 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
1539
2021
|
settle(offset);
|
|
1540
2022
|
}
|
|
1541
2023
|
await sendPapers();
|
|
1542
|
-
}
|
|
2024
|
+
});
|
|
1543
2025
|
}
|
|
1544
2026
|
for (const offset of unexplainedYet) {
|
|
1545
2027
|
await reportSettled(offset);
|
|
@@ -1547,12 +2029,42 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
1547
2029
|
record(offset);
|
|
1548
2030
|
}
|
|
1549
2031
|
unexplainedYet.clear();
|
|
2032
|
+
/** An open-access paper's Markdown or files done, at the end: its PDF stood all along. */
|
|
2033
|
+
const partsSettled = async (offset) => {
|
|
2034
|
+
await reportSettled(offset);
|
|
2035
|
+
openSettled += 1;
|
|
2036
|
+
record(offset);
|
|
2037
|
+
settle(offset);
|
|
2038
|
+
};
|
|
2039
|
+
/** A paper waiting for its Markdown or files (``partsYet``) with ``school``: those alone, its PDF standing. */
|
|
2040
|
+
const partsWithSchool = async (offset, school, lane) => {
|
|
2041
|
+
const visitBackend = lane ?? backend;
|
|
2042
|
+
await reportLater();
|
|
2043
|
+
const item = results[offset];
|
|
2044
|
+
const done = settledOk() + failed;
|
|
2045
|
+
const request = { ...requests[offset], school: school.entityId,
|
|
2046
|
+
batch: { id: batchId, index: done + 1, total, done, succeeded: settledOk(), failed }, ...(away ? { shortUserWait: true } : {}) };
|
|
2047
|
+
log(`[批量] 换${school.name}补${partsNamed(item)}:${id(offset)}`);
|
|
2048
|
+
await partsAgain(item, recorded(request, ledger, entries[offset]), visitBackend, searchers, pageVisit(visitBackend, request, log, pause, (trace) => learnPublisher(offset, trace?.publisher)), watch);
|
|
2049
|
+
if (stoppedByUser(item))
|
|
2050
|
+
stopped = true;
|
|
2051
|
+
// Its publisher's site blocks the batch's end: no more visits go there.
|
|
2052
|
+
if (item.markdown?.code && blockedBySite(item.markdown.code))
|
|
2053
|
+
giveUp(offset);
|
|
2054
|
+
if (!stopped && partsWithoutAccess(item))
|
|
2055
|
+
return;
|
|
2056
|
+
partsYet.delete(offset);
|
|
2057
|
+
await partsSettled(offset);
|
|
2058
|
+
await sendPapers();
|
|
2059
|
+
};
|
|
1550
2060
|
// The papers without access go to the other saved schools, one school at a time; not where the site still blocks
|
|
1551
2061
|
// (they end without access, with the schools tried so far).
|
|
1552
|
-
const withSchool = async (offset, school) => {
|
|
1553
|
-
if (!visitable(offset))
|
|
2062
|
+
const withSchool = async (offset, school, lane) => {
|
|
2063
|
+
if (stopped || !visitable(offset))
|
|
1554
2064
|
return;
|
|
1555
|
-
|
|
2065
|
+
if (partsYet.has(offset))
|
|
2066
|
+
return partsWithSchool(offset, school, lane);
|
|
2067
|
+
const item = await visitAgain(offset, school, lane);
|
|
1556
2068
|
schoolsTried.set(offset, (schoolsTried.get(offset) ?? 1) + 1);
|
|
1557
2069
|
if (!stopped && withoutAccess(item)) {
|
|
1558
2070
|
triedSchools(item, schoolsTried.get(offset));
|
|
@@ -1569,8 +2081,8 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
1569
2081
|
}
|
|
1570
2082
|
await sendPapers();
|
|
1571
2083
|
};
|
|
1572
|
-
if (withoutAccessYet.size && !stopped) {
|
|
1573
|
-
await laterSchools(await otherSchools(), () => [...withoutAccessYet].filter(visitable), async (school, count) => {
|
|
2084
|
+
if ((withoutAccessYet.size || partsYet.size) && !stopped) {
|
|
2085
|
+
await laterSchools(await otherSchools(), () => [...withoutAccessYet, ...partsYet].filter(visitable), async (school, count) => {
|
|
1574
2086
|
log(`[批量] 没权限的 ${count} 篇换${school.name}再试`);
|
|
1575
2087
|
ledger?.phase(`没权限的 ${count} 篇换${school.name}再试`);
|
|
1576
2088
|
if (!backend?.reportBatchProgress)
|
|
@@ -1580,7 +2092,7 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
1580
2092
|
finished: false, switchTo: school.name, laterOnly: true });
|
|
1581
2093
|
reports = reports.then(() => backend.reportBatchProgress(update)).catch(() => false);
|
|
1582
2094
|
await reports;
|
|
1583
|
-
}, withSchool, () => stopped);
|
|
2095
|
+
}, withSchool, () => stopped, (offsets, each) => atTheEnd(offsets, each, true));
|
|
1584
2096
|
}
|
|
1585
2097
|
// Without access with every school (or the batch stopped first): failed.
|
|
1586
2098
|
for (const offset of withoutAccessYet) {
|
|
@@ -1589,6 +2101,10 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
1589
2101
|
record(offset);
|
|
1590
2102
|
}
|
|
1591
2103
|
withoutAccessYet.clear();
|
|
2104
|
+
// Their Markdown or files without access with every school: saved all the same, their PDF standing.
|
|
2105
|
+
for (const offset of partsYet)
|
|
2106
|
+
await partsSettled(offset);
|
|
2107
|
+
partsYet.clear();
|
|
1592
2108
|
// Markdown saved without some figures: those once more, unless the user stopped the batch (user, 2026-11-23);
|
|
1593
2109
|
// recorded before a stop's own marks on the papers not recorded yet.
|
|
1594
2110
|
figuresTried = true;
|