papersearch 0.7.2 → 0.8.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/LICENSE-paper-search-mcp-nodejs +24 -0
- package/NOTICE +8 -37
- package/README.en.md +7 -3
- package/README.fr.md +268 -0
- package/README.ja.md +268 -0
- package/README.ko.md +268 -0
- package/README.md +7 -3
- package/README.zh-Hant.md +268 -0
- package/dist/capabilities/download/batch/batch.d.ts +16 -1
- package/dist/capabilities/download/batch/batch.d.ts.map +1 -1
- package/dist/capabilities/download/batch/batch.js +669 -53
- package/dist/capabilities/download/batch/batch.js.map +1 -1
- package/dist/capabilities/download/batch/batchFolder.d.ts.map +1 -1
- package/dist/capabilities/download/batch/batchFolder.js +4 -3
- package/dist/capabilities/download/batch/batchFolder.js.map +1 -1
- package/dist/capabilities/download/batch/batchSummary.js +1 -1
- package/dist/capabilities/download/batch/batchSummary.js.map +1 -1
- package/dist/capabilities/download/batch/networkLoss.d.ts +12 -3
- package/dist/capabilities/download/batch/networkLoss.d.ts.map +1 -1
- package/dist/capabilities/download/batch/networkLoss.js +25 -4
- package/dist/capabilities/download/batch/networkLoss.js.map +1 -1
- package/dist/capabilities/download/batch/resume.d.ts +7 -0
- package/dist/capabilities/download/batch/resume.d.ts.map +1 -1
- package/dist/capabilities/download/batch/resume.js +50 -9
- package/dist/capabilities/download/batch/resume.js.map +1 -1
- package/dist/capabilities/download/content/markdown/pro.d.ts +6 -0
- package/dist/capabilities/download/content/markdown/pro.d.ts.map +1 -1
- package/dist/capabilities/download/content/markdown/pro.js.map +1 -1
- package/dist/capabilities/download/content/markdown/shortfall.d.ts +8 -5
- package/dist/capabilities/download/content/markdown/shortfall.d.ts.map +1 -1
- package/dist/capabilities/download/content/markdown/shortfall.js +27 -15
- package/dist/capabilities/download/content/markdown/shortfall.js.map +1 -1
- package/dist/capabilities/download/content/markdown/step.d.ts +9 -0
- package/dist/capabilities/download/content/markdown/step.d.ts.map +1 -1
- package/dist/capabilities/download/content/markdown/step.js +42 -2
- package/dist/capabilities/download/content/markdown/step.js.map +1 -1
- package/dist/capabilities/download/content/markdown/types.d.ts +10 -1
- package/dist/capabilities/download/content/markdown/types.d.ts.map +1 -1
- package/dist/capabilities/download/handler.d.ts.map +1 -1
- package/dist/capabilities/download/handler.js +13 -5
- package/dist/capabilities/download/handler.js.map +1 -1
- package/dist/capabilities/download/outputLayout.d.ts.map +1 -1
- package/dist/capabilities/download/outputLayout.js +3 -1
- package/dist/capabilities/download/outputLayout.js.map +1 -1
- package/dist/capabilities/download/tools.js +2 -2
- package/dist/capabilities/download/tools.js.map +1 -1
- package/dist/capabilities/download/types.d.ts +9 -1
- package/dist/capabilities/download/types.d.ts.map +1 -1
- package/dist/cli.js +3 -1
- package/dist/cli.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 +13 -0
- package/dist/infrastructure/runlog/RunLedger.js.map +1 -1
- package/dist/infrastructure/runlog/failureClass.d.ts +17 -9
- package/dist/infrastructure/runlog/failureClass.d.ts.map +1 -1
- package/dist/infrastructure/runlog/failureClass.js +89 -56
- package/dist/infrastructure/runlog/failureClass.js.map +1 -1
- package/dist/infrastructure/school/SchoolAttempt.d.ts +11 -0
- package/dist/infrastructure/school/SchoolAttempt.d.ts.map +1 -1
- package/dist/infrastructure/school/SchoolAttempt.js +107 -59
- package/dist/infrastructure/school/SchoolAttempt.js.map +1 -1
- package/dist/infrastructure/school/SchoolMcpClient.d.ts +7 -2
- package/dist/infrastructure/school/SchoolMcpClient.d.ts.map +1 -1
- package/dist/infrastructure/school/SchoolMcpClient.js +30 -7
- package/dist/infrastructure/school/SchoolMcpClient.js.map +1 -1
- package/dist/infrastructure/school/SchoolProgress.d.ts +2 -0
- package/dist/infrastructure/school/SchoolProgress.d.ts.map +1 -1
- package/dist/infrastructure/school/SchoolProgress.js +2 -0
- package/dist/infrastructure/school/SchoolProgress.js.map +1 -1
- package/dist/infrastructure/school/SchoolReadiness.d.ts +2 -0
- package/dist/infrastructure/school/SchoolReadiness.d.ts.map +1 -1
- package/dist/infrastructure/school/SchoolReadiness.js +2 -0
- package/dist/infrastructure/school/SchoolReadiness.js.map +1 -1
- package/dist/infrastructure/school/SchoolResultNormalizer.d.ts.map +1 -1
- package/dist/infrastructure/school/SchoolResultNormalizer.js +4 -0
- package/dist/infrastructure/school/SchoolResultNormalizer.js.map +1 -1
- package/dist/infrastructure/school/index.d.ts +2 -2
- package/dist/infrastructure/school/index.d.ts.map +1 -1
- package/dist/infrastructure/school/index.js +1 -1
- package/dist/infrastructure/school/index.js.map +1 -1
- package/dist/infrastructure/school/types.d.ts +52 -3
- package/dist/infrastructure/school/types.d.ts.map +1 -1
- package/dist/infrastructure/school/types.js.map +1 -1
- package/dist/management/failures/index.d.ts +28 -2
- package/dist/management/failures/index.d.ts.map +1 -1
- package/dist/management/failures/index.js +83 -21
- package/dist/management/failures/index.js.map +1 -1
- package/package.json +7 -2
- package/school/NOTICE +3 -2
- package/school/paper_search_school/adapters/__init__.py +2 -0
- package/school/paper_search_school/adapters/aps/__init__.py +3 -6
- package/school/paper_search_school/adapters/base.py +18 -0
- package/school/paper_search_school/adapters/copernicus/__init__.py +2 -7
- package/school/paper_search_school/adapters/elsevier/__init__.py +50 -1
- package/school/paper_search_school/adapters/frontiers/__init__.py +41 -2
- package/school/paper_search_school/adapters/ieee/__init__.py +6 -4
- package/school/paper_search_school/adapters/iop/__init__.py +8 -0
- package/school/paper_search_school/adapters/jama/__init__.py +8 -0
- package/school/paper_search_school/adapters/lww/__init__.py +14 -6
- package/school/paper_search_school/adapters/mdpi/__init__.py +12 -7
- package/school/paper_search_school/adapters/neurology/__init__.py +5 -2
- package/school/paper_search_school/adapters/optica/__init__.py +2 -9
- package/school/paper_search_school/adapters/oup/__init__.py +27 -4
- package/school/paper_search_school/adapters/platforms/__init__.py +4 -1
- package/school/paper_search_school/adapters/platforms/atypon/__init__.py +8 -0
- package/school/paper_search_school/adapters/platforms/jstage/__init__.py +64 -0
- package/school/paper_search_school/adapters/sage/__init__.py +4 -1
- package/school/paper_search_school/adapters/thieme/__init__.py +6 -11
- package/school/paper_search_school/api_keys.py +63 -13
- package/school/paper_search_school/assets/api_keys.json +14 -0
- package/school/paper_search_school/assets/capsule.html +274 -158
- package/school/paper_search_school/assets/capsule_en.json +99 -98
- package/school/paper_search_school/assets/capsule_fr.json +100 -99
- package/school/paper_search_school/assets/capsule_ja.json +99 -98
- package/school/paper_search_school/assets/capsule_ko.json +99 -98
- package/school/paper_search_school/assets/capsule_zh-Hant.json +94 -97
- package/school/paper_search_school/browser/batch_session.py +1 -1
- package/school/paper_search_school/browser/challenge/__init__.py +12 -9
- package/school/paper_search_school/browser/challenge/continuation.py +65 -0
- package/school/paper_search_school/browser/challenge/handlers/__init__.py +1 -0
- package/school/paper_search_school/browser/challenge/handlers/aws_waf.py +260 -0
- package/school/paper_search_school/browser/challenge/handlers/recaptcha_hcaptcha.py +167 -0
- package/school/paper_search_school/browser/challenge/handlers/text_captcha.py +100 -0
- package/school/paper_search_school/browser/challenge/handlers/turnstile.py +174 -0
- package/school/paper_search_school/browser/challenge/kinds.py +203 -0
- package/school/paper_search_school/browser/challenge/local/__init__.py +1 -0
- package/school/paper_search_school/browser/challenge/{interaction.py → local/interaction.py} +94 -13
- package/school/paper_search_school/browser/challenge/{pointer.py → local/pointer.py} +2 -2
- package/school/paper_search_school/browser/challenge/local/prompt.py +283 -0
- package/school/paper_search_school/browser/challenge/local/submission.py +33 -0
- package/school/paper_search_school/browser/challenge/services/__init__.py +1 -0
- package/school/paper_search_school/browser/challenge/services/solver.py +272 -0
- package/school/paper_search_school/cli_config.py +10 -5
- package/school/paper_search_school/cold_start.py +22 -11
- package/school/paper_search_school/config.py +11 -10
- package/school/paper_search_school/download_diagnostics.py +7 -2
- package/school/paper_search_school/engine/access_rules.py +15 -5
- package/school/paper_search_school/engine/batch.py +55 -15
- package/school/paper_search_school/engine/batch_access.py +59 -21
- package/school/paper_search_school/engine/batch_capture.py +215 -18
- package/school/paper_search_school/engine/batch_common.py +3 -0
- package/school/paper_search_school/engine/batch_diagnostics.py +25 -5
- package/school/paper_search_school/engine/batch_institution.py +168 -32
- package/school/paper_search_school/engine/batch_login.py +223 -16
- package/school/paper_search_school/engine/batch_pdf_urls.py +38 -7
- package/school/paper_search_school/engine/batch_waits.py +179 -12
- package/school/paper_search_school/engine/bridge.py +44 -7
- package/school/paper_search_school/engine/fulltext_page.py +45 -7
- package/school/paper_search_school/engine/router.py +18 -2
- package/school/paper_search_school/entitlements.py +1 -1
- package/school/paper_search_school/orchestrator.py +44 -18
- package/school/paper_search_school/pdf_utils.py +25 -16
- package/school/paper_search_school/progress_capsule.py +36 -6
- package/school/paper_search_school/progress_reporter.py +71 -13
- package/school/paper_search_school/progress_view.py +64 -44
- package/school/paper_search_school/publishers/builtin/iop.json +2 -1
- package/school/paper_search_school/publishers/builtin/neurology.json +4 -2
- package/school/paper_search_school/publishers/builtin/sage.json +4 -2
- package/school/paper_search_school/publishers/builtin/thieme.json +3 -1
- package/school/paper_search_school/publishers/site.py +12 -4
- package/school/paper_search_school/school_switch.py +34 -52
- package/school/paper_search_school/school_test.py +1 -1
- package/school/paper_search_school/server.py +24 -11
- package/school/paper_search_school/user_settings.py +13 -0
- package/school/pyproject.toml +1 -0
- package/skills/paper-search/SKILL.md +107 -107
- package/skills/paper-search/references/browser-download.md +102 -101
- package/skills/paper-search/references/capability-routing.md +89 -89
- package/skills/paper-search/references/cli-contract.md +78 -78
- package/skills/paper-search/references/federation-support.md +172 -172
- package/skills/paper-search/references/management-layer.md +78 -78
- package/skills/paper-search/references/publisher-support.md +38 -38
- package/skills/paper-search/references/school-setup.md +56 -55
- package/skills/paper-search/references/setup.md +20 -20
- package/skills/paper-search/references/troubleshooting.md +12 -12
- package/school/paper_search_school/browser/challenge/prompt.py +0 -154
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { resolveDownloadPath } from "../../../config/ConfigService.js";
|
|
2
2
|
import { randomUUID } from "node:crypto";
|
|
3
3
|
import { RunLedger } from "../../../infrastructure/runlog/RunLedger.js";
|
|
4
|
-
import { FAILURE_CLASS_NAMES, } from "../../../infrastructure/runlog/failureClass.js";
|
|
4
|
+
import { classifyFailureCode, FAILURE_CLASS_NAMES, } from "../../../infrastructure/runlog/failureClass.js";
|
|
5
5
|
import { createDefaultDownloadTiers, downloadWithFallback, } from "../content/pdf/OpenAccessFallbackService.js";
|
|
6
|
-
import { schoolAttemptDetails, schoolShortReason } from "../../../infrastructure/school/index.js";
|
|
6
|
+
import { blockedBySite, schoolAttemptDetails, schoolShortReason } from "../../../infrastructure/school/index.js";
|
|
7
7
|
import { journalOfDoi, takeNext } from "./journalKey.js";
|
|
8
8
|
import { downloadSupplementsOnly, filesPastNetworkLoss } from "../content/supplements/fetch.js";
|
|
9
9
|
import { supplementShortfall } from "../content/supplements/shortfall.js";
|
|
10
10
|
import { fulltextPageVisit } from "../content/markdown/pageVisit.js";
|
|
11
|
-
import { fulltextAlone, fulltextForItem, markdownBeside, markdownPastNetworkLoss } from "../content/markdown/step.js";
|
|
11
|
+
import { figuresMissing, figuresOnceMore, fulltextAlone, fulltextForItem, markdownBeside, markdownPastNetworkLoss } from "../content/markdown/step.js";
|
|
12
12
|
import { markdownDoubt, markdownMissing } from "../content/markdown/shortfall.js";
|
|
13
13
|
import { batchPaper, failure, reportBatchResults, stoppedItem } from "./batchSummary.js";
|
|
14
14
|
import { loadMarkdownPro, markdownBlocked } from "../content/markdown/pro.js";
|
|
@@ -85,6 +85,23 @@ const SCHOOL_SIGN_IN_PAGE = "school_login";
|
|
|
85
85
|
/** How often a batch waiting for the user to go on with held publishers asks its window. */
|
|
86
86
|
const HOLD_POLL_MS = 3_000;
|
|
87
87
|
const HOLD_WAIT_PHASE = "其他篇目已完成,等你回来点“登录并继续”";
|
|
88
|
+
/**
|
|
89
|
+
* How long a publisher's papers wait after its site blocked one of them (被网站拦截: its human verification not passed,
|
|
90
|
+
* its block page): the batch goes on with the other publishers meanwhile, then tries them once more (user, 2026-11-23).
|
|
91
|
+
* Ten minutes: JAMA's first block in the publisher sweep had cleared within five (the re-check interval the user set for
|
|
92
|
+
* a blocked journal, 2026-11-16), its second held for the 47 minutes the batch went on hitting it; with one retry only,
|
|
93
|
+
* twice that interval. Shorter than the half hour after which a publisher's school session lapsed unused (Wiley,
|
|
94
|
+
* 2026-11-12), so nothing is loaded from the blocking site to keep it.
|
|
95
|
+
*/
|
|
96
|
+
export const BLOCKED_PAUSE_MS = 10 * 60_000;
|
|
97
|
+
/**
|
|
98
|
+
* Without 失败后自动重试: a publisher's papers its site blocks in a row before its others are given up for the batch
|
|
99
|
+
* (user, 2026-11-24: Wiley's one verification not passed, paper 85, gave its other 40 up untried). Each one blocked
|
|
100
|
+
* fails on its own; one not blocked starts the count again.
|
|
101
|
+
*/
|
|
102
|
+
export const BLOCKED_IN_ROW = 3;
|
|
103
|
+
/** A time of day as the batch's lines say it ("01:05"). */
|
|
104
|
+
const clockTime = (at) => new Date(at).toTimeString().slice(0, 5);
|
|
88
105
|
const AWAY_NOTICE = "[批量] 这一篇等你登录或验证已到时限;后面的篇目遇到登录或验证只等 60 秒,你在登录页开始输入就照常等";
|
|
89
106
|
/**
|
|
90
107
|
* Entitled access waited on the user (school login, verification) until its
|
|
@@ -93,7 +110,7 @@ const AWAY_NOTICE = "[批量] 这一篇等你登录或验证已到时限;后
|
|
|
93
110
|
function waitedOutOnUser(result) {
|
|
94
111
|
return (result.status !== "ok" &&
|
|
95
112
|
result.attempts.some((attempt) => attempt.stage === "entitled_access" &&
|
|
96
|
-
(attempt.code === "login_timeout" || attempt.code === "download_timeout")));
|
|
113
|
+
(attempt.code === "login_timeout" || attempt.code === "download_timeout" || attempt.code === "captcha_required")));
|
|
97
114
|
}
|
|
98
115
|
/**
|
|
99
116
|
* The seconds the backend asked this article to wait, and whose limit it is: the publisher's
|
|
@@ -192,6 +209,79 @@ const STOP_NOTICE = "[批量] 已在进度窗口终止任务,剩下的篇目
|
|
|
192
209
|
export function batchStopped(result) {
|
|
193
210
|
return !!result.stopped || result.items.some((item) => stoppedByUser(item) || item.attempts.some((attempt) => attempt.code === "login_paused"));
|
|
194
211
|
}
|
|
212
|
+
/**
|
|
213
|
+
* 没权限 (FailureClass no_access) at the school's stage: the publisher refused the signed-in school, the school's
|
|
214
|
+
* sign-in is not enabled for the publisher, the publisher has no route for it, or the user marked the journal as
|
|
215
|
+
* without access. Another saved school may have it (没权限时换下一所学校, user 2026-11-22: every paper under 没权限).
|
|
216
|
+
*/
|
|
217
|
+
function withoutAccess(result) {
|
|
218
|
+
if (!result || result.status === "ok")
|
|
219
|
+
return false;
|
|
220
|
+
const school = [...result.attempts].reverse().find((attempt) => attempt.stage === "entitled_access");
|
|
221
|
+
return school?.status === "error" && classifyFailureCode(school.code) === "no_access";
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* 原因不明 (FailureClass unexplained) at the school's stage: tried once more at the batch's end, with the school it had,
|
|
225
|
+
* before any other school (user, 2026-11-22: "原因不明的也最后再试一次"); not a paper the user stopped.
|
|
226
|
+
*/
|
|
227
|
+
function unexplainedFailure(result) {
|
|
228
|
+
if (!result || result.status === "ok" || stoppedByUser(result))
|
|
229
|
+
return false;
|
|
230
|
+
const school = [...result.attempts].reverse().find((attempt) => attempt.stage === "entitled_access");
|
|
231
|
+
return school?.status === "error" && classifyFailureCode(school.code) === "unexplained";
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* 被网站拦截 at the school's stage (blockedBySite): the publisher's site blocked the network or its human verification was
|
|
235
|
+
* not passed. Its publisher's papers wait (``BLOCKED_PAUSE_MS``); none goes to the end's retry or other schools.
|
|
236
|
+
*/
|
|
237
|
+
function siteBlocked(result) {
|
|
238
|
+
if (!result || result.status === "ok" || stoppedByUser(result))
|
|
239
|
+
return false;
|
|
240
|
+
const school = [...result.attempts].reverse().find((attempt) => attempt.stage === "entitled_access");
|
|
241
|
+
return school?.status === "error" && blockedBySite(school.code);
|
|
242
|
+
}
|
|
243
|
+
/** The other saved schools a batch's papers without access go to at its end: none for a batch given its school. */
|
|
244
|
+
async function schoolsAfterDefault(backend, options) {
|
|
245
|
+
if (options.school?.trim() || !backend?.schoolSwitch)
|
|
246
|
+
return [];
|
|
247
|
+
return (await backend.schoolSwitch().catch(() => undefined))?.schools.slice(1) ?? [];
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* 失败后自动重试 (the settings' 等待和提醒 page; user, 2026-11-24: off by default, whoever wants it turns it on): 原因不明
|
|
251
|
+
* tried once more at the batch's end, a publisher whose site blocked it paused and tried once more after BLOCKED_PAUSE_MS.
|
|
252
|
+
* Off, or a backend that does not say: 原因不明 fails at once, and a blocking site's publisher is given up for the batch
|
|
253
|
+
* (接着下 goes on with it). Neither 没权限时换下一所学校 (its own switch) nor a lapsed school login (登录并继续) is one.
|
|
254
|
+
*/
|
|
255
|
+
async function retrySetting(backend) {
|
|
256
|
+
if (!backend?.schoolSwitch)
|
|
257
|
+
return false;
|
|
258
|
+
return (await backend.schoolSwitch().catch(() => undefined))?.retry === true;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* 没权限时换下一所学校 at a batch's end (PaperSearch Pro; user, 2026-11-22): the papers still without access go to each
|
|
262
|
+
* other saved school in turn, all of them with one before the next, so that the school's login is switched once, not at
|
|
263
|
+
* every paper (switching to and fro had a school's login ask for a picture code, Windows 2026-11-22). ``visit`` tries
|
|
264
|
+
* one paper with a school.
|
|
265
|
+
*/
|
|
266
|
+
async function laterSchools(schools, waiting, announce, visit, stopped) {
|
|
267
|
+
for (const school of schools) {
|
|
268
|
+
const offsets = waiting();
|
|
269
|
+
if (!offsets.length || stopped())
|
|
270
|
+
return;
|
|
271
|
+
await announce(school, offsets.length);
|
|
272
|
+
for (const offset of offsets) {
|
|
273
|
+
if (stopped())
|
|
274
|
+
return;
|
|
275
|
+
await visit(offset, school);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
/** A paper without access with each of ``count`` schools: its last school stage says so (N 所学校都没有权限). */
|
|
280
|
+
function triedSchools(result, count) {
|
|
281
|
+
const school = [...result.attempts].reverse().find((attempt) => attempt.stage === "entitled_access");
|
|
282
|
+
if (school && count > 1)
|
|
283
|
+
school.schools = count;
|
|
284
|
+
}
|
|
195
285
|
/** The attempt recorded for an item the batch did not reach because the user stopped it. */
|
|
196
286
|
function notReached() {
|
|
197
287
|
return { stage: "entitled_access", status: "skipped", message: "已终止任务,未下载", code: "batch_stopped" };
|
|
@@ -228,6 +318,21 @@ function recorded(request, ledger, item) {
|
|
|
228
318
|
? { ...request, onStage: (record) => ledger.stage(item, record), recordsDir: ledger.records, diagnosticsDir: ledger.diagnostics }
|
|
229
319
|
: request;
|
|
230
320
|
}
|
|
321
|
+
/**
|
|
322
|
+
* A batch's end (user, 2026-11-23): the papers whose Markdown was saved without figures that did not download get
|
|
323
|
+
* those figures once more (figuresOnceMore); still missing, the Markdown stays saved with them marked in it. Each
|
|
324
|
+
* paper's record waits for this, to say how it ended.
|
|
325
|
+
*/
|
|
326
|
+
async function figuresAtTheEnd(papers, log, ledger) {
|
|
327
|
+
if (!papers.length)
|
|
328
|
+
return;
|
|
329
|
+
log(`[批量] Markdown 缺图的 ${papers.length} 篇再取一次图片`);
|
|
330
|
+
ledger?.phase(`Markdown 缺图的 ${papers.length} 篇再取一次图片`);
|
|
331
|
+
for (const { result, request } of papers)
|
|
332
|
+
await figuresOnceMore(result, request);
|
|
333
|
+
const still = papers.filter(({ result }) => figuresMissing(result)).length;
|
|
334
|
+
log(`[批量] 再取图片:${papers.length - still} 篇补齐` + (still ? `,${still} 篇仍缺图(Markdown 已保存,缺的图在文中标明)` : ""));
|
|
335
|
+
}
|
|
231
336
|
function openLedger(mode, batchId, options, log) {
|
|
232
337
|
const ledger = RunLedger.open({
|
|
233
338
|
kind: "download_batch",
|
|
@@ -278,7 +383,7 @@ function summary(mode, batchId, items, extra = {}) {
|
|
|
278
383
|
};
|
|
279
384
|
}
|
|
280
385
|
/** Download a list serially; one batch id ties it to one progress window. */
|
|
281
|
-
export async function downloadBatch(searchers, options, backend, log = (line) => process.stderr.write(`${line}\n`), createTiers = createDefaultDownloadTiers, pause = wait, online = networkReachable) {
|
|
386
|
+
export async function downloadBatch(searchers, options, backend, log = (line) => process.stderr.write(`${line}\n`), createTiers = createDefaultDownloadTiers, pause = wait, online = networkReachable, now = Date.now) {
|
|
282
387
|
options = { ...options, savePath: resolveDownloadPath(options.savePath) };
|
|
283
388
|
// The batch's own folder keeps its records beside its papers (user, 2026-11-17).
|
|
284
389
|
const { folderRecord: _given, ...asked } = options;
|
|
@@ -317,7 +422,7 @@ export async function downloadBatch(searchers, options, backend, log = (line) =>
|
|
|
317
422
|
? downloadSerialBatch(options, backend, log, searchers, createTiers, pause, online)
|
|
318
423
|
: options.mode === "per_item"
|
|
319
424
|
? downloadPerItem(searchers, options, backend, log, createTiers, pause, online)
|
|
320
|
-
: downloadTwoPhase(searchers, options, backend, log, createTiers, options.mode === "entitled_only" ? "entitled_only" : "two_phase", pause, online));
|
|
425
|
+
: downloadTwoPhase(searchers, options, backend, log, createTiers, options.mode === "entitled_only" ? "entitled_only" : "two_phase", pause, online, now));
|
|
321
426
|
await reportBatchResults(result, backend, startedAt, options.savePath);
|
|
322
427
|
return result;
|
|
323
428
|
};
|
|
@@ -380,8 +485,8 @@ export async function downloadBatch(searchers, options, backend, log = (line) =>
|
|
|
380
485
|
(missed ? `。附录未下全 ${missed} 篇:${[...shortfalls].map(([reason, n]) => `${reason} ${n}`).join("、")}` : "") +
|
|
381
486
|
(incomplete ? `。Markdown 未生成 ${incomplete} 篇:${[...noMarkdown].map(([reason, n]) => `${reason} ${n}`).join("、")}` : "") +
|
|
382
487
|
(doubtful ? `。Markdown 可能不全 ${doubtful} 篇(已保存,请核对):${[...doubts].map(([reason, n]) => `${reason} ${n}`).join("、")}` : ""));
|
|
383
|
-
// What is left goes on later with one word (user, 2026-11-15).
|
|
384
|
-
if ((result.failed || missed || incomplete) && result.runLog) {
|
|
488
|
+
// What is left goes on later with one word (user, 2026-11-15): a Markdown's missing figures too.
|
|
489
|
+
if ((result.failed || missed || incomplete || result.items.some(figuresMissing)) && result.runLog) {
|
|
385
490
|
log("[批量] 没下全的篇目:回来后对 AI 说“接着下”即可补(download_batch 的 resume: \"latest\",同一套环境变量)");
|
|
386
491
|
}
|
|
387
492
|
folderRecord?.refresh();
|
|
@@ -406,6 +511,9 @@ async function downloadSerialBatch(options, backend, log, searchers, createTiers
|
|
|
406
511
|
let succeeded = 0;
|
|
407
512
|
let failed = 0;
|
|
408
513
|
let away = false;
|
|
514
|
+
/** Papers whose Markdown lacks figures, recorded after their second try at the end (figuresAtTheEnd). */
|
|
515
|
+
const figuresLater = [];
|
|
516
|
+
let stopped = false;
|
|
409
517
|
for (const [offset, item] of options.items.entries()) {
|
|
410
518
|
const batch = { id: batchId, index: offset + 1, total, done: offset, succeeded, failed };
|
|
411
519
|
const request = { ...itemOptions(item, options), batch, ...(away ? { shortUserWait: true } : {}) };
|
|
@@ -437,10 +545,14 @@ async function downloadSerialBatch(options, backend, log, searchers, createTiers
|
|
|
437
545
|
succeeded += 1;
|
|
438
546
|
else
|
|
439
547
|
failed += 1;
|
|
440
|
-
ledger?.item(entry, result);
|
|
441
548
|
items.push(itemResult(result, offset + 1, request));
|
|
549
|
+
if (figuresMissing(result))
|
|
550
|
+
figuresLater.push({ offset, entry, request, ...(item.mainPath ? { mainPath: item.mainPath } : {}) });
|
|
551
|
+
else
|
|
552
|
+
ledger?.item(entry, result);
|
|
442
553
|
papers.set(offset, batchPaper(request, items[items.length - 1], undefined, content, item.mainPath));
|
|
443
554
|
if (stoppedByUser(result)) {
|
|
555
|
+
stopped = true;
|
|
444
556
|
for (const [rest, item] of options.items.entries()) {
|
|
445
557
|
if (rest <= offset)
|
|
446
558
|
continue;
|
|
@@ -456,6 +568,14 @@ async function downloadSerialBatch(options, backend, log, searchers, createTiers
|
|
|
456
568
|
}
|
|
457
569
|
await papers.send();
|
|
458
570
|
}
|
|
571
|
+
// Markdown saved without some figures: those once more, unless the user stopped the batch (user, 2026-11-23).
|
|
572
|
+
if (!stopped) {
|
|
573
|
+
await figuresAtTheEnd(figuresLater.map(({ offset, entry, request }) => ({ result: items[offset], request: recorded(request, ledger, entry) })), log, ledger);
|
|
574
|
+
}
|
|
575
|
+
for (const { offset, entry, request, mainPath } of figuresLater) {
|
|
576
|
+
ledger?.item(entry, items[offset]);
|
|
577
|
+
papers.set(offset, batchPaper(request, items[offset], undefined, content, mainPath));
|
|
578
|
+
}
|
|
459
579
|
await papers.send(true);
|
|
460
580
|
ledger?.close();
|
|
461
581
|
return summary(options.mode ?? "two_phase", batchId, items, {
|
|
@@ -475,17 +595,48 @@ async function downloadPerItem(searchers, options, backend, log, createTiers, pa
|
|
|
475
595
|
let succeeded = 0;
|
|
476
596
|
let failed = 0;
|
|
477
597
|
let away = false;
|
|
598
|
+
// 没权限时换下一所学校 (PaperSearch Pro): papers without access wait for the other saved schools, tried at the end, one
|
|
599
|
+
// school at a time (``laterSchools``, user 2026-11-22).
|
|
600
|
+
let turns;
|
|
601
|
+
const otherSchools = () => (turns ??= schoolsAfterDefault(backend, options));
|
|
602
|
+
let retryOn;
|
|
603
|
+
const retrying = () => (retryOn ??= retrySetting(backend));
|
|
604
|
+
const waiting = new Map();
|
|
605
|
+
const schoolsTried = new Map();
|
|
606
|
+
/** Papers failed for a reason not known, waiting for their one more try at the end (user, 2026-11-22). */
|
|
607
|
+
const unexplainedYet = new Map();
|
|
608
|
+
// The papers held for a later try are in the window's ring beside those done (user, 2026-11-23), told when they change.
|
|
609
|
+
const dois = (entries) => [...entries.values()].map((entry) => entry.doi ?? "").filter(Boolean);
|
|
610
|
+
const later = () => ({ noAccess: waiting.size, unexplained: unexplainedYet.size, blocked: 0, login: 0,
|
|
611
|
+
papers: { noAccess: dois(waiting), unexplained: dois(unexplainedYet), blocked: [], login: [] } });
|
|
612
|
+
let laterSaid = JSON.stringify(later());
|
|
613
|
+
/** The window told of them, and at the switch to another school, its name; none once every paper is done. */
|
|
614
|
+
const reportLater = async (school) => {
|
|
615
|
+
const counts = later();
|
|
616
|
+
if (!backend?.reportBatchProgress || succeeded + failed >= total || !school && JSON.stringify(counts) === laterSaid)
|
|
617
|
+
return;
|
|
618
|
+
laterSaid = JSON.stringify(counts);
|
|
619
|
+
await backend.reportBatchProgress({ id: batchId, total, done: succeeded + failed, succeeded, failed, current: "",
|
|
620
|
+
finished: false, later: counts, ...(school ? { switchTo: school.name } : {}), laterOnly: true }).catch(() => false);
|
|
621
|
+
};
|
|
622
|
+
/** Papers whose Markdown lacks figures, recorded after their second try at the end (figuresAtTheEnd). */
|
|
623
|
+
const figuresLater = new Map();
|
|
624
|
+
/** A finished paper into the run's records, unless its Markdown's figures wait for the end. */
|
|
625
|
+
const record = (offset, entry) => {
|
|
626
|
+
if (figuresMissing(items[offset]))
|
|
627
|
+
figuresLater.set(offset, entry);
|
|
628
|
+
else
|
|
629
|
+
ledger?.item(entry, items[offset]);
|
|
630
|
+
};
|
|
631
|
+
let stopped = false;
|
|
478
632
|
for (const [offset, item] of options.items.entries()) {
|
|
479
633
|
const index = offset + 1;
|
|
480
634
|
const request = itemOptions(item, options);
|
|
481
635
|
const entry = runItem(index, request);
|
|
482
636
|
log(`[批量] 第 ${index}/${total} 篇:${request.doi || request.paperId}`);
|
|
483
|
-
|
|
637
|
+
// Papers waiting for the end (without access, 原因不明) are not done yet: the counts are of those finished.
|
|
638
|
+
const batch = { id: batchId, index, total, done: succeeded + failed, succeeded, failed };
|
|
484
639
|
const result = await pastNetworkLoss(() => pastHourlyLimit(() => downloadWithFallback(searchers, recorded({ ...request, batch, ...(away ? { shortUserWait: true } : {}) }, ledger, entry), createTiers(backend)), log, pause, backend), watch);
|
|
485
|
-
if (result.status === "ok")
|
|
486
|
-
succeeded += 1;
|
|
487
|
-
else
|
|
488
|
-
failed += 1;
|
|
489
640
|
if (!away && waitedOutOnUser(result)) {
|
|
490
641
|
away = true;
|
|
491
642
|
log(AWAY_NOTICE);
|
|
@@ -496,13 +647,35 @@ async function downloadPerItem(searchers, options, backend, log, createTiers, pa
|
|
|
496
647
|
if (!stoppedByUser(result)) {
|
|
497
648
|
await markdownPastNetworkLoss(finished, recorded({ ...request, batch }, ledger, entry), searchers, pageVisit(backend, { ...request, batch }, log, pause), watch);
|
|
498
649
|
}
|
|
499
|
-
await reportBatchItem({ ...request, batch }, finished, backend);
|
|
500
|
-
ledger?.item(entry, finished);
|
|
501
650
|
items.push(finished);
|
|
651
|
+
if (withoutAccess(finished) && !stoppedByUser(finished) && (await otherSchools()).length) {
|
|
652
|
+
// Not finished: it waits for the other schools at the end.
|
|
653
|
+
waiting.set(offset, entry);
|
|
654
|
+
papers.set(offset, batchPaper(request, finished, "school", content));
|
|
655
|
+
await papers.send();
|
|
656
|
+
await reportLater();
|
|
657
|
+
continue;
|
|
658
|
+
}
|
|
659
|
+
if (unexplainedFailure(finished) && await retrying()) {
|
|
660
|
+
// Not finished: once more at the end, with this school.
|
|
661
|
+
unexplainedYet.set(offset, entry);
|
|
662
|
+
papers.set(offset, batchPaper(request, finished, "school", content));
|
|
663
|
+
await papers.send();
|
|
664
|
+
await reportLater();
|
|
665
|
+
continue;
|
|
666
|
+
}
|
|
667
|
+
if (result.status === "ok")
|
|
668
|
+
succeeded += 1;
|
|
669
|
+
else
|
|
670
|
+
failed += 1;
|
|
671
|
+
// The window's end when the last paper is finished, not the last in the list (some wait for the end).
|
|
672
|
+
await reportBatchItem({ ...request, batch: { ...batch, index: batch.done + 1 } }, finished, backend);
|
|
673
|
+
record(offset, entry);
|
|
502
674
|
papers.set(offset, batchPaper(request, finished, undefined, content));
|
|
503
675
|
if (publisherBlocked(result))
|
|
504
676
|
log(blockedNotice(request.doi || request.paperId));
|
|
505
677
|
if (stoppedByUser(finished)) {
|
|
678
|
+
stopped = true;
|
|
506
679
|
for (const [rest, restItem] of options.items.entries()) {
|
|
507
680
|
if (rest <= offset)
|
|
508
681
|
continue;
|
|
@@ -517,6 +690,87 @@ async function downloadPerItem(searchers, options, backend, log, createTiers, pa
|
|
|
517
690
|
}
|
|
518
691
|
await papers.send();
|
|
519
692
|
}
|
|
693
|
+
/** A paper's school stage once more (another saved school when given), its files and Markdown when it comes. */
|
|
694
|
+
const again = async (offset, entry, school) => {
|
|
695
|
+
await reportLater();
|
|
696
|
+
const request = { ...itemOptions(options.items[offset], options), ...(school ? { school: school.entityId } : {}) };
|
|
697
|
+
const batch = { id: batchId, index: offset + 1, total, done: succeeded + failed, succeeded, failed };
|
|
698
|
+
log(school ? `[批量] 换${school.name}:${request.doi || request.paperId}` : `[批量] 再试一次:${request.doi || request.paperId}`);
|
|
699
|
+
const result = await pastNetworkLoss(() => pastHourlyLimit(() => downloadWithFallback(searchers, recorded({ ...request, batch, ...(away ? { shortUserWait: true } : {}) }, ledger, entry), createTiers(backend).filter((tier) => tier.stage === "entitled_access")), log, pause, backend), watch);
|
|
700
|
+
const before = items[offset];
|
|
701
|
+
const finished = itemResult({ ...result, attempts: [...before.attempts, ...result.attempts],
|
|
702
|
+
...(before.markdown ? { markdown: before.markdown } : {}) }, offset + 1, request);
|
|
703
|
+
items[offset] = finished;
|
|
704
|
+
if (result.status === "ok") {
|
|
705
|
+
await filesPastNetworkLoss(finished, recorded({ ...request, batch }, ledger, entry), backend, searchers, watch);
|
|
706
|
+
if (finished.markdown?.status !== "ok") {
|
|
707
|
+
await markdownPastNetworkLoss(finished, recorded({ ...request, batch }, ledger, entry), searchers, pageVisit(backend, { ...request, batch }, log, pause), watch);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
if (stoppedByUser(result))
|
|
711
|
+
stopped = true;
|
|
712
|
+
return finished;
|
|
713
|
+
};
|
|
714
|
+
/** A paper finished at the end: counted, recorded, shown. */
|
|
715
|
+
const finish = async (offset, entry) => {
|
|
716
|
+
const request = itemOptions(options.items[offset], options);
|
|
717
|
+
const done = succeeded + failed;
|
|
718
|
+
const batch = { id: batchId, index: done + 1, total, done, succeeded, failed };
|
|
719
|
+
if (items[offset].status === "ok")
|
|
720
|
+
succeeded += 1;
|
|
721
|
+
else
|
|
722
|
+
failed += 1;
|
|
723
|
+
await reportBatchItem({ ...request, batch }, items[offset], backend);
|
|
724
|
+
record(offset, entry);
|
|
725
|
+
papers.set(offset, batchPaper(request, items[offset], undefined, content));
|
|
726
|
+
await papers.send();
|
|
727
|
+
};
|
|
728
|
+
// 原因不明 once more, with this school, before any other school (user, 2026-11-22).
|
|
729
|
+
if (unexplainedYet.size && !stopped) {
|
|
730
|
+
log(`[批量] 原因不明的 ${unexplainedYet.size} 篇再试一次`);
|
|
731
|
+
ledger?.phase(`原因不明的 ${unexplainedYet.size} 篇再试一次`);
|
|
732
|
+
for (const [offset, entry] of [...unexplainedYet]) {
|
|
733
|
+
if (stopped)
|
|
734
|
+
break;
|
|
735
|
+
const finished = await again(offset, entry);
|
|
736
|
+
unexplainedYet.delete(offset);
|
|
737
|
+
if (!stopped && withoutAccess(finished) && (await otherSchools()).length)
|
|
738
|
+
waiting.set(offset, entry);
|
|
739
|
+
else
|
|
740
|
+
await finish(offset, entry);
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
for (const [offset, entry] of unexplainedYet)
|
|
744
|
+
await finish(offset, entry);
|
|
745
|
+
// The papers without access go to the other saved schools, one school at a time.
|
|
746
|
+
if (waiting.size && !stopped) {
|
|
747
|
+
await laterSchools(await otherSchools(), () => [...waiting.keys()], async (school, count) => {
|
|
748
|
+
log(`[批量] 没权限的 ${count} 篇换${school.name}再试`);
|
|
749
|
+
ledger?.phase(`没权限的 ${count} 篇换${school.name}再试`);
|
|
750
|
+
await reportLater(school);
|
|
751
|
+
}, async (offset, school) => {
|
|
752
|
+
const entry = waiting.get(offset);
|
|
753
|
+
const finished = await again(offset, entry, school);
|
|
754
|
+
schoolsTried.set(offset, (schoolsTried.get(offset) ?? 1) + 1);
|
|
755
|
+
if (!stopped && withoutAccess(finished)) {
|
|
756
|
+
triedSchools(finished, schoolsTried.get(offset));
|
|
757
|
+
return;
|
|
758
|
+
}
|
|
759
|
+
waiting.delete(offset);
|
|
760
|
+
await finish(offset, entry);
|
|
761
|
+
}, () => stopped);
|
|
762
|
+
}
|
|
763
|
+
// Without access with every school (or the batch stopped first): failed.
|
|
764
|
+
for (const [offset, entry] of waiting)
|
|
765
|
+
await finish(offset, entry);
|
|
766
|
+
// Markdown saved without some figures: those once more, unless the user stopped the batch (user, 2026-11-23).
|
|
767
|
+
if (!stopped) {
|
|
768
|
+
await figuresAtTheEnd([...figuresLater].map(([offset, entry]) => ({ result: items[offset], request: recorded(itemOptions(options.items[offset], options), ledger, entry) })), log, ledger);
|
|
769
|
+
}
|
|
770
|
+
for (const [offset, entry] of figuresLater) {
|
|
771
|
+
ledger?.item(entry, items[offset]);
|
|
772
|
+
papers.set(offset, batchPaper(itemOptions(options.items[offset], options), items[offset], undefined, content));
|
|
773
|
+
}
|
|
520
774
|
await papers.send(true);
|
|
521
775
|
ledger?.close();
|
|
522
776
|
return summary("per_item", batchId, items, {
|
|
@@ -532,7 +786,7 @@ function laneError(stage, error) {
|
|
|
532
786
|
message: error instanceof Error ? `${error.name}: ${error.message}` : String(error) }],
|
|
533
787
|
};
|
|
534
788
|
}
|
|
535
|
-
async function downloadTwoPhase(searchers, options, backend, log, createTiers, mode = "two_phase", pause = wait, online = networkReachable) {
|
|
789
|
+
async function downloadTwoPhase(searchers, options, backend, log, createTiers, mode = "two_phase", pause = wait, online = networkReachable, now = Date.now) {
|
|
536
790
|
const batchId = randomUUID();
|
|
537
791
|
const total = options.items.length;
|
|
538
792
|
const ledger = openLedger(mode, batchId, options, log);
|
|
@@ -542,10 +796,17 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
542
796
|
const content = options.content ?? "main";
|
|
543
797
|
const papers = new PaperList(backend, batchId, options.savePath, requests.map((request) => batchPaper(request, undefined, mode === "entitled_only" ? "school" : "open", content)));
|
|
544
798
|
const recordedOffsets = new Set();
|
|
799
|
+
/** Papers whose Markdown lacks figures: recorded after their second try at the end (figuresAtTheEnd). */
|
|
800
|
+
const figuresLater = new Set();
|
|
801
|
+
let figuresTried = false;
|
|
545
802
|
/** An item goes to the run record once, when it has settled. */
|
|
546
803
|
const record = (offset) => {
|
|
547
804
|
if (recordedOffsets.has(offset))
|
|
548
805
|
return;
|
|
806
|
+
if (!figuresTried && figuresMissing(results[offset])) {
|
|
807
|
+
figuresLater.add(offset);
|
|
808
|
+
return;
|
|
809
|
+
}
|
|
549
810
|
recordedOffsets.add(offset);
|
|
550
811
|
ledger?.item(entries[offset], results[offset]);
|
|
551
812
|
};
|
|
@@ -594,11 +855,11 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
594
855
|
const reportProgress = (current) => {
|
|
595
856
|
if (!openProgress)
|
|
596
857
|
return reports;
|
|
597
|
-
const update = {
|
|
858
|
+
const update = withLater({
|
|
598
859
|
id: batchId, total, done: settledOk() + failed, succeeded: settledOk(), failed, current, finished: false,
|
|
599
860
|
...(openRunning ? { phase: `${OPEN_CHANNEL}:已查 ${checked}/${total} 篇,找到 ${openFound} 篇`, channel: OPEN_CHANNEL } : {}),
|
|
600
861
|
...(missed.length ? { lanes: lanes() } : {}),
|
|
601
|
-
};
|
|
862
|
+
});
|
|
602
863
|
reports = reports.then(() => backend.reportBatchProgress(update)).catch(() => false);
|
|
603
864
|
return reports;
|
|
604
865
|
};
|
|
@@ -612,8 +873,8 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
612
873
|
const watch = { log, pause, online, notice: (phase) => {
|
|
613
874
|
if (!backend?.reportBatchProgress || !(openProgress || missed.length))
|
|
614
875
|
return reports;
|
|
615
|
-
const update = { id: batchId, total, done: settledOk() + failed, succeeded: settledOk(), failed, current: "",
|
|
616
|
-
finished: false, phase };
|
|
876
|
+
const update = withLater({ id: batchId, total, done: settledOk() + failed, succeeded: settledOk(), failed, current: "",
|
|
877
|
+
finished: false, phase });
|
|
617
878
|
reports = reports.then(() => backend.reportBatchProgress(update)).catch(() => false);
|
|
618
879
|
return reports;
|
|
619
880
|
} };
|
|
@@ -714,7 +975,8 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
714
975
|
}
|
|
715
976
|
// Its page stopped at the school's own sign-in page: held with its publisher, its Markdown
|
|
716
977
|
// fetched again when the user goes on (the files it got stand).
|
|
717
|
-
if (pageWait === SCHOOL_SIGN_IN_PAGE && wentThrough(offset) && !stoppedByUser(item) && await canHold()
|
|
978
|
+
if (pageWait === SCHOOL_SIGN_IN_PAGE && wentThrough(offset) && !stoppedByUser(item) && await canHold()
|
|
979
|
+
&& !heldAgain(offset)) {
|
|
718
980
|
item.attempts.length = markdownFrom;
|
|
719
981
|
delete item.markdown;
|
|
720
982
|
finishing.add(offset);
|
|
@@ -752,6 +1014,12 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
752
1014
|
const viaSchool = new Set();
|
|
753
1015
|
/** Each held publisher's papers, open-access ones still to be finished among them. */
|
|
754
1016
|
const held = new Map();
|
|
1017
|
+
/**
|
|
1018
|
+
* The papers that held their publisher. Gone on with by 登录并继续 and stopped at the school's sign-in page again, such
|
|
1019
|
+
* a paper fails and its publisher's other papers go on, not held a second time (Windows, 2026-11-24: Nature Digest, a
|
|
1020
|
+
* site with only its subscribers' sign-in, held Springer Nature's 73 other papers again after each 登录并继续).
|
|
1021
|
+
*/
|
|
1022
|
+
const heldBy = new Set();
|
|
755
1023
|
/** Where each paper's wait on a school login ended, as the backend said (trace.loginWaitPage). */
|
|
756
1024
|
const waitEnded = new Map();
|
|
757
1025
|
const prefixOf = (offset) => (/^10\.\d{4,9}\//.exec(requests[offset].doi.toLowerCase()) ?? [""])[0];
|
|
@@ -777,15 +1045,26 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
777
1045
|
const canHold = async () => (holdOk ??= await backend?.holdSupported?.().catch(() => false) ?? false);
|
|
778
1046
|
/** Each DOI prefix's publisher as the school's stages named it ("Science"); the last one named. */
|
|
779
1047
|
const publishers = new Map();
|
|
780
|
-
|
|
1048
|
+
// A publisher whose site blocks the batch (被网站拦截: its human verification not passed, its block page): its papers
|
|
1049
|
+
// wait in ``held`` too, the others go on, and they are tried once more after BLOCKED_PAUSE_MS, without the user; blocked
|
|
1050
|
+
// again then, the publisher is given up for this batch (user, 2026-11-23: JAMA blocked after about 15 papers, and 21
|
|
1051
|
+
// of 28 each waited out its verification, at the end again with the retry of 原因不明 and with the next school).
|
|
1052
|
+
/** Each held publisher whose site blocked the batch: when its papers are tried again. */
|
|
1053
|
+
const blockedUntil = new Map();
|
|
1054
|
+
/** Publishers whose papers went on after their pause: blocked again, the batch gives them up. */
|
|
1055
|
+
const triedAgain = new Set();
|
|
1056
|
+
/** Publishers given up: their papers not tried yet end as 被网站拦截, untried; no visit of the batch's end goes to them. */
|
|
1057
|
+
const givenUp = new Set();
|
|
1058
|
+
/** The held publishers waiting for the user (登录并继续), not those blocked. */
|
|
1059
|
+
const heldList = () => [...held.entries()].filter(([key]) => !blockedUntil.has(key)).map(([key, offsets]) => ({
|
|
781
1060
|
key, name: publishers.get(key) ?? key, count: offsets.length,
|
|
782
1061
|
}));
|
|
783
1062
|
/** The window's line and button for the held publishers (none: cleared). */
|
|
784
1063
|
const reportHeld = (waiting = false) => {
|
|
785
1064
|
if (!backend?.reportBatchProgress || !holdOk)
|
|
786
1065
|
return reports;
|
|
787
|
-
const update = { id: batchId, total, done: settledOk() + failed, succeeded: settledOk(), failed, current: "",
|
|
788
|
-
finished: false, held: heldList(), ...(waiting ? { phase: HOLD_WAIT_PHASE, heldWait: true } : { lanes: lanes() }) };
|
|
1066
|
+
const update = withLater({ id: batchId, total, done: settledOk() + failed, succeeded: settledOk(), failed, current: "",
|
|
1067
|
+
finished: false, held: heldList(), ...(waiting ? { phase: HOLD_WAIT_PHASE, heldWait: true } : { lanes: lanes() }) });
|
|
789
1068
|
reports = reports.then(() => backend.reportBatchProgress(update)).catch(() => false);
|
|
790
1069
|
return reports;
|
|
791
1070
|
};
|
|
@@ -797,13 +1076,98 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
797
1076
|
return;
|
|
798
1077
|
}
|
|
799
1078
|
held.set(key, [offset]);
|
|
1079
|
+
heldBy.add(offset);
|
|
800
1080
|
log(`[批量] ${key} 的文章前面经学校登录下到了,这一篇停在学校登录页没有完成登录:` +
|
|
801
1081
|
"这家出版社的篇目先暂停,其他照常下载;回来后在进度窗口点“登录并继续”");
|
|
802
1082
|
ledger?.phase(`暂停 ${key}:学校登录中途失效`);
|
|
803
1083
|
void reportHeld();
|
|
804
1084
|
};
|
|
805
|
-
/**
|
|
1085
|
+
/** ``offset`` held its publisher before and stopped at the school's sign-in page again after 登录并继续: not held again. */
|
|
1086
|
+
const heldAgain = (offset) => {
|
|
1087
|
+
if (!heldBy.has(offset))
|
|
1088
|
+
return false;
|
|
1089
|
+
log(`[批量] ${id(offset)} 点“登录并继续”后仍停在学校登录页:这一篇记为失败,${publisherOf(offset)} 的其他篇目接着下载`);
|
|
1090
|
+
return true;
|
|
1091
|
+
};
|
|
1092
|
+
/** The window's line while blocked publishers' papers wait: when they are tried again, how many. */
|
|
1093
|
+
const reportBlocked = () => {
|
|
1094
|
+
if (!backend?.reportBatchProgress || !blockedUntil.size)
|
|
1095
|
+
return reports;
|
|
1096
|
+
const until = Math.min(...blockedUntil.values());
|
|
1097
|
+
const count = [...blockedUntil.keys()].reduce((sum, key) => sum + (held.get(key)?.length ?? 0), 0);
|
|
1098
|
+
if (!count)
|
|
1099
|
+
return reports;
|
|
1100
|
+
const update = withLater({ id: batchId, total, done: settledOk() + failed, succeeded: settledOk(), failed, current: "",
|
|
1101
|
+
finished: false, phase: `被网站拦截,${clockTime(until)} 再试暂停的 ${count} 篇` });
|
|
1102
|
+
reports = reports.then(() => backend.reportBatchProgress(update)).catch(() => false);
|
|
1103
|
+
return reports;
|
|
1104
|
+
};
|
|
1105
|
+
/**
|
|
1106
|
+
* ``key``'s site blocked the batch (``why``): the publisher's papers wait, the others go on. ``offset``, the paper it
|
|
1107
|
+
* blocked, waits with them; none when the paper was saved and only its files were refused as too many requests.
|
|
1108
|
+
*/
|
|
1109
|
+
const pauseBlocked = (key, why, offset) => {
|
|
1110
|
+
const until = now() + BLOCKED_PAUSE_MS;
|
|
1111
|
+
held.set(key, [...(held.get(key) ?? []), ...(offset === undefined ? [] : [offset])]);
|
|
1112
|
+
blockedUntil.set(key, until);
|
|
1113
|
+
log(`[批量] ${key} 的网站拦截了访问(${why}):这家出版社的篇目先暂停,${clockTime(until)} 再试一次,其他照常下载`);
|
|
1114
|
+
ledger?.phase(`暂停 ${key}:被网站拦截`);
|
|
1115
|
+
void reportBlocked();
|
|
1116
|
+
};
|
|
1117
|
+
/** Pauses with none of their publisher's papers left to wait (its files refused on its last paper): not waited out. */
|
|
1118
|
+
const dropEmptyPauses = () => {
|
|
1119
|
+
for (const key of [...blockedUntil.keys()]) {
|
|
1120
|
+
if (held.get(key)?.length)
|
|
1121
|
+
continue;
|
|
1122
|
+
blockedUntil.delete(key);
|
|
1123
|
+
held.delete(key);
|
|
1124
|
+
}
|
|
1125
|
+
};
|
|
1126
|
+
/** ``offset``'s publisher's site blocked it again after the pause, or at the batch's end: given up for this batch. */
|
|
1127
|
+
const giveUp = (offset) => {
|
|
1128
|
+
const key = publisherOf(offset);
|
|
1129
|
+
if (givenUp.has(key))
|
|
1130
|
+
return;
|
|
1131
|
+
givenUp.add(key);
|
|
1132
|
+
log(`[批量] ${key} 的网站${triedAgain.has(key) ? "暂停后仍" : ""}拦截了访问(${id(offset)}):这家出版社余下的篇目本批不再试` +
|
|
1133
|
+
"(没试的记为被网站拦截,原因不明的不再重试,没权限的不再换学校);过一阵说“接着下”再补");
|
|
1134
|
+
ledger?.phase(`不再试 ${key}:仍被网站拦截`);
|
|
1135
|
+
};
|
|
1136
|
+
/** Without 失败后自动重试: each publisher's papers blocked in a row (BLOCKED_IN_ROW of them give it up). */
|
|
1137
|
+
const blockedInRow = new Map();
|
|
1138
|
+
const blockedOnce = (offset) => {
|
|
1139
|
+
const key = publisherOf(offset);
|
|
1140
|
+
const count = (blockedInRow.get(key) ?? 0) + 1;
|
|
1141
|
+
blockedInRow.set(key, count);
|
|
1142
|
+
if (count >= BLOCKED_IN_ROW)
|
|
1143
|
+
giveUp(offset);
|
|
1144
|
+
else
|
|
1145
|
+
log(`[批量] ${key} 的网站拦截了 ${id(offset)}(连续第 ${count} 篇):这一篇记为失败,这家出版社的其他篇目接着试`);
|
|
1146
|
+
};
|
|
1147
|
+
/** Blocked publishers whose pause is over: their papers go first, once more. */
|
|
1148
|
+
const releaseBlocked = () => {
|
|
1149
|
+
let released = 0;
|
|
1150
|
+
for (const [key, until] of blockedUntil) {
|
|
1151
|
+
if (now() < until)
|
|
1152
|
+
continue;
|
|
1153
|
+
const offsets = held.get(key) ?? [];
|
|
1154
|
+
blockedUntil.delete(key);
|
|
1155
|
+
held.delete(key);
|
|
1156
|
+
// A pause nothing waited in (only a paper's files were refused) is no try: its next block pauses again.
|
|
1157
|
+
if (!offsets.length)
|
|
1158
|
+
continue;
|
|
1159
|
+
triedAgain.add(key);
|
|
1160
|
+
queue.unshift(...offsets);
|
|
1161
|
+
released += offsets.length;
|
|
1162
|
+
log(`[批量] ${key} 已暂停 ${BLOCKED_PAUSE_MS / 60_000} 分钟:暂停的 ${offsets.length} 篇再试一次`);
|
|
1163
|
+
ledger?.phase(`再试 ${key} 暂停的 ${offsets.length} 篇`);
|
|
1164
|
+
}
|
|
1165
|
+
if (released)
|
|
1166
|
+
notify();
|
|
1167
|
+
};
|
|
1168
|
+
/** The user pressed 登录并继续 (or 终止) in the window: held papers go on first. Blocked ones whose pause is over too. */
|
|
806
1169
|
const takeRequests = () => {
|
|
1170
|
+
releaseBlocked();
|
|
807
1171
|
const asked = backend?.takeBatchRequests?.(batchId);
|
|
808
1172
|
if (!asked)
|
|
809
1173
|
return;
|
|
@@ -811,6 +1175,8 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
811
1175
|
stopped = true;
|
|
812
1176
|
let resumed = 0;
|
|
813
1177
|
for (const key of asked.resume) {
|
|
1178
|
+
if (blockedUntil.has(key))
|
|
1179
|
+
continue;
|
|
814
1180
|
const offsets = held.get(key) ?? [];
|
|
815
1181
|
held.delete(key);
|
|
816
1182
|
queue.unshift(...offsets);
|
|
@@ -823,22 +1189,32 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
823
1189
|
notify();
|
|
824
1190
|
}
|
|
825
1191
|
};
|
|
1192
|
+
/** A paper its publisher's pause left untried: its school stage says so (``code``), or, its PDF from open access standing, its files. */
|
|
1193
|
+
const untried = (offset, code, files = code) => {
|
|
1194
|
+
const item = results[offset];
|
|
1195
|
+
if (finishing.has(offset)) {
|
|
1196
|
+
// Its PDF stands; its browser's part waited.
|
|
1197
|
+
if (wantsFiles)
|
|
1198
|
+
item.supplements = [{ status: "skipped", label: "", reason: files }];
|
|
1199
|
+
}
|
|
1200
|
+
else {
|
|
1201
|
+
const stage = { stage: "entitled_access", status: "error", ...schoolAttemptDetails(code) };
|
|
1202
|
+
results[offset] = itemResult({ ...item, status: "error", attempts: [...item.attempts, stage] }, offset + 1, requests[offset]);
|
|
1203
|
+
// The run record says it too, as `paper-search failures` reads it: there such a paper had no school stage, "没有走到
|
|
1204
|
+
// 机构下载" beside the batch's 被网站拦截 (BMJ, 2026-11-23: 1 + 47 against 48).
|
|
1205
|
+
ledger?.stage(entries[offset], { ...stage, startedAt: new Date().toISOString(), durationMs: 0,
|
|
1206
|
+
trace: { publisher: publisherOf(offset) } });
|
|
1207
|
+
}
|
|
1208
|
+
};
|
|
826
1209
|
/** The held papers end as paused (the batch stopped): returns them, no longer held. */
|
|
827
1210
|
const markPaused = () => {
|
|
828
1211
|
const paused = [...held.values()].flat();
|
|
829
|
-
for (const
|
|
830
|
-
const
|
|
831
|
-
|
|
832
|
-
// Its PDF stands; its browser's part waited for the login.
|
|
833
|
-
if (wantsFiles)
|
|
834
|
-
item.supplements = [{ status: "skipped", label: "", reason: "batch_stopped" }];
|
|
835
|
-
}
|
|
836
|
-
else {
|
|
837
|
-
results[offset] = itemResult({ ...item, status: "error", attempts: [...item.attempts,
|
|
838
|
-
{ stage: "entitled_access", status: "error", ...schoolAttemptDetails("login_paused") }] }, offset + 1, requests[offset]);
|
|
839
|
-
}
|
|
1212
|
+
for (const [key, offsets] of held) {
|
|
1213
|
+
for (const offset of offsets)
|
|
1214
|
+
untried(offset, blockedUntil.has(key) ? "blocked_paused" : "login_paused", "batch_stopped");
|
|
840
1215
|
}
|
|
841
1216
|
held.clear();
|
|
1217
|
+
blockedUntil.clear();
|
|
842
1218
|
return paused;
|
|
843
1219
|
};
|
|
844
1220
|
/**
|
|
@@ -859,9 +1235,51 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
859
1235
|
forward?.(record);
|
|
860
1236
|
} };
|
|
861
1237
|
};
|
|
862
|
-
//
|
|
863
|
-
//
|
|
864
|
-
//
|
|
1238
|
+
// 没权限时换下一所学校 (PaperSearch Pro): the other saved schools, asked once a paper has gone without access (the
|
|
1239
|
+
// backend the batch has by then can say). Such papers wait, not finished, and go to them at the end, all of them with
|
|
1240
|
+
// one school before the next (``laterSchools``, user 2026-11-22).
|
|
1241
|
+
let turns;
|
|
1242
|
+
const otherSchools = () => (turns ??= schoolsAfterDefault(backend, options));
|
|
1243
|
+
// 失败后自动重试 (retrySetting): asked once, when the first paper might be tried again.
|
|
1244
|
+
let retryOn;
|
|
1245
|
+
const retrying = () => (retryOn ??= retrySetting(backend));
|
|
1246
|
+
/** Papers without access with the schools tried so far, waiting for the next one. */
|
|
1247
|
+
const withoutAccessYet = new Set();
|
|
1248
|
+
/** Papers failed for a reason not known, waiting for their one more try at the end. */
|
|
1249
|
+
const unexplainedYet = new Set();
|
|
1250
|
+
/** Each waiting paper's schools tried, the default one counted. */
|
|
1251
|
+
const schoolsTried = new Map();
|
|
1252
|
+
// The papers held for a later try, by why, are in the window's ring beside those done, not in 剩余 unsaid (user,
|
|
1253
|
+
// 2026-11-23): every report to it says them, and one of its own does before a paper's school visit when they changed.
|
|
1254
|
+
/** The papers held for a later try now. */
|
|
1255
|
+
const later = () => {
|
|
1256
|
+
const waiting = (blocked) => [...held].filter(([key]) => blockedUntil.has(key) === blocked).flatMap(([, offsets]) => offsets);
|
|
1257
|
+
const until = [...blockedUntil].filter(([key]) => held.get(key)?.length).map(([, at]) => at);
|
|
1258
|
+
const dois = (offsets) => [...offsets].map((offset) => requests[offset].doi).filter(Boolean);
|
|
1259
|
+
const blocked = waiting(true), login = waiting(false);
|
|
1260
|
+
return { noAccess: withoutAccessYet.size, unexplained: unexplainedYet.size, blocked: blocked.length, login: login.length,
|
|
1261
|
+
...(until.length ? { blockedAt: Math.min(...until) } : {}),
|
|
1262
|
+
papers: { noAccess: dois(withoutAccessYet), unexplained: dois(unexplainedYet), blocked: dois(blocked), login: dois(login) } };
|
|
1263
|
+
};
|
|
1264
|
+
/** What the window was last told of them. */
|
|
1265
|
+
let laterSaid = JSON.stringify(later());
|
|
1266
|
+
/** A report to the window with them (``later``). */
|
|
1267
|
+
const withLater = (update) => {
|
|
1268
|
+
const counts = later();
|
|
1269
|
+
laterSaid = JSON.stringify(counts);
|
|
1270
|
+
return { ...update, later: counts };
|
|
1271
|
+
};
|
|
1272
|
+
/** The window told of papers held for a later try, or back from it, when no report has said so; none once all are done. */
|
|
1273
|
+
const reportLater = () => {
|
|
1274
|
+
if (!backend?.reportBatchProgress || JSON.stringify(later()) === laterSaid || settledOk() + failed >= total)
|
|
1275
|
+
return reports;
|
|
1276
|
+
const update = withLater({ id: batchId, total, done: settledOk() + failed, succeeded: settledOk(), failed, current: "",
|
|
1277
|
+
finished: false, laterOnly: true });
|
|
1278
|
+
reports = reports.then(() => backend.reportBatchProgress(update)).catch(() => false);
|
|
1279
|
+
return reports;
|
|
1280
|
+
};
|
|
1281
|
+
// One journal's papers one after another when the backend says so (more than one school taking part, user
|
|
1282
|
+
// 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.
|
|
865
1283
|
let groupJournals;
|
|
866
1284
|
let lastJournal = "";
|
|
867
1285
|
const entitledLane = async () => {
|
|
@@ -880,6 +1298,19 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
880
1298
|
held.get(publisherOf(offset)).push(offset);
|
|
881
1299
|
continue;
|
|
882
1300
|
}
|
|
1301
|
+
if (givenUp.has(publisherOf(offset))) {
|
|
1302
|
+
// Its publisher's site still blocks after the pause: not tried, 被网站拦截.
|
|
1303
|
+
untried(offset, "blocked_paused");
|
|
1304
|
+
if (finishing.delete(offset))
|
|
1305
|
+
openSettled += 1;
|
|
1306
|
+
else
|
|
1307
|
+
failed += 1;
|
|
1308
|
+
record(offset);
|
|
1309
|
+
settle(offset);
|
|
1310
|
+
await sendPapers();
|
|
1311
|
+
continue;
|
|
1312
|
+
}
|
|
1313
|
+
await reportLater();
|
|
883
1314
|
if (finishing.has(offset)) {
|
|
884
1315
|
await finishOpenItem(offset);
|
|
885
1316
|
if (stopped)
|
|
@@ -890,15 +1321,21 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
890
1321
|
continue;
|
|
891
1322
|
}
|
|
892
1323
|
const request = requests[offset];
|
|
893
|
-
|
|
1324
|
+
// Papers waiting for the end (without access, 原因不明) counted too: the log goes 1/7, 2/7 … as the window does
|
|
1325
|
+
// (Windows 2026-11-22: every line said 1/7 while the papers waited).
|
|
1326
|
+
const step = succeeded + failed + withoutAccessYet.size + unexplainedYet.size + 1;
|
|
894
1327
|
log(openRunning
|
|
895
1328
|
? `[批量] 机构下载第 ${step} 篇(开放来源还在查):${id(offset)}`
|
|
896
1329
|
: `[批量] 机构下载 ${step}/${missed.length}:${id(offset)}`);
|
|
897
1330
|
const done = settledOk() + failed;
|
|
898
|
-
|
|
1331
|
+
// Its place among the papers gone through, those waiting for the end counted (the window's "k / N" goes on);
|
|
1332
|
+
// the counts are of those finished.
|
|
1333
|
+
const batch = { id: batchId, index: done + withoutAccessYet.size + unexplainedYet.size + 1, total, done,
|
|
1334
|
+
succeeded: settledOk(), failed };
|
|
899
1335
|
let result;
|
|
900
1336
|
try {
|
|
901
|
-
|
|
1337
|
+
// Reported to the window once it is known whether the paper waits for the end (``reportBatchItem`` below).
|
|
1338
|
+
result = await pastNetworkLoss(() => pastHourlyLimit(() => downloadWithFallback(searchers, watchingSchool(recorded({ ...request, batch, ...(away ? { shortUserWait: true } : {}) }, ledger, entries[offset]), offset), createTiers(backend).filter((tier) => tier.stage === "entitled_access" || mode !== "entitled_only" && tier.stage === "preprint")), log, pause, backend), watch);
|
|
902
1339
|
}
|
|
903
1340
|
catch (error) {
|
|
904
1341
|
result = laneError("entitled_access", error);
|
|
@@ -918,11 +1355,17 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
918
1355
|
}, offset + 1, request);
|
|
919
1356
|
const school = result.attempts.find((attempt) => attempt.stage === "entitled_access");
|
|
920
1357
|
const holding = result.status !== "ok" && wentThrough(offset) && school?.code === LOGIN_LAPSED_CODE
|
|
921
|
-
&& waitEnded.get(offset) === SCHOOL_SIGN_IN_PAGE && !stoppedByUser(result) && await canHold();
|
|
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));
|
|
922
1364
|
// Its Markdown now, from the page this visit saved, or the open sources (only these
|
|
923
|
-
// while its school login is in doubt).
|
|
924
|
-
if (wantsMarkdown && !stoppedByUser(result)) {
|
|
925
|
-
await markdownPastNetworkLoss(results[offset], recorded({ ...request, batch }, ledger, entries[offset]), searchers, holding
|
|
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);
|
|
926
1369
|
}
|
|
927
1370
|
if (result.status === "ok") {
|
|
928
1371
|
succeeded += 1;
|
|
@@ -930,17 +1373,52 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
930
1373
|
settle(offset);
|
|
931
1374
|
if (school?.status === "ok")
|
|
932
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
|
+
}
|
|
933
1386
|
}
|
|
934
1387
|
else if (holding) {
|
|
935
1388
|
hold(offset);
|
|
936
1389
|
}
|
|
1390
|
+
else if (pausing) {
|
|
1391
|
+
pauseBlocked(publisherOf(offset), `${id(offset)}:人机验证没过或拦截页`, offset);
|
|
1392
|
+
}
|
|
1393
|
+
else if (blocked) {
|
|
1394
|
+
// Blocked again after its pause: this one fails 被网站拦截, its publisher's others are not tried. Without 失败后自动
|
|
1395
|
+
// 重试: this one fails, its publisher's others go on until BLOCKED_IN_ROW in a row (blockedOnce).
|
|
1396
|
+
if (triedAgain.has(publisherOf(offset)))
|
|
1397
|
+
giveUp(offset);
|
|
1398
|
+
else
|
|
1399
|
+
blockedOnce(offset);
|
|
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
|
+
}
|
|
937
1412
|
else {
|
|
938
1413
|
failed += 1;
|
|
939
1414
|
record(offset);
|
|
940
1415
|
settle(offset);
|
|
941
1416
|
}
|
|
942
|
-
|
|
943
|
-
|
|
1417
|
+
// The window's line, not for a paper that waits for the end or its publisher's pause (it would close the batch
|
|
1418
|
+
// early when last).
|
|
1419
|
+
if (!withoutAccessYet.has(offset) && !unexplainedYet.has(offset) && !pausing) {
|
|
1420
|
+
await reportBatchItem({ ...request, batch: { ...batch, index: batch.done + 1 } }, result, backend);
|
|
1421
|
+
}
|
|
944
1422
|
if (stoppedByUser(result)) {
|
|
945
1423
|
stopped = true;
|
|
946
1424
|
return;
|
|
@@ -970,18 +1448,156 @@ async function downloadTwoPhase(searchers, options, backend, log, createTiers, m
|
|
|
970
1448
|
// The held publishers wait for the user as long as it takes (user, 2026-11-15): the
|
|
971
1449
|
// window shows 登录并继续 and 终止. 登录并继续 downloads them, the school's login asking
|
|
972
1450
|
// the user where it lapsed; held again, the wait starts over. 终止 ends them as paused.
|
|
1451
|
+
// Those blocked by their site wait out their pause and go on by themselves (user, 2026-11-23).
|
|
1452
|
+
dropEmptyPauses();
|
|
973
1453
|
if (held.size && !stopped) {
|
|
974
|
-
const waiting =
|
|
975
|
-
|
|
976
|
-
|
|
1454
|
+
const waiting = heldList().reduce((sum, publisher) => sum + publisher.count, 0);
|
|
1455
|
+
if (waiting) {
|
|
1456
|
+
log(`[批量] 其他篇目已完成,暂停的 ${waiting} 篇等你回来:在进度窗口点“登录并继续”,或点“终止”结束`);
|
|
1457
|
+
ledger?.phase(`等你回来:暂停的 ${waiting} 篇`);
|
|
1458
|
+
}
|
|
1459
|
+
const blockedWaiting = [...blockedUntil.keys()].reduce((sum, key) => sum + (held.get(key)?.length ?? 0), 0);
|
|
1460
|
+
if (blockedWaiting) {
|
|
1461
|
+
log(`[批量] 其他篇目已完成,被网站拦截暂停的 ${blockedWaiting} 篇 ${clockTime(Math.min(...blockedUntil.values()))} 再试一次`);
|
|
1462
|
+
ledger?.phase(`等网站解除拦截:暂停的 ${blockedWaiting} 篇`);
|
|
1463
|
+
}
|
|
977
1464
|
}
|
|
978
1465
|
while (held.size && !stopped) {
|
|
979
|
-
await reportHeld(true);
|
|
1466
|
+
await (heldList().length ? reportHeld(true) : reportBlocked());
|
|
980
1467
|
await pause(HOLD_POLL_MS);
|
|
981
1468
|
takeRequests();
|
|
982
1469
|
if (queue.length && !stopped) {
|
|
983
1470
|
await entitledLane();
|
|
984
1471
|
}
|
|
1472
|
+
dropEmptyPauses();
|
|
1473
|
+
}
|
|
1474
|
+
/** A paper's school stage once more, with another saved school when given; merged into its result. */
|
|
1475
|
+
const visitAgain = async (offset, school) => {
|
|
1476
|
+
await reportLater();
|
|
1477
|
+
const request = { ...requests[offset], ...(school ? { school: school.entityId } : {}) };
|
|
1478
|
+
const done = settledOk() + failed;
|
|
1479
|
+
const batch = { id: batchId, index: done + 1, total, done, succeeded: settledOk(), failed };
|
|
1480
|
+
log(school ? `[批量] 换${school.name}:${id(offset)}` : `[批量] 再试一次:${id(offset)}`);
|
|
1481
|
+
let result;
|
|
1482
|
+
try {
|
|
1483
|
+
result = await pastNetworkLoss(() => pastHourlyLimit(() => downloadWithFallback(searchers, watchingSchool(recorded({ ...request, batch, ...(away ? { shortUserWait: true } : {}) }, ledger, entries[offset]), offset), createTiers(backend).filter((tier) => tier.stage === "entitled_access")), log, pause, backend), watch);
|
|
1484
|
+
}
|
|
1485
|
+
catch (error) {
|
|
1486
|
+
result = laneError("entitled_access", error);
|
|
1487
|
+
log(`[批量] 机构下载出错(${id(offset)}):${result.attempts[0].message}`);
|
|
1488
|
+
}
|
|
1489
|
+
const before = results[offset];
|
|
1490
|
+
const markdown = result.markdown ?? before.markdown;
|
|
1491
|
+
results[offset] = itemResult({
|
|
1492
|
+
...result,
|
|
1493
|
+
attempts: [...before.attempts, ...result.attempts],
|
|
1494
|
+
...(result.review ?? before.review ? { review: result.review ?? before.review } : {}),
|
|
1495
|
+
...(markdown ? { markdown } : {}),
|
|
1496
|
+
}, offset + 1, requests[offset]);
|
|
1497
|
+
const item = results[offset];
|
|
1498
|
+
// Its Markdown from the page this visit saved, unless the open sources gave it already.
|
|
1499
|
+
if (wantsMarkdown && result.status === "ok" && item.markdown?.status !== "ok") {
|
|
1500
|
+
await markdownPastNetworkLoss(item, recorded({ ...request, batch }, ledger, entries[offset]), searchers, pageVisit(backend, { ...request, batch, ...(away ? { shortUserWait: true } : {}) }, log, pause), watch);
|
|
1501
|
+
}
|
|
1502
|
+
if (stoppedByUser(result))
|
|
1503
|
+
stopped = true;
|
|
1504
|
+
// Its publisher's site blocks the batch's end too: no more visits go there (no pause at the end).
|
|
1505
|
+
if (siteBlocked(result))
|
|
1506
|
+
giveUp(offset);
|
|
1507
|
+
return item;
|
|
1508
|
+
};
|
|
1509
|
+
/** Not visited again at the batch's end: its publisher's site still blocks (``giveUp``). */
|
|
1510
|
+
const visitable = (offset) => !givenUp.has(publisherOf(offset));
|
|
1511
|
+
/** The window's line for a paper finished at the end (the counts before it, as ``reportBatchItem`` takes them). */
|
|
1512
|
+
const reportSettled = async (offset) => {
|
|
1513
|
+
const done = settledOk() + failed;
|
|
1514
|
+
await reportBatchItem({ ...requests[offset], batch: { id: batchId, index: done + 1, total, done, succeeded: settledOk(), failed } }, results[offset], backend);
|
|
1515
|
+
};
|
|
1516
|
+
// 原因不明 once more, the default school still signed in, before any other school (user, 2026-11-22). Not where the
|
|
1517
|
+
// site still blocks: those end as they were.
|
|
1518
|
+
const unexplainedAgain = [...unexplainedYet].filter(visitable);
|
|
1519
|
+
if (unexplainedAgain.length && !stopped) {
|
|
1520
|
+
log(`[批量] 原因不明的 ${unexplainedAgain.length} 篇再试一次`);
|
|
1521
|
+
ledger?.phase(`原因不明的 ${unexplainedAgain.length} 篇再试一次`);
|
|
1522
|
+
for (const offset of unexplainedAgain) {
|
|
1523
|
+
if (stopped)
|
|
1524
|
+
break;
|
|
1525
|
+
if (!visitable(offset))
|
|
1526
|
+
continue;
|
|
1527
|
+
const item = await visitAgain(offset);
|
|
1528
|
+
unexplainedYet.delete(offset);
|
|
1529
|
+
if (!stopped && item.status !== "ok" && withoutAccess(item) && (await otherSchools()).length) {
|
|
1530
|
+
withoutAccessYet.add(offset);
|
|
1531
|
+
}
|
|
1532
|
+
else {
|
|
1533
|
+
await reportSettled(offset);
|
|
1534
|
+
if (item.status === "ok")
|
|
1535
|
+
succeeded += 1;
|
|
1536
|
+
else
|
|
1537
|
+
failed += 1;
|
|
1538
|
+
record(offset);
|
|
1539
|
+
settle(offset);
|
|
1540
|
+
}
|
|
1541
|
+
await sendPapers();
|
|
1542
|
+
}
|
|
1543
|
+
}
|
|
1544
|
+
for (const offset of unexplainedYet) {
|
|
1545
|
+
await reportSettled(offset);
|
|
1546
|
+
failed += 1;
|
|
1547
|
+
record(offset);
|
|
1548
|
+
}
|
|
1549
|
+
unexplainedYet.clear();
|
|
1550
|
+
// The papers without access go to the other saved schools, one school at a time; not where the site still blocks
|
|
1551
|
+
// (they end without access, with the schools tried so far).
|
|
1552
|
+
const withSchool = async (offset, school) => {
|
|
1553
|
+
if (!visitable(offset))
|
|
1554
|
+
return;
|
|
1555
|
+
const item = await visitAgain(offset, school);
|
|
1556
|
+
schoolsTried.set(offset, (schoolsTried.get(offset) ?? 1) + 1);
|
|
1557
|
+
if (!stopped && withoutAccess(item)) {
|
|
1558
|
+
triedSchools(item, schoolsTried.get(offset));
|
|
1559
|
+
}
|
|
1560
|
+
else {
|
|
1561
|
+
withoutAccessYet.delete(offset);
|
|
1562
|
+
await reportSettled(offset);
|
|
1563
|
+
if (item.status === "ok")
|
|
1564
|
+
succeeded += 1;
|
|
1565
|
+
else
|
|
1566
|
+
failed += 1;
|
|
1567
|
+
record(offset);
|
|
1568
|
+
settle(offset);
|
|
1569
|
+
}
|
|
1570
|
+
await sendPapers();
|
|
1571
|
+
};
|
|
1572
|
+
if (withoutAccessYet.size && !stopped) {
|
|
1573
|
+
await laterSchools(await otherSchools(), () => [...withoutAccessYet].filter(visitable), async (school, count) => {
|
|
1574
|
+
log(`[批量] 没权限的 ${count} 篇换${school.name}再试`);
|
|
1575
|
+
ledger?.phase(`没权限的 ${count} 篇换${school.name}再试`);
|
|
1576
|
+
if (!backend?.reportBatchProgress)
|
|
1577
|
+
return;
|
|
1578
|
+
// The card names the school a moment (switchTo), in place of a line saying so.
|
|
1579
|
+
const update = withLater({ id: batchId, total, done: settledOk() + failed, succeeded: settledOk(), failed, current: "",
|
|
1580
|
+
finished: false, switchTo: school.name, laterOnly: true });
|
|
1581
|
+
reports = reports.then(() => backend.reportBatchProgress(update)).catch(() => false);
|
|
1582
|
+
await reports;
|
|
1583
|
+
}, withSchool, () => stopped);
|
|
1584
|
+
}
|
|
1585
|
+
// Without access with every school (or the batch stopped first): failed.
|
|
1586
|
+
for (const offset of withoutAccessYet) {
|
|
1587
|
+
await reportSettled(offset);
|
|
1588
|
+
failed += 1;
|
|
1589
|
+
record(offset);
|
|
1590
|
+
}
|
|
1591
|
+
withoutAccessYet.clear();
|
|
1592
|
+
// Markdown saved without some figures: those once more, unless the user stopped the batch (user, 2026-11-23);
|
|
1593
|
+
// recorded before a stop's own marks on the papers not recorded yet.
|
|
1594
|
+
figuresTried = true;
|
|
1595
|
+
if (!stopped) {
|
|
1596
|
+
await figuresAtTheEnd([...figuresLater].map((offset) => ({ result: results[offset], request: recorded(requests[offset], ledger, entries[offset]) })), log, ledger);
|
|
1597
|
+
}
|
|
1598
|
+
for (const offset of figuresLater) {
|
|
1599
|
+
record(offset);
|
|
1600
|
+
settle(offset);
|
|
985
1601
|
}
|
|
986
1602
|
await reports;
|
|
987
1603
|
// Stopped in the window: the items neither lane reached end as not downloaded, the held as paused.
|