papersearch 0.7.3 → 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 +8 -37
- package/README.fr.md +265 -0
- package/README.ja.md +265 -0
- package/README.ko.md +265 -0
- package/README.md +149 -148
- package/README.zh-Hant.md +265 -0
- package/README.zh.md +265 -0
- package/dist/capabilities/download/batch/batch.d.ts +20 -1
- package/dist/capabilities/download/batch/batch.d.ts.map +1 -1
- package/dist/capabilities/download/batch/batch.js +1057 -208
- 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 +3 -2
- package/dist/capabilities/download/batch/batchFolder.js.map +1 -1
- package/dist/capabilities/download/batch/networkLoss.d.ts +11 -2
- package/dist/capabilities/download/batch/networkLoss.d.ts.map +1 -1
- package/dist/capabilities/download/batch/networkLoss.js +24 -3
- 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/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/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 +22 -10
- 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 +46 -3
- 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/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/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/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 +14 -0
- package/dist/infrastructure/runlog/RunLedger.d.ts.map +1 -1
- package/dist/infrastructure/runlog/RunLedger.js +30 -4
- package/dist/infrastructure/runlog/RunLedger.js.map +1 -1
- package/dist/infrastructure/runlog/failureClass.d.ts +2 -1
- package/dist/infrastructure/runlog/failureClass.d.ts.map +1 -1
- package/dist/infrastructure/runlog/failureClass.js +27 -4
- 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 +48 -5
- package/dist/infrastructure/school/SchoolAttempt.js.map +1 -1
- package/dist/infrastructure/school/SchoolMcpClient.d.ts +13 -2
- package/dist/infrastructure/school/SchoolMcpClient.d.ts.map +1 -1
- package/dist/infrastructure/school/SchoolMcpClient.js +138 -15
- 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 +9 -0
- package/dist/infrastructure/school/SchoolReadiness.d.ts.map +1 -1
- package/dist/infrastructure/school/SchoolReadiness.js +10 -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 +73 -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 +23 -0
- package/dist/management/failures/index.d.ts.map +1 -1
- package/dist/management/failures/index.js +68 -20
- 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 +8 -3
- 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 +19 -2
- 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 +52 -13
- package/school/paper_search_school/adapters/oup/__init__.py +7 -4
- package/school/paper_search_school/adapters/platforms/__init__.py +4 -1
- package/school/paper_search_school/adapters/platforms/atypon/__init__.py +12 -3
- package/school/paper_search_school/adapters/platforms/figshare/__init__.py +23 -4
- package/school/paper_search_school/adapters/platforms/jstage/__init__.py +64 -0
- package/school/paper_search_school/adapters/taylor_francis/__init__.py +0 -3
- package/school/paper_search_school/adapters/thieme/__init__.py +2 -9
- 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 +526 -147
- package/school/paper_search_school/assets/capsule_en.json +140 -49
- package/school/paper_search_school/assets/capsule_fr.json +141 -50
- package/school/paper_search_school/assets/capsule_ja.json +140 -49
- package/school/paper_search_school/assets/capsule_ko.json +140 -49
- package/school/paper_search_school/assets/capsule_zh-Hant.json +134 -48
- package/school/paper_search_school/browser/backend.py +125 -4
- package/school/paper_search_school/browser/batch_session.py +189 -8
- package/school/paper_search_school/browser/challenge/__init__.py +12 -9
- package/school/paper_search_school/browser/challenge/continuation.py +83 -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 +128 -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/pro.py +24 -0
- package/school/paper_search_school/browser/challenge/services/__init__.py +1 -0
- package/school/paper_search_school/browser/challenge/services/solver.py +292 -0
- 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/cli_config.py +10 -5
- package/school/paper_search_school/config.py +16 -10
- package/school/paper_search_school/download_diagnostics.py +7 -2
- package/school/paper_search_school/engine/access_rules.py +12 -3
- package/school/paper_search_school/engine/batch.py +52 -18
- package/school/paper_search_school/engine/batch_access.py +62 -16
- package/school/paper_search_school/engine/batch_capture.py +260 -31
- 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 +52 -3
- package/school/paper_search_school/engine/batch_login.py +398 -36
- package/school/paper_search_school/engine/batch_pdf_urls.py +38 -7
- package/school/paper_search_school/engine/batch_user.py +48 -3
- package/school/paper_search_school/engine/batch_waits.py +221 -14
- package/school/paper_search_school/engine/bridge.py +29 -6
- package/school/paper_search_school/engine/fulltext_page.py +55 -8
- package/school/paper_search_school/engine/router.py +16 -1
- package/school/paper_search_school/engine/supplements.py +7 -1
- package/school/paper_search_school/entitlements.py +29 -1
- package/school/paper_search_school/pdf_utils.py +25 -16
- package/school/paper_search_school/progress_capsule.py +69 -11
- package/school/paper_search_school/progress_reporter.py +243 -11
- package/school/paper_search_school/progress_view.py +205 -91
- 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/site.py +12 -4
- 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/school_test.py +1 -1
- package/school/paper_search_school/server.py +179 -23
- package/school/paper_search_school/user_settings.py +46 -4
- package/school/pyproject.toml +1 -0
- package/skills/paper-search/SKILL.md +107 -107
- package/skills/paper-search/references/browser-download.md +102 -99
- 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/README.en.md +0 -264
- package/school/paper_search_school/browser/challenge/prompt.py +0 -154
|
@@ -37,6 +37,15 @@ _STATE: dict[str, Any] = {}
|
|
|
37
37
|
_STARTED_MONO: float = 0.0
|
|
38
38
|
_STAGE_LISTENER: ContextVar[tuple[str, Callable[[str], None]] | None] = ContextVar(
|
|
39
39
|
"school_request_stage_listener", default=None)
|
|
40
|
+
#: 并行下载 (user, 2026-11-29): the window of a batch whose papers run at once that the current download runs in
|
|
41
|
+
#: (server.py binds it); None for a download on its own or a batch one paper after another.
|
|
42
|
+
_WINDOW: ContextVar[int | None] = ContextVar("school_progress_window", default=None)
|
|
43
|
+
#: What the card says of the paper downloading. In a batch whose papers run at once, each window's paper keeps its own
|
|
44
|
+
#: under ``papers`` (by window), and the card's own fields are those of the paper it shows (``_show``). The browser's
|
|
45
|
+
#: processes (``browser_pid``, ``browser_pids``) are the batch's: its windows are one Chrome (user's Mac, 2026-11-29: a
|
|
46
|
+
#: click on a row said 未能切到下载浏览器, the paper shown being one whose window had none to say).
|
|
47
|
+
PAPER_KEYS = ("current", "title", "journal", "publisher", "notice", "stage", "phase", "part", "transfer",
|
|
48
|
+
"supplements_saved", "supplements_total", "page_reading", "content", "markdown")
|
|
40
49
|
|
|
41
50
|
STAGES = frozenset({
|
|
42
51
|
"starting", "opening_article", "waiting_login", "waiting_verification",
|
|
@@ -276,6 +285,42 @@ def _batch_state(batch_id: str) -> dict[str, Any] | None:
|
|
|
276
285
|
PAPER_FIELDS = ("title", "journal", "publisher", "notice")
|
|
277
286
|
|
|
278
287
|
|
|
288
|
+
def bind_window(window: int | None):
|
|
289
|
+
"""The current download's window in a batch whose papers run at once (None: none); the token to unbind."""
|
|
290
|
+
return _WINDOW.set(window)
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def unbind_window(token) -> None:
|
|
294
|
+
_WINDOW.reset(token)
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def _window_paper() -> dict[str, Any] | None:
|
|
298
|
+
"""Under _LOCK: the current window's paper in a batch whose papers run at once ({} once it has left the card:
|
|
299
|
+
what it says then goes nowhere); None for a download that is no window's."""
|
|
300
|
+
window = _WINDOW.get()
|
|
301
|
+
if window is None or not _STATE:
|
|
302
|
+
return None
|
|
303
|
+
papers = _STATE.get("papers")
|
|
304
|
+
return papers.get(str(window), {}) if isinstance(papers, dict) else {}
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def _show() -> None:
|
|
308
|
+
"""Under _LOCK: the card shows one of the papers downloading at once: the one asking for the user, else the one
|
|
309
|
+
downloading longest (it stays until it ends). Its fields become the card's."""
|
|
310
|
+
papers = _STATE.get("papers") if _STATE else None
|
|
311
|
+
if not isinstance(papers, dict) or not papers:
|
|
312
|
+
return
|
|
313
|
+
asking = [str(entry.get("current") or "") for entry in (_STATE.get("attention") or {}).values() if isinstance(entry, dict)]
|
|
314
|
+
shown = next((paper for paper in papers.values() if asking and paper.get("current") in asking), None)
|
|
315
|
+
if shown is None:
|
|
316
|
+
shown = min(papers.values(), key=lambda paper: paper.get("since") or 0)
|
|
317
|
+
for key in PAPER_KEYS:
|
|
318
|
+
if key in shown:
|
|
319
|
+
_STATE[key] = shown[key]
|
|
320
|
+
elif key not in ("current", "stage", "phase"):
|
|
321
|
+
_STATE.pop(key, None)
|
|
322
|
+
|
|
323
|
+
|
|
279
324
|
def _adopt_batch(batch_id: str, total: int, counts: tuple[int | None, int | None, int | None],
|
|
280
325
|
*, beside_download: bool = False, **fields: Any) -> bool | None:
|
|
281
326
|
"""Continue this batch's report in the current process, or start it (True).
|
|
@@ -296,6 +341,10 @@ def _adopt_batch(batch_id: str, total: int, counts: tuple[int | None, int | None
|
|
|
296
341
|
pass
|
|
297
342
|
previous = _batch_state(batch_id)
|
|
298
343
|
with _LOCK:
|
|
344
|
+
if (not beside_download and _STATE.get("batch_id") == batch_id and _STATE.get("status") == "running"
|
|
345
|
+
and _STATE.get("papers") and _WINDOW.get() is None):
|
|
346
|
+
# Papers downloading at once: a report of the caller changes the totals and its lines only.
|
|
347
|
+
beside_download = True
|
|
299
348
|
if beside_download:
|
|
300
349
|
if (_STATE.get("batch_id") == batch_id and _STATE.get("status") == "running"
|
|
301
350
|
and not _STATE.get("awaiting_next")):
|
|
@@ -303,6 +352,7 @@ def _adopt_batch(batch_id: str, total: int, counts: tuple[int | None, int | None
|
|
|
303
352
|
_STATE.update(total=int(total), lanes=fields.get("lanes", ""),
|
|
304
353
|
**({"held": fields["held"]} if "held" in fields else {}),
|
|
305
354
|
**({"held_wait": fields["held_wait"]} if "held_wait" in fields else {}),
|
|
355
|
+
**({"later": fields["later"]} if "later" in fields else {}),
|
|
306
356
|
**({"done": done} if done is not None else {}),
|
|
307
357
|
**({"success": succeeded} if succeeded is not None else {}),
|
|
308
358
|
**({"failed": failed} if failed is not None else {}))
|
|
@@ -328,6 +378,31 @@ def _adopt_batch(batch_id: str, total: int, counts: tuple[int | None, int | None
|
|
|
328
378
|
"revision": 0,
|
|
329
379
|
}
|
|
330
380
|
done, succeeded, failed = counts
|
|
381
|
+
window = _WINDOW.get()
|
|
382
|
+
if _STATE.get("batch_id") == batch_id and _STATE.get("status") in ("running", "done"):
|
|
383
|
+
previous = _STATE # this process's own report, newer than the file it read above
|
|
384
|
+
if window is not None and previous is not None and previous.get("status") == "running":
|
|
385
|
+
# A window's paper of a batch whose papers run at once: the others' papers and prompts stay, and the counts
|
|
386
|
+
# its download came with (the caller's when it started) never take back what was counted since.
|
|
387
|
+
papers = previous.get("papers") if isinstance(previous.get("papers"), dict) else {}
|
|
388
|
+
paper = {"since": time.time(), **{k: v for k, v in fields.items() if k in PAPER_KEYS}}
|
|
389
|
+
_STATE = {
|
|
390
|
+
**previous,
|
|
391
|
+
"batch_id": batch_id,
|
|
392
|
+
"total": int(total),
|
|
393
|
+
"status": "running",
|
|
394
|
+
"awaiting_next": False,
|
|
395
|
+
"owner_pid": os.getpid(),
|
|
396
|
+
"papers": {**papers, str(window): paper},
|
|
397
|
+
**{k: v for k, v in fields.items() if k not in PAPER_KEYS},
|
|
398
|
+
**{key: max(int(previous.get(key) or 0), value)
|
|
399
|
+
for key, value in (("done", done), ("success", succeeded), ("failed", failed)) if value is not None},
|
|
400
|
+
}
|
|
401
|
+
_STATE.pop("finished_at", None)
|
|
402
|
+
_STATE.pop("results", None)
|
|
403
|
+
_show()
|
|
404
|
+
_write_state()
|
|
405
|
+
return True
|
|
331
406
|
_STATE = {
|
|
332
407
|
**previous,
|
|
333
408
|
"batch_id": batch_id,
|
|
@@ -341,21 +416,30 @@ def _adopt_batch(batch_id: str, total: int, counts: tuple[int | None, int | None
|
|
|
341
416
|
**({"failed": failed} if failed is not None else {}),
|
|
342
417
|
**fields,
|
|
343
418
|
}
|
|
419
|
+
if window is not None:
|
|
420
|
+
_STATE["papers"] = {str(window): {"since": time.time(), **{k: v for k, v in fields.items() if k in PAPER_KEYS}}}
|
|
344
421
|
_STATE.pop("finished_at", None)
|
|
345
422
|
_STATE.pop("results", None)
|
|
346
423
|
_write_state()
|
|
347
424
|
return True
|
|
348
425
|
|
|
349
426
|
|
|
350
|
-
|
|
427
|
+
def _held_key(value: Any) -> str:
|
|
428
|
+
"""A held publisher's key as the caller sent it, which 登录并继续 sends back unchanged: the
|
|
429
|
+
publisher's name ("Science"), else its DOI prefix ("10.1126/"); empty when it is not one short line.
|
|
430
|
+
The batch keys them by name since 2026-11-17 (a publisher's other DOI prefixes wait with it); a
|
|
431
|
+
check for DOI prefixes alone dropped every one of them, and the window showed no 登录并继续 (2026-11-23)."""
|
|
432
|
+
if not isinstance(value, str) or not 0 < len(value) <= 120 or not value.isprintable() or value != value.strip():
|
|
433
|
+
return ""
|
|
434
|
+
return value
|
|
351
435
|
|
|
352
436
|
|
|
353
437
|
def _held(raw: Any) -> list[dict[str, Any]]:
|
|
354
438
|
"""The publishers whose papers the caller holds (their school login stopped working
|
|
355
|
-
mid-batch): [{key (
|
|
439
|
+
mid-batch): [{key (``_held_key``), name, count}], at most four, checked for the window."""
|
|
356
440
|
held: list[dict[str, Any]] = []
|
|
357
441
|
for entry in raw if isinstance(raw, list) else []:
|
|
358
|
-
if not isinstance(entry, dict) or not
|
|
442
|
+
if not isinstance(entry, dict) or not _held_key(entry.get("key")):
|
|
359
443
|
continue
|
|
360
444
|
count = entry.get("count")
|
|
361
445
|
if not isinstance(count, int) or isinstance(count, bool) or not 0 < count <= 1_000_000:
|
|
@@ -365,10 +449,35 @@ def _held(raw: Any) -> list[dict[str, Any]]:
|
|
|
365
449
|
return held[:4]
|
|
366
450
|
|
|
367
451
|
|
|
452
|
+
#: Why the caller holds a paper for a later try (``sync_batch``'s ``later``).
|
|
453
|
+
LATER_KINDS = ("no_access", "unexplained", "blocked", "login")
|
|
454
|
+
|
|
455
|
+
|
|
456
|
+
def _later(raw: Any) -> dict[str, Any]:
|
|
457
|
+
"""The papers the caller holds for a later try, by why (LATER_KINDS), and when the blocked ones are tried
|
|
458
|
+
again (``blocked_at``, epoch seconds), checked for the window."""
|
|
459
|
+
raw = raw if isinstance(raw, dict) else {}
|
|
460
|
+
|
|
461
|
+
def count(value: Any) -> int:
|
|
462
|
+
return value if isinstance(value, int) and not isinstance(value, bool) and 0 <= value <= _MAX_BATCH_PAPERS else 0
|
|
463
|
+
|
|
464
|
+
later: dict[str, Any] = {kind: count(raw.get(kind)) for kind in LATER_KINDS}
|
|
465
|
+
at = raw.get("blocked_at")
|
|
466
|
+
if later["blocked"] and isinstance(at, (int, float)) and not isinstance(at, bool) and at > 0:
|
|
467
|
+
later["blocked_at"] = float(at)
|
|
468
|
+
# Each kind's papers by DOI (user, 2026-11-24: the window's 重试 list names them; 剩余 lists the others), as many as
|
|
469
|
+
# it counts at most; none from a caller that does not send them.
|
|
470
|
+
papers = raw.get("papers")
|
|
471
|
+
if isinstance(papers, dict):
|
|
472
|
+
later["papers"] = {kind: [doi for doi in papers.get(kind) if isinstance(doi, str) and 0 < len(doi) <= 300][:later[kind]]
|
|
473
|
+
for kind in LATER_KINDS if isinstance(papers.get(kind), list)}
|
|
474
|
+
return later
|
|
475
|
+
|
|
476
|
+
|
|
368
477
|
def sync_batch(batch_id: str, total: int, done: int, succeeded: int, failed: int, *,
|
|
369
478
|
current: str = "", finished: bool = False, results: Any = None,
|
|
370
479
|
phase: str = "", channel: str = "", lanes: str = "", held: Any = None,
|
|
371
|
-
held_wait: bool = False) -> bool:
|
|
480
|
+
held_wait: bool = False, later: Any = None, switch_to: str = "") -> bool:
|
|
372
481
|
"""Record batch totals for items finished without the backend, or the batch end.
|
|
373
482
|
|
|
374
483
|
``results`` (the batch end only) is the caller's summary of every item,
|
|
@@ -382,7 +491,10 @@ def sync_batch(batch_id: str, total: int, done: int, succeeded: int, failed: int
|
|
|
382
491
|
stage and prompts (``_adopt_batch``). ``held``: the publishers the caller holds
|
|
383
492
|
(``_held``), each with a button to go on with them; an empty list clears them.
|
|
384
493
|
``held_wait``: nothing else is left and the batch waits for the user to go on with
|
|
385
|
-
them or end it (each report says it again; any other report, or a download, ends it).
|
|
494
|
+
them or end it (each report says it again; any other report, or a download, ends it).
|
|
495
|
+
``later``: the papers the caller holds for a later try (``_later``), kept until the next
|
|
496
|
+
report that says them; dropped at the batch end. ``switch_to``: the school the caller now
|
|
497
|
+
tries its papers without access with, which the card names for a moment (``switch_note``)."""
|
|
386
498
|
ctx = BatchContext.from_args(batch_id, max(1, min(done, total)), total, done, succeeded, failed)
|
|
387
499
|
if ctx is None or done > total:
|
|
388
500
|
return False
|
|
@@ -391,12 +503,19 @@ def sync_batch(batch_id: str, total: int, done: int, succeeded: int, failed: int
|
|
|
391
503
|
if finished and previous is None and (done == 0 or summary is not None):
|
|
392
504
|
return False
|
|
393
505
|
phase, channel, lanes = _short_line(phase), _short_line(channel), _short_line(lanes)
|
|
506
|
+
school = _short_line(switch_to, 40)
|
|
507
|
+
if finished:
|
|
508
|
+
with _LOCK:
|
|
509
|
+
if _STATE.get("batch_id") == ctx.batch_id:
|
|
510
|
+
_STATE.pop("papers", None) # the batch's end: no window's paper is left on the card
|
|
394
511
|
adopted = _adopt_batch(ctx.batch_id, total, (done, succeeded, failed),
|
|
395
512
|
beside_download=bool(lanes) and not finished,
|
|
396
513
|
**({"current": current} if current else {}),
|
|
397
514
|
**({"lanes": lanes} if lanes or finished else {}),
|
|
398
515
|
**({"held": [] if finished else _held(held)} if held is not None or finished else {}),
|
|
399
516
|
held_wait=bool(held_wait) and not finished,
|
|
517
|
+
**({"later": {} if finished else _later(later)} if later is not None or finished else {}),
|
|
518
|
+
**({"switch_note": {"school": school, "at": time.time()}} if school and not finished else {}),
|
|
400
519
|
stage="saved_pending_verification" if finished else "starting",
|
|
401
520
|
channel="" if finished else channel,
|
|
402
521
|
phase=(f"批量下载结束:成功 {succeeded},失败 {failed}" if finished
|
|
@@ -687,6 +806,16 @@ def tracked_download(current: str, output_dir: Any = None, batch: BatchContext |
|
|
|
687
806
|
_STATE.pop("held_wait", None) # nor waiting for the user to go on with held publishers
|
|
688
807
|
update(content=content if content in ("main", "supplements", "both", "markdown") else "main",
|
|
689
808
|
markdown=bool(markdown), page_reading=False)
|
|
809
|
+
if batch is not None:
|
|
810
|
+
with _LOCK:
|
|
811
|
+
# What the batch was asked for, kept from its first article: its lists say what its failed papers did not
|
|
812
|
+
# get by it. ``content`` is the running article's (the card's label), and the batch's end asks for the
|
|
813
|
+
# Markdown alone of those short of figures: the failed list's heading said "Markdown 未生成" a while
|
|
814
|
+
# (Windows, 2026-11-24).
|
|
815
|
+
if _STATE and _STATE.get("batch_content_of") != batch.batch_id:
|
|
816
|
+
_STATE["batch_content"] = _STATE.get("content")
|
|
817
|
+
_STATE["batch_content_of"] = batch.batch_id
|
|
818
|
+
_write_state()
|
|
690
819
|
if output_dir:
|
|
691
820
|
set_output_dir(output_dir)
|
|
692
821
|
name_journal(current)
|
|
@@ -694,7 +823,9 @@ def tracked_download(current: str, output_dir: Any = None, batch: BatchContext |
|
|
|
694
823
|
try:
|
|
695
824
|
yield outcome
|
|
696
825
|
finally:
|
|
697
|
-
if
|
|
826
|
+
if _WINDOW.get() is not None and batch is not None:
|
|
827
|
+
_leave_window(outcome)
|
|
828
|
+
elif outcome.get("paused") and batch is not None:
|
|
698
829
|
with _LOCK:
|
|
699
830
|
if _STATE:
|
|
700
831
|
_STATE.update(awaiting_next=True, phase=str(outcome["paused"]),
|
|
@@ -704,6 +835,29 @@ def tracked_download(current: str, output_dir: Any = None, batch: BatchContext |
|
|
|
704
835
|
_finish_download(outcome, current, batch, content)
|
|
705
836
|
|
|
706
837
|
|
|
838
|
+
def _leave_window(outcome: dict[str, Any]) -> None:
|
|
839
|
+
"""A window's paper of a batch whose papers run at once ended (or waits for its publisher's hour): it leaves the
|
|
840
|
+
card, which shows another one downloading; with none left the card waits for the next, as between two papers of
|
|
841
|
+
a batch. The caller counts it (its next report)."""
|
|
842
|
+
with _LOCK:
|
|
843
|
+
if not _STATE:
|
|
844
|
+
return
|
|
845
|
+
papers = _STATE.get("papers") if isinstance(_STATE.get("papers"), dict) else {}
|
|
846
|
+
papers.pop(str(_WINDOW.get()), None)
|
|
847
|
+
attention = _STATE.get("attention") if isinstance(_STATE.get("attention"), dict) else {}
|
|
848
|
+
current = str(((outcome.get("result") or {}).get("identifier")) or "")
|
|
849
|
+
for key in [key for key, entry in attention.items() if isinstance(entry, dict) and current
|
|
850
|
+
and str(entry.get("current") or "").lower() == current.lower()]:
|
|
851
|
+
attention.pop(key, None)
|
|
852
|
+
if papers:
|
|
853
|
+
_show()
|
|
854
|
+
else:
|
|
855
|
+
_STATE.pop("papers", None)
|
|
856
|
+
_STATE.update(awaiting_next=True, transfer=None, phase=str(outcome.get("paused") or "")
|
|
857
|
+
or f"已完成 {int(_STATE.get('done') or 0)}/{int(_STATE.get('total') or 0)} 篇,等待下一篇")
|
|
858
|
+
_write_state()
|
|
859
|
+
|
|
860
|
+
|
|
707
861
|
def _finish_download(outcome: dict[str, Any], current: str, batch: BatchContext | None,
|
|
708
862
|
content: str = "main") -> None:
|
|
709
863
|
"""Count the download and show how it ended (end of ``tracked_download``)."""
|
|
@@ -747,7 +901,11 @@ def name_journal(current: str, look_up: Callable[[str], dict[str, Any]] | None =
|
|
|
747
901
|
if look_up is None and os.environ.get("SCHOOL_PROGRESS_BAR") == "0":
|
|
748
902
|
return None # no card to say it on (and no lookups from the tests)
|
|
749
903
|
with _LOCK:
|
|
750
|
-
|
|
904
|
+
paper = _window_paper()
|
|
905
|
+
if paper is not None:
|
|
906
|
+
if paper.get("journal") or str(paper.get("current") or "") != current or not current:
|
|
907
|
+
return None
|
|
908
|
+
elif not _STATE or _STATE.get("journal") or str(_STATE.get("current") or "") != current or not current:
|
|
751
909
|
return None
|
|
752
910
|
|
|
753
911
|
def look() -> None:
|
|
@@ -763,6 +921,13 @@ def name_journal(current: str, look_up: Callable[[str], dict[str, Any]] | None =
|
|
|
763
921
|
if not name:
|
|
764
922
|
return
|
|
765
923
|
with _LOCK:
|
|
924
|
+
papers = _STATE.get("papers") if _STATE and isinstance(_STATE.get("papers"), dict) else {}
|
|
925
|
+
for paper in papers.values():
|
|
926
|
+
if str(paper.get("current") or "") == current and not paper.get("journal"):
|
|
927
|
+
paper["journal"] = name
|
|
928
|
+
_show()
|
|
929
|
+
_write_state()
|
|
930
|
+
return
|
|
766
931
|
if _STATE and str(_STATE.get("current") or "") == current and not _STATE.get("journal"):
|
|
767
932
|
_STATE["journal"] = name
|
|
768
933
|
_write_state()
|
|
@@ -882,10 +1047,69 @@ def update(**kw: Any) -> None:
|
|
|
882
1047
|
with _LOCK:
|
|
883
1048
|
if not _STATE:
|
|
884
1049
|
return
|
|
885
|
-
|
|
1050
|
+
paper = _window_paper()
|
|
1051
|
+
if paper is not None:
|
|
1052
|
+
paper.update({k: v for k, v in kw.items() if k in PAPER_KEYS})
|
|
1053
|
+
_STATE.update({k: v for k, v in kw.items() if k not in PAPER_KEYS})
|
|
1054
|
+
_show()
|
|
1055
|
+
else:
|
|
1056
|
+
_STATE.update(kw)
|
|
886
1057
|
_write_state()
|
|
887
1058
|
|
|
888
1059
|
|
|
1060
|
+
#: A file arriving is written into the report at most this often (the window reads it twice a second).
|
|
1061
|
+
TRANSFER_WRITE_SEC = 0.5
|
|
1062
|
+
|
|
1063
|
+
|
|
1064
|
+
class Transfer:
|
|
1065
|
+
"""One file arriving, said on the window's card (``transfer``: the PDF or a supplementary file, which of
|
|
1066
|
+
how many, its name, the bytes so far and in all, when it started and when bytes last came). User,
|
|
1067
|
+
2026-11-26: a big file's size and how far it has come, so that a slow one is not taken for a stuck
|
|
1068
|
+
one. A context manager: the report forgets it when it ends (another's, started since, is kept)."""
|
|
1069
|
+
|
|
1070
|
+
def __init__(self, kind: str, size: int = 0, *, index: int = 0, count: int = 0, name: str = "") -> None:
|
|
1071
|
+
self.kind, self.index, self.count, self.name = kind, int(index or 0), int(count or 0), str(name or "")[:120]
|
|
1072
|
+
self.size = max(0, int(size or 0))
|
|
1073
|
+
self.done = 0
|
|
1074
|
+
self.started = self.at = time.time()
|
|
1075
|
+
self._written = 0.0
|
|
1076
|
+
self._id = uuid.uuid4().hex[:12]
|
|
1077
|
+
|
|
1078
|
+
def __enter__(self) -> "Transfer":
|
|
1079
|
+
self._write()
|
|
1080
|
+
return self
|
|
1081
|
+
|
|
1082
|
+
def __exit__(self, *exc: Any) -> None:
|
|
1083
|
+
self.close()
|
|
1084
|
+
|
|
1085
|
+
def add(self, count: int) -> None:
|
|
1086
|
+
"""``count`` more bytes have come."""
|
|
1087
|
+
if count <= 0:
|
|
1088
|
+
return
|
|
1089
|
+
self.done += count
|
|
1090
|
+
self.at = time.time()
|
|
1091
|
+
if self.at - self._written >= TRANSFER_WRITE_SEC:
|
|
1092
|
+
self._write()
|
|
1093
|
+
|
|
1094
|
+
def _write(self) -> None:
|
|
1095
|
+
self._written = time.time()
|
|
1096
|
+
update(transfer={"id": self._id, "kind": self.kind, "index": self.index, "count": self.count,
|
|
1097
|
+
"name": self.name, "size": self.size, "done": self.done,
|
|
1098
|
+
"started": self.started, "at": self.at})
|
|
1099
|
+
|
|
1100
|
+
def close(self) -> None:
|
|
1101
|
+
with _LOCK:
|
|
1102
|
+
holder = _window_paper()
|
|
1103
|
+
holder = _STATE if holder is None else holder
|
|
1104
|
+
current = holder.get("transfer")
|
|
1105
|
+
if not (isinstance(current, dict) and current.get("id") == self._id):
|
|
1106
|
+
return
|
|
1107
|
+
holder["transfer"] = None
|
|
1108
|
+
if holder is not _STATE:
|
|
1109
|
+
_show()
|
|
1110
|
+
_write_state()
|
|
1111
|
+
|
|
1112
|
+
|
|
889
1113
|
def batch_id() -> str:
|
|
890
1114
|
"""The caller's serial batch the current download belongs to; "" for a download on its own."""
|
|
891
1115
|
with _LOCK:
|
|
@@ -896,12 +1120,15 @@ def current_part() -> str:
|
|
|
896
1120
|
"""The part of the paper the window says is being fetched ("main", "supplements",
|
|
897
1121
|
"markdown"), or ""."""
|
|
898
1122
|
with _LOCK:
|
|
899
|
-
|
|
1123
|
+
paper = _window_paper()
|
|
1124
|
+
return str((_STATE if paper is None else paper).get("part") or "")
|
|
900
1125
|
|
|
901
1126
|
|
|
902
1127
|
def advance(ok: bool, current: str = "", phase: str = "", *, pending: bool = False) -> None:
|
|
903
1128
|
"""Record one finished item (the hot path for per-paper lanes); ``pending``:
|
|
904
1129
|
finished, but whether it succeeded is the caller's to count."""
|
|
1130
|
+
if _WINDOW.get() is not None:
|
|
1131
|
+
return # papers downloading at once: the batch's caller counts each (its reports)
|
|
905
1132
|
with _LOCK:
|
|
906
1133
|
if not _STATE:
|
|
907
1134
|
return
|
|
@@ -924,9 +1151,10 @@ def has_attention(*, current: str = "", excluding: str = "") -> bool:
|
|
|
924
1151
|
|
|
925
1152
|
|
|
926
1153
|
def set_attention(key: str, message: str, *, current: str = "", phase: str = "",
|
|
927
|
-
deadline: float | None = None) -> None:
|
|
1154
|
+
deadline: float | None = None, ways: list[str] | None = None, school: str = "") -> None:
|
|
928
1155
|
"""Show a manual-action reminder without stopping other workers; ``deadline``
|
|
929
|
-
(epoch seconds) is when the wait ends, for the window's countdown.
|
|
1156
|
+
(epoch seconds) is when the wait ends, for the window's countdown. ``ways``: the ways a sign-in chooser
|
|
1157
|
+
offers, picked from in the window itself (with ``school``, whose they are; user, 2026-11-29)."""
|
|
930
1158
|
with _LOCK:
|
|
931
1159
|
if not _STATE:
|
|
932
1160
|
return
|
|
@@ -942,6 +1170,9 @@ def set_attention(key: str, message: str, *, current: str = "", phase: str = "",
|
|
|
942
1170
|
}
|
|
943
1171
|
if deadline is not None:
|
|
944
1172
|
attention[str(key)]["deadline"] = float(deadline)
|
|
1173
|
+
if ways:
|
|
1174
|
+
attention[str(key)].update(ways=[str(way) for way in ways], school=str(school or ""))
|
|
1175
|
+
_show() # papers downloading at once: the card shows the one asking
|
|
945
1176
|
_write_state()
|
|
946
1177
|
|
|
947
1178
|
|
|
@@ -976,6 +1207,7 @@ def clear_attention(key: str) -> None:
|
|
|
976
1207
|
if not isinstance(attention, dict):
|
|
977
1208
|
return
|
|
978
1209
|
attention.pop(str(key), None)
|
|
1210
|
+
_show()
|
|
979
1211
|
_write_state()
|
|
980
1212
|
|
|
981
1213
|
|