litloop 1.18.3__py3-none-any.whl
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.
- litloop/__init__.py +89 -0
- litloop/__main__.py +6 -0
- litloop/_source_hygiene.py +158 -0
- litloop/agents/__init__.py +13 -0
- litloop/agents/client.py +164 -0
- litloop/agents/runner.py +123 -0
- litloop/agents/system.py +54 -0
- litloop/agents/tools.py +142 -0
- litloop/app_server.py +8 -0
- litloop/browser/__init__.py +1 -0
- litloop/browser/backend.py +1009 -0
- litloop/browser/camofox_api.py +368 -0
- litloop/browser/chrome_copy.py +136 -0
- litloop/browser/cloakbrowser_compat.py +72 -0
- litloop/browser/cookies.py +347 -0
- litloop/browser/engine.py +965 -0
- litloop/browser/login.py +636 -0
- litloop/browser/pdf.py +263 -0
- litloop/browser/profile_health.py +167 -0
- litloop/browser/runtime.py +210 -0
- litloop/cache.py +104 -0
- litloop/citations/__init__.py +1 -0
- litloop/citations/bib_import.py +102 -0
- litloop/citations/bibtex_crossref.py +143 -0
- litloop/citations/formats.py +225 -0
- litloop/citations/paper_list.py +200 -0
- litloop/citations/session_export.py +94 -0
- litloop/cli/__init__.py +12 -0
- litloop/cli/_utils.py +267 -0
- litloop/cli/commands.py +1098 -0
- litloop/cli/doctor.py +141 -0
- litloop/cli/main.py +1173 -0
- litloop/cli/web.py +190 -0
- litloop/config.py +413 -0
- litloop/data/VARCON-LICENSE.txt +62 -0
- litloop/data/elements.tsv +119 -0
- litloop/data/institutional_identity_policy.json +165 -0
- litloop/data/publisher_access_catalog.json +969 -0
- litloop/data/publisher_carsi.json +105 -0
- litloop/data/spelling_search.tsv.gz +0 -0
- litloop/data/spelling_us.tsv.gz +0 -0
- litloop/data/webvpn.dat +0 -0
- litloop/data/webvpn.json +603 -0
- litloop/db.py +1922 -0
- litloop/deps.py +128 -0
- litloop/desktop/__init__.py +1 -0
- litloop/desktop/agent_runner.py +537 -0
- litloop/desktop/download_jobs.py +571 -0
- litloop/desktop/error_text.py +192 -0
- litloop/desktop/server.py +999 -0
- litloop/desktop/settings.py +552 -0
- litloop/desktop/translate.py +103 -0
- litloop/diagnostics.py +126 -0
- litloop/download/__init__.py +1 -0
- litloop/download/doi_check.py +44 -0
- litloop/download/fetcher.py +975 -0
- litloop/download/markdown.py +130 -0
- litloop/download/pdf.py +454 -0
- litloop/download/pipeline.py +1166 -0
- litloop/download/progress.py +312 -0
- litloop/download/rename.py +211 -0
- litloop/extractors/__init__.py +0 -0
- litloop/extractors/html_extractor.py +31 -0
- litloop/extractors/pdf_extractor.py +151 -0
- litloop/extractors/publisher_adapters/__init__.py +0 -0
- litloop/extractors/publisher_adapters/acs.py +167 -0
- litloop/extractors/publisher_adapters/elsevier.py +178 -0
- litloop/extractors/publisher_adapters/generic.py +191 -0
- litloop/extractors/publisher_adapters/nature.py +204 -0
- litloop/extractors/publisher_adapters/rsc.py +154 -0
- litloop/extractors/publisher_adapters/tandfonline.py +154 -0
- litloop/extractors/publisher_adapters/wiley.py +160 -0
- litloop/http_utils.py +57 -0
- litloop/identifiers.py +134 -0
- litloop/institutional/__init__.py +1 -0
- litloop/institutional/auth.py +576 -0
- litloop/institutional/carsi_login.py +569 -0
- litloop/institutional/config_adapter.py +63 -0
- litloop/institutional/proxy_check.py +98 -0
- litloop/institutional/remote_assist.py +274 -0
- litloop/institutional/schools.py +102 -0
- litloop/institutional/session_heal.py +73 -0
- litloop/institutional/session_store.py +105 -0
- litloop/log.py +47 -0
- litloop/models.py +269 -0
- litloop/network.py +417 -0
- litloop/optional.py +310 -0
- litloop/publishers/__init__.py +1 -0
- litloop/publishers/access.py +232 -0
- litloop/publishers/pdf_router.py +451 -0
- litloop/publishers/profiles.py +899 -0
- litloop/publishers/strategies/__init__.py +122 -0
- litloop/publishers/strategies/_core.py +2340 -0
- litloop/publishers/strategies/_elsevier_api.py +578 -0
- litloop/publishers/strategies/acm.py +27 -0
- litloop/publishers/strategies/acs.py +42 -0
- litloop/publishers/strategies/aip.py +27 -0
- litloop/publishers/strategies/aps.py +29 -0
- litloop/publishers/strategies/asce.py +28 -0
- litloop/publishers/strategies/base.py +108 -0
- litloop/publishers/strategies/copernicus.py +24 -0
- litloop/publishers/strategies/elsevier.py +66 -0
- litloop/publishers/strategies/generic.py +39 -0
- litloop/publishers/strategies/ieee.py +28 -0
- litloop/publishers/strategies/iop.py +27 -0
- litloop/publishers/strategies/nature.py +27 -0
- litloop/publishers/strategies/oxford.py +27 -0
- litloop/publishers/strategies/registry.py +83 -0
- litloop/publishers/strategies/royalsociety.py +28 -0
- litloop/publishers/strategies/rsc.py +27 -0
- litloop/publishers/strategies/sage.py +30 -0
- litloop/publishers/strategies/science.py +27 -0
- litloop/publishers/strategies/springer.py +34 -0
- litloop/publishers/strategies/tandfonline.py +32 -0
- litloop/publishers/strategies/wiley.py +32 -0
- litloop/publishers/supplementary.py +219 -0
- litloop/quota.py +202 -0
- litloop/search/__init__.py +1 -0
- litloop/search/abstracts.py +291 -0
- litloop/search/deep.py +1216 -0
- litloop/search/quick.py +1102 -0
- litloop/search/scansci_find.py +410 -0
- litloop/search/screening.py +225 -0
- litloop/search/title_to_doi.py +163 -0
- litloop/search/watches.py +124 -0
- litloop/server.py +1731 -0
- litloop/sources/__init__.py +1339 -0
- litloop/sources/_batch.py +529 -0
- litloop/sources/_index.py +221 -0
- litloop/sources/_routes.py +290 -0
- litloop/sources/altcha.py +90 -0
- litloop/sources/arxiv.py +45 -0
- litloop/sources/attempt.py +91 -0
- litloop/sources/carsi.py +74 -0
- litloop/sources/core_api.py +46 -0
- litloop/sources/crossref.py +162 -0
- litloop/sources/elsevier_api.py +620 -0
- litloop/sources/elsevier_check.py +274 -0
- litloop/sources/europepmc.py +81 -0
- litloop/sources/ezproxy.py +317 -0
- litloop/sources/instsci.py +1295 -0
- litloop/sources/libgen.py +116 -0
- litloop/sources/nature.py +39 -0
- litloop/sources/oa_discovery.py +135 -0
- litloop/sources/openaire.py +122 -0
- litloop/sources/openalex.py +100 -0
- litloop/sources/publishers.py +684 -0
- litloop/sources/sage_cn.py +139 -0
- litloop/sources/scibban.py +10 -0
- litloop/sources/scihub.py +1379 -0
- litloop/sources/scihub_domains.py +203 -0
- litloop/sources/scihub_speed.py +187 -0
- litloop/sources/scoring.py +250 -0
- litloop/sources/semantic_scholar.py +146 -0
- litloop/sources/springer_tdm.py +172 -0
- litloop/sources/unpaywall.py +116 -0
- litloop/sources/vpnsci.py +38 -0
- litloop/store.py +264 -0
- litloop/templates/index.html +347 -0
- litloop/terms/__init__.py +1 -0
- litloop/terms/glossary.py +422 -0
- litloop/terms/spelling.py +89 -0
- litloop/terms/text.py +106 -0
- litloop/tor/__init__.py +1 -0
- litloop/tor/embedded.py +371 -0
- litloop/tor/proxy.py +110 -0
- litloop/updates.py +148 -0
- litloop/zotero/__init__.py +1 -0
- litloop/zotero/bridge.py +309 -0
- litloop/zotero/client.py +317 -0
- litloop-1.18.3.dist-info/METADATA +347 -0
- litloop-1.18.3.dist-info/RECORD +176 -0
- litloop-1.18.3.dist-info/WHEEL +5 -0
- litloop-1.18.3.dist-info/entry_points.txt +2 -0
- litloop-1.18.3.dist-info/licenses/LICENSE +197 -0
- litloop-1.18.3.dist-info/top_level.txt +1 -0
litloop/cli/commands.py
ADDED
|
@@ -0,0 +1,1098 @@
|
|
|
1
|
+
"""LitLoop command line: the `litloop` commands besides the server (main.py)."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import logging
|
|
5
|
+
import os
|
|
6
|
+
import sys
|
|
7
|
+
import time
|
|
8
|
+
from datetime import datetime
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from urllib.parse import urlparse
|
|
11
|
+
|
|
12
|
+
# Fix Windows console encoding for Unicode output
|
|
13
|
+
if sys.platform == "win32":
|
|
14
|
+
os.environ.setdefault("PYTHONIOENCODING", "utf-8")
|
|
15
|
+
try:
|
|
16
|
+
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
17
|
+
sys.stderr.reconfigure(encoding="utf-8", errors="replace")
|
|
18
|
+
except Exception:
|
|
19
|
+
pass
|
|
20
|
+
|
|
21
|
+
import typer
|
|
22
|
+
from rich.console import Console
|
|
23
|
+
from rich.table import Table
|
|
24
|
+
|
|
25
|
+
from ..config import load_config, save_config
|
|
26
|
+
from ..download.fetcher import PaperFetcher
|
|
27
|
+
from ..institutional.schools import get_school, list_schools, search_schools
|
|
28
|
+
from ..sources import semantic_scholar
|
|
29
|
+
|
|
30
|
+
app = typer.Typer(
|
|
31
|
+
name="litloop",
|
|
32
|
+
help="Fetch academic papers via institutional access, Open Access, or arXiv.",
|
|
33
|
+
no_args_is_help=True,
|
|
34
|
+
)
|
|
35
|
+
console = Console()
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _setup_logging(verbose: bool = False):
|
|
39
|
+
level = logging.DEBUG if verbose else logging.INFO
|
|
40
|
+
logging.basicConfig(
|
|
41
|
+
level=level,
|
|
42
|
+
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
|
|
43
|
+
datefmt="%H:%M:%S",
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _ensure_email(config: dict):
|
|
48
|
+
"""Prompt user to set email if not configured (needed for Unpaywall)."""
|
|
49
|
+
if not config.get("email", ""):
|
|
50
|
+
console.print("[yellow]Email not configured (needed for Unpaywall OA detection).[/yellow]")
|
|
51
|
+
email = typer.prompt("Enter your email address")
|
|
52
|
+
config["email"] = email
|
|
53
|
+
save_config(config)
|
|
54
|
+
console.print(f"[green]Email saved: {email}[/green]")
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _school_type_label(school_type: str) -> str:
|
|
58
|
+
return {
|
|
59
|
+
"webvpn": "CampusPortal",
|
|
60
|
+
"easyconnect": "CampusConnector",
|
|
61
|
+
"atrust": "CampusConnector",
|
|
62
|
+
"ezproxy": "LibraryPortal",
|
|
63
|
+
}.get(school_type, school_type)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _read_dois(path: Path) -> list[str]:
|
|
67
|
+
"""Read DOIs from a text file, one per line, skipping comments and blanks."""
|
|
68
|
+
return [
|
|
69
|
+
line.strip()
|
|
70
|
+
for line in path.read_text(encoding="utf-8").splitlines()
|
|
71
|
+
if line.strip() and not line.strip().startswith("#")
|
|
72
|
+
]
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _print_batch_summary(summary: dict, *, extra_lines: list[str] | None = None) -> None:
|
|
76
|
+
"""Print a standard batch download summary."""
|
|
77
|
+
console.print(
|
|
78
|
+
f"[bold]Done:[/bold] {summary['success']}/{summary['count']} verified PDFs, "
|
|
79
|
+
f"{summary.get('unverified', 0)} unverified PDFs."
|
|
80
|
+
)
|
|
81
|
+
console.print(f"[dim]PDF dir: {summary['pdf_dir']}[/dim]")
|
|
82
|
+
console.print(f"[dim]Manifest: {summary['manifest']}[/dim]")
|
|
83
|
+
# Auto-stop banner: make clear the batch was halted, not finished cleanly.
|
|
84
|
+
if summary.get("auto_stopped"):
|
|
85
|
+
n = summary.get("ip_blocked_count", 0)
|
|
86
|
+
console.print(
|
|
87
|
+
f"[yellow bold]⚠ 已自动停止:[/yellow bold]连续检测到 IP 被出版商封禁"
|
|
88
|
+
f"({n} 篇返回 ip_blocked),剩余任务已取消。"
|
|
89
|
+
)
|
|
90
|
+
console.print(
|
|
91
|
+
"[yellow]应对:调低 batch_workers、增大 request_delay_min/max,"
|
|
92
|
+
"或参考 README「被封 IP」章节。[/yellow]"
|
|
93
|
+
)
|
|
94
|
+
for line in extra_lines or []:
|
|
95
|
+
console.print(line)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _installed_package_version(name: str) -> str:
|
|
99
|
+
import importlib.metadata
|
|
100
|
+
try:
|
|
101
|
+
return importlib.metadata.version(name)
|
|
102
|
+
except importlib.metadata.PackageNotFoundError:
|
|
103
|
+
return ""
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
# Minimum recommended cloakbrowser version. cloakbrowser iterates on
|
|
107
|
+
# fingerprint patches and anti-detection fixes very frequently; older versions
|
|
108
|
+
# fall behind bot-detection heuristics and start failing silently. This is a
|
|
109
|
+
# soft floor surfaced by `doctor`/`check`/`browser-doctor` (offline, no network),
|
|
110
|
+
# not a hard install requirement. Bump it whenever you pin a new floor in
|
|
111
|
+
# pyproject.toml.
|
|
112
|
+
CLOAKBROWSER_RECOMMENDED_MIN = "0.5.9"
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def _parse_version_tuple(v: str) -> tuple[int, ...]:
|
|
116
|
+
parts: list[int] = []
|
|
117
|
+
for token in (v or "").split("."):
|
|
118
|
+
# Only the leading digits count: "3b1" is release 3 of a pre-release,
|
|
119
|
+
# not 31, which would compare higher than a plain "3".
|
|
120
|
+
digits = ""
|
|
121
|
+
for ch in token:
|
|
122
|
+
if not ch.isdigit():
|
|
123
|
+
break
|
|
124
|
+
digits += ch
|
|
125
|
+
parts.append(int(digits) if digits else 0)
|
|
126
|
+
return tuple(parts)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def _cloakbrowser_version_status() -> tuple[str, str]:
|
|
130
|
+
"""Return (status, detail) for the installed cloakbrowser.
|
|
131
|
+
|
|
132
|
+
status is one of: not_installed / outdated / ok. detail carries the version
|
|
133
|
+
(and an upgrade hint when outdated). Kept offline: anti-detection tooling
|
|
134
|
+
version checks should not phone home.
|
|
135
|
+
"""
|
|
136
|
+
version = _installed_package_version("cloakbrowser")
|
|
137
|
+
if not version:
|
|
138
|
+
return ("not_installed", "not installed (pip install 'litloop[cloakbrowser]')")
|
|
139
|
+
if _parse_version_tuple(version) < _parse_version_tuple(CLOAKBROWSER_RECOMMENDED_MIN):
|
|
140
|
+
hint = f"{version} (outdated, recommend >={CLOAKBROWSER_RECOMMENDED_MIN}: pip install -U cloakbrowser)"
|
|
141
|
+
return ("outdated", hint)
|
|
142
|
+
return ("ok", version)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def _doctor_checks() -> list[tuple[str, str, str]]:
|
|
146
|
+
import shutil
|
|
147
|
+
checks: list[tuple[str, str, str]] = [
|
|
148
|
+
("Python runtime", "ok", sys.executable),
|
|
149
|
+
]
|
|
150
|
+
for command in ("litloop", "litloop-mcp"):
|
|
151
|
+
path = shutil.which(command)
|
|
152
|
+
checks.append((command, "ok" if path else "warning", path or "not found on PATH"))
|
|
153
|
+
# litloop + pymupdf: simple installed/missing check
|
|
154
|
+
for package in ("litloop", "pymupdf"):
|
|
155
|
+
version = _installed_package_version(package)
|
|
156
|
+
checks.append((f"package: {package}", "ok" if version else "warning", version or "not installed"))
|
|
157
|
+
# cloakbrowser: also flag stale versions (anti-detection patches age fast)
|
|
158
|
+
cb_status, cb_detail = _cloakbrowser_version_status()
|
|
159
|
+
checks.append(("package: cloakbrowser", cb_status if cb_status != "not_installed" else "warning", cb_detail))
|
|
160
|
+
try:
|
|
161
|
+
from ..browser.cloakbrowser_compat import configure_builtin_cloakbrowser
|
|
162
|
+
cache_dir = configure_builtin_cloakbrowser(create_dir=False)
|
|
163
|
+
status = "ok" if cache_dir.exists() else "warning"
|
|
164
|
+
detail = str(cache_dir) if cache_dir.exists() else f"not downloaded yet: {cache_dir}"
|
|
165
|
+
except Exception as exc:
|
|
166
|
+
status = "warning"
|
|
167
|
+
detail = f"cache check failed: {exc}"
|
|
168
|
+
checks.append(("CloakBrowser cache", status, detail))
|
|
169
|
+
return checks
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
@app.command("doctor")
|
|
173
|
+
def doctor():
|
|
174
|
+
"""Inspect LitLoop-PDF runtime, dependencies, and browser cache."""
|
|
175
|
+
table = Table(title="LitLoop-PDF Doctor")
|
|
176
|
+
table.add_column("Item", width=24)
|
|
177
|
+
table.add_column("Status", width=10)
|
|
178
|
+
table.add_column("Detail", overflow="fold")
|
|
179
|
+
styles = {"ok": "green", "warning": "yellow", "info": "cyan", "outdated": "yellow", "not_installed": "yellow"}
|
|
180
|
+
for label, status, detail in _doctor_checks():
|
|
181
|
+
style = styles.get(status, "white")
|
|
182
|
+
table.add_row(label, f"[{style}]{status}[/{style}]", detail)
|
|
183
|
+
console.print(table)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def _apply_school_config(cfg: dict, school: str):
|
|
187
|
+
entry = get_school(school)
|
|
188
|
+
cfg["instsci_school"] = entry.name
|
|
189
|
+
if entry.school_type == "ezproxy":
|
|
190
|
+
cfg["ezproxy_login_url"] = entry.host
|
|
191
|
+
cfg["instsci_base_url"] = ""
|
|
192
|
+
else:
|
|
193
|
+
cfg["instsci_base_url"] = entry.host
|
|
194
|
+
cfg["ezproxy_login_url"] = ""
|
|
195
|
+
return entry
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def _access_url(cfg: dict) -> str:
|
|
199
|
+
return cfg.get("ezproxy_login_url", "") or cfg.get("instsci_base_url", "")
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _configured_subscription_institution(cfg: dict) -> str:
|
|
203
|
+
"""Return the configured subscription institution search text, if any."""
|
|
204
|
+
return (cfg.get("carsi_idp_name", "") or cfg.get("instsci_school", "") or "").strip()
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def _resolve_subscription_institution(
|
|
208
|
+
cfg: dict,
|
|
209
|
+
institution: str,
|
|
210
|
+
*,
|
|
211
|
+
prompt: bool = True,
|
|
212
|
+
) -> str:
|
|
213
|
+
"""Resolve institution text without hard-coding any school as the default."""
|
|
214
|
+
explicit = institution.strip()
|
|
215
|
+
if explicit:
|
|
216
|
+
return explicit
|
|
217
|
+
|
|
218
|
+
configured = _configured_subscription_institution(cfg)
|
|
219
|
+
if configured:
|
|
220
|
+
return configured
|
|
221
|
+
|
|
222
|
+
if not prompt:
|
|
223
|
+
console.print(
|
|
224
|
+
"[red]Subscription institution is required.[/red] "
|
|
225
|
+
"Pass --institution or run: instsci setup --school \"Your Institution\""
|
|
226
|
+
)
|
|
227
|
+
raise typer.Exit(1)
|
|
228
|
+
|
|
229
|
+
console.print(
|
|
230
|
+
"[yellow]Subscription institution is required for closed-access publisher PDFs.[/yellow]"
|
|
231
|
+
)
|
|
232
|
+
console.print(
|
|
233
|
+
"[dim]Use the institution that owns your subscription, e.g. the name shown in "
|
|
234
|
+
"OpenAthens/Shibboleth/CARSI login pages.[/dim]"
|
|
235
|
+
)
|
|
236
|
+
value = typer.prompt("Subscription institution").strip()
|
|
237
|
+
if not value:
|
|
238
|
+
console.print("[red]Subscription institution cannot be empty.[/red]")
|
|
239
|
+
raise typer.Exit(1)
|
|
240
|
+
|
|
241
|
+
cfg["carsi_enabled"] = True
|
|
242
|
+
cfg["carsi_idp_name"] = value
|
|
243
|
+
save_config(cfg)
|
|
244
|
+
return value
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def _path_status(path_value: str) -> tuple[str, str]:
|
|
248
|
+
if not path_value:
|
|
249
|
+
return "missing", ""
|
|
250
|
+
path = Path(path_value)
|
|
251
|
+
return ("ok" if path.exists() else "missing", str(path))
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def _show_setup_check(cfg: dict) -> bool:
|
|
255
|
+
checks: list[tuple[str, str, str]] = []
|
|
256
|
+
checks.append(("School", "ok" if cfg.get("instsci_school", "") else "missing", cfg.get("instsci_school", "") or "set with --school"))
|
|
257
|
+
checks.append(("Access URL", "ok" if _access_url(cfg) else "missing", _access_url(cfg) or "derived from --school"))
|
|
258
|
+
federated_ready = (not cfg.get("carsi_enabled", False)) or bool(cfg.get("carsi_idp_name", ""))
|
|
259
|
+
checks.append((
|
|
260
|
+
"Federated login",
|
|
261
|
+
"ok" if federated_ready else "missing",
|
|
262
|
+
cfg.get("carsi_idp_name", "") or ("disabled" if not cfg.get("carsi_enabled", False) else "set with --federated-school"),
|
|
263
|
+
))
|
|
264
|
+
for label, path_value in [
|
|
265
|
+
("Output dir", cfg.get("output_dir", "")),
|
|
266
|
+
("Cache dir", cfg.get("cache_dir", "")),
|
|
267
|
+
("Chrome profile", cfg.get("chrome_profile_dir", "")),
|
|
268
|
+
("Session dir", cfg.get("carsi_cookie_dir", "")),
|
|
269
|
+
]:
|
|
270
|
+
status, detail = _path_status(path_value)
|
|
271
|
+
checks.append((label, status, detail))
|
|
272
|
+
|
|
273
|
+
table = Table(title="InstSci Environment Check")
|
|
274
|
+
table.add_column("Item", width=18)
|
|
275
|
+
table.add_column("Status", width=10)
|
|
276
|
+
table.add_column("Detail", overflow="fold")
|
|
277
|
+
ready = True
|
|
278
|
+
for label, status, detail in checks:
|
|
279
|
+
if status != "ok":
|
|
280
|
+
ready = False
|
|
281
|
+
style = "green" if status == "ok" else "yellow"
|
|
282
|
+
table.add_row(label, f"[{style}]{status}[/{style}]", detail)
|
|
283
|
+
console.print(table)
|
|
284
|
+
return ready
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
@app.command()
|
|
288
|
+
def setup(
|
|
289
|
+
school: str = typer.Option("", "--school", help="Set institution by school name or partial match."),
|
|
290
|
+
email: str = typer.Option("", "--email", help="Set email for Open Access metadata services."),
|
|
291
|
+
output_dir: str = typer.Option("", "--output-dir", help="Set the default PDF output directory."),
|
|
292
|
+
federated: bool = typer.Option(True, "--federated/--no-federated", help="Enable browser federated institutional login."),
|
|
293
|
+
federated_school: str = typer.Option("", "--federated-school", help="Override the school name shown in publisher login pages."),
|
|
294
|
+
check: bool = typer.Option(False, "--check", help="Check environment without changing configuration."),
|
|
295
|
+
):
|
|
296
|
+
"""One-step environment setup for institutional paper downloads."""
|
|
297
|
+
cfg = load_config()
|
|
298
|
+
changed = False
|
|
299
|
+
school_entry = None
|
|
300
|
+
|
|
301
|
+
has_setter = any([school, email, output_dir, federated_school]) or not federated
|
|
302
|
+
if check and not has_setter:
|
|
303
|
+
if not _show_setup_check(cfg):
|
|
304
|
+
raise typer.Exit(2)
|
|
305
|
+
return
|
|
306
|
+
|
|
307
|
+
if school:
|
|
308
|
+
try:
|
|
309
|
+
school_entry = _apply_school_config(cfg, school)
|
|
310
|
+
except ValueError as exc:
|
|
311
|
+
console.print(f"[red]{exc}[/red]")
|
|
312
|
+
raise typer.Exit(1) from exc
|
|
313
|
+
changed = True
|
|
314
|
+
|
|
315
|
+
if email:
|
|
316
|
+
cfg["email"] = email
|
|
317
|
+
changed = True
|
|
318
|
+
|
|
319
|
+
if output_dir:
|
|
320
|
+
cfg["output_dir"] = output_dir
|
|
321
|
+
changed = True
|
|
322
|
+
|
|
323
|
+
if federated and (school or federated_school or cfg.get("instsci_school", "")):
|
|
324
|
+
cfg["carsi_enabled"] = True
|
|
325
|
+
if federated_school:
|
|
326
|
+
cfg["carsi_idp_name"] = federated_school
|
|
327
|
+
elif school_entry is not None:
|
|
328
|
+
cfg["carsi_idp_name"] = school_entry.name
|
|
329
|
+
elif cfg.get("instsci_school", "") and not cfg.get("carsi_idp_name", ""):
|
|
330
|
+
cfg["carsi_idp_name"] = cfg.get("instsci_school", "")
|
|
331
|
+
changed = True
|
|
332
|
+
elif not federated:
|
|
333
|
+
cfg["carsi_enabled"] = False
|
|
334
|
+
changed = True
|
|
335
|
+
|
|
336
|
+
for d in [cfg.get("output_dir", ""), cfg.get("cache_dir", ""), cfg.get("carsi_cookie_dir", "")]:
|
|
337
|
+
if d:
|
|
338
|
+
Path(d).mkdir(parents=True, exist_ok=True)
|
|
339
|
+
if changed:
|
|
340
|
+
save_config(cfg)
|
|
341
|
+
|
|
342
|
+
ready = bool(cfg.get("instsci_school", "") and _access_url(cfg) and ((not cfg.get("carsi_enabled", False)) or cfg.get("carsi_idp_name", "")))
|
|
343
|
+
if ready:
|
|
344
|
+
console.print("[green]Environment ready.[/green]")
|
|
345
|
+
else:
|
|
346
|
+
console.print("[yellow]Environment prepared, but institution access is incomplete.[/yellow]")
|
|
347
|
+
if school_entry is not None:
|
|
348
|
+
type_label = _school_type_label(school_entry.school_type)
|
|
349
|
+
console.print(f" School: {school_entry.name} ({type_label})")
|
|
350
|
+
console.print(f" Access URL: {_access_url(cfg)}")
|
|
351
|
+
if school_entry.school_type in {"easyconnect", "atrust"}:
|
|
352
|
+
console.print("[yellow]This school needs a local campus connector before downloading.[/yellow]")
|
|
353
|
+
console.print(" Set it with: [cyan]instsci config-cmd --connector-url socks5://127.0.0.1:1080[/cyan]")
|
|
354
|
+
_output_dir = cfg.get("output_dir", "")
|
|
355
|
+
_browser_dir = cfg.get("chrome_profile_dir", "")
|
|
356
|
+
_sessions_dir = cfg.get("carsi_cookie_dir", "")
|
|
357
|
+
console.print(f" Output dir: {_output_dir}")
|
|
358
|
+
console.print(f" Browser dir: {_browser_dir}")
|
|
359
|
+
console.print(f" Sessions dir: {_sessions_dir}")
|
|
360
|
+
console.print("[dim]Next: instsci papers dois.txt --publisher auto[/dim]")
|
|
361
|
+
console.print("[dim]If SSO, 2FA, or CAPTCHA appears, complete it once in the opened browser window.[/dim]")
|
|
362
|
+
|
|
363
|
+
if (check or not ready) and not _show_setup_check(cfg):
|
|
364
|
+
raise typer.Exit(2)
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
@app.command()
|
|
368
|
+
def login(
|
|
369
|
+
force: bool = typer.Option(False, "--force", "-f", help="Force re-login even if session is valid."),
|
|
370
|
+
verbose: bool = typer.Option(False, "--verbose", "-v", help="Verbose logging."),
|
|
371
|
+
):
|
|
372
|
+
"""Initialize or refresh institutional access session."""
|
|
373
|
+
_setup_logging(verbose)
|
|
374
|
+
config = load_config()
|
|
375
|
+
fetcher = PaperFetcher(config)
|
|
376
|
+
|
|
377
|
+
console.print("[bold]Checking institutional access session...[/bold]")
|
|
378
|
+
if fetcher.auth.login(force=force):
|
|
379
|
+
console.print("[green]Institutional access session is active.[/green]")
|
|
380
|
+
else:
|
|
381
|
+
console.print("[red]Failed to authenticate institutional access.[/red]")
|
|
382
|
+
raise typer.Exit(1)
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
@app.command()
|
|
386
|
+
def fetch(
|
|
387
|
+
identifier: str = typer.Argument(help="DOI or URL of the paper to fetch."),
|
|
388
|
+
output: str = typer.Option("", "--output", "-o", help="Output directory for PDFs."),
|
|
389
|
+
format: str = typer.Option("json", "--format", "-f", help="Output format: json, markdown, text."),
|
|
390
|
+
text_only: bool = typer.Option(False, "--text-only", "-t", help="Output only plain text (minimal tokens)."),
|
|
391
|
+
no_cache: bool = typer.Option(False, "--no-cache", help="Bypass cache."),
|
|
392
|
+
verbose: bool = typer.Option(False, "--verbose", "-v", help="Verbose logging."),
|
|
393
|
+
):
|
|
394
|
+
"""Fetch a single paper by DOI or URL."""
|
|
395
|
+
_setup_logging(verbose)
|
|
396
|
+
config = load_config()
|
|
397
|
+
_ensure_email(config)
|
|
398
|
+
if output:
|
|
399
|
+
config["output_dir"] = output
|
|
400
|
+
|
|
401
|
+
fetcher = PaperFetcher(config)
|
|
402
|
+
try:
|
|
403
|
+
console.print(f"[bold]Fetching:[/bold] {identifier}")
|
|
404
|
+
result = fetcher.fetch_with_result(identifier, use_cache=not no_cache)
|
|
405
|
+
paper = result.paper
|
|
406
|
+
|
|
407
|
+
if result.status != "success":
|
|
408
|
+
console.print(f"[yellow]Status: {result.status} ({result.reason or result.quality})[/yellow]")
|
|
409
|
+
if result.next_action:
|
|
410
|
+
console.print(f"[yellow]Next: {result.next_action.message}[/yellow]")
|
|
411
|
+
if result.next_action.command:
|
|
412
|
+
console.print(f"[dim]{result.next_action.command}[/dim]")
|
|
413
|
+
|
|
414
|
+
if text_only:
|
|
415
|
+
console.print(result.to_text())
|
|
416
|
+
elif format == "markdown":
|
|
417
|
+
console.print(result.to_markdown())
|
|
418
|
+
elif format == "text":
|
|
419
|
+
console.print(result.to_text())
|
|
420
|
+
else:
|
|
421
|
+
console.print(result.to_json())
|
|
422
|
+
|
|
423
|
+
if paper.pdf_path:
|
|
424
|
+
console.print(f"\n[dim]PDF saved to: {paper.pdf_path}[/dim]")
|
|
425
|
+
console.print(f"[dim]Source: {paper.source}[/dim]")
|
|
426
|
+
|
|
427
|
+
finally:
|
|
428
|
+
fetcher.close()
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
@app.command()
|
|
432
|
+
def batch(
|
|
433
|
+
file: Path = typer.Argument(help="File containing DOIs (one per line)."),
|
|
434
|
+
output: str = typer.Option("", "--output", "-o", help="Output directory."),
|
|
435
|
+
format: str = typer.Option("json", "--format", "-f", help="Output format: json, markdown, text."),
|
|
436
|
+
verbose: bool = typer.Option(False, "--verbose", "-v", help="Verbose logging."),
|
|
437
|
+
):
|
|
438
|
+
"""Fetch multiple papers from a file of DOIs."""
|
|
439
|
+
_setup_logging(verbose)
|
|
440
|
+
|
|
441
|
+
if not file.exists():
|
|
442
|
+
console.print(f"[red]File not found: {file}[/red]")
|
|
443
|
+
raise typer.Exit(1)
|
|
444
|
+
|
|
445
|
+
dois = _read_dois(file)
|
|
446
|
+
|
|
447
|
+
if not dois:
|
|
448
|
+
console.print("[yellow]No DOIs found in file.[/yellow]")
|
|
449
|
+
raise typer.Exit(0)
|
|
450
|
+
|
|
451
|
+
console.print(f"[bold]Found {len(dois)} DOIs to fetch.[/bold]")
|
|
452
|
+
|
|
453
|
+
config = load_config()
|
|
454
|
+
if output:
|
|
455
|
+
config["output_dir"] = output
|
|
456
|
+
|
|
457
|
+
fetcher = PaperFetcher(config)
|
|
458
|
+
results_dir = Path(config.get("output_dir", ""))
|
|
459
|
+
results_dir.mkdir(parents=True, exist_ok=True)
|
|
460
|
+
|
|
461
|
+
succeeded = 0
|
|
462
|
+
failed = 0
|
|
463
|
+
|
|
464
|
+
try:
|
|
465
|
+
for i, doi in enumerate(dois, 1):
|
|
466
|
+
console.print(f"\n[bold][{i}/{len(dois)}][/bold] Fetching: {doi}")
|
|
467
|
+
try:
|
|
468
|
+
paper = fetcher.fetch(doi)
|
|
469
|
+
if paper.full_text:
|
|
470
|
+
succeeded += 1
|
|
471
|
+
# Save result
|
|
472
|
+
safe_name = doi.replace("/", "_").replace(":", "_")
|
|
473
|
+
if format == "markdown":
|
|
474
|
+
out_file = results_dir / f"{safe_name}.md"
|
|
475
|
+
out_file.write_text(paper.to_markdown(), encoding="utf-8")
|
|
476
|
+
elif format == "text":
|
|
477
|
+
out_file = results_dir / f"{safe_name}.txt"
|
|
478
|
+
out_file.write_text(paper.to_text(), encoding="utf-8")
|
|
479
|
+
else:
|
|
480
|
+
out_file = results_dir / f"{safe_name}.json"
|
|
481
|
+
out_file.write_text(paper.to_json(), encoding="utf-8")
|
|
482
|
+
console.print(f" [green]OK[/green] → {out_file.name}")
|
|
483
|
+
else:
|
|
484
|
+
failed += 1
|
|
485
|
+
console.print(" [yellow]No full text extracted[/yellow]")
|
|
486
|
+
except Exception as e:
|
|
487
|
+
failed += 1
|
|
488
|
+
console.print(f" [red]Error: {e}[/red]")
|
|
489
|
+
|
|
490
|
+
console.print(f"\n[bold]Done:[/bold] {succeeded} succeeded, {failed} failed out of {len(dois)}.")
|
|
491
|
+
|
|
492
|
+
finally:
|
|
493
|
+
fetcher.close()
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
@app.command("session-doctor")
|
|
497
|
+
def session_doctor(
|
|
498
|
+
publisher: str = typer.Option("", "--publisher", "-p", help="Publisher profile key to include publisher domains."),
|
|
499
|
+
browser_profile: str = typer.Option("", "--browser-profile", help="Inspect one browser profile instead of known candidates."),
|
|
500
|
+
output: str = typer.Option("", "--output", "-o", help="Optional JSON report path."),
|
|
501
|
+
):
|
|
502
|
+
"""Inspect local browser profiles for institution/publisher session presence."""
|
|
503
|
+
from ..browser.profile_health import DEFAULT_SESSION_DOMAINS, candidate_profile_dirs, inspect_browser_profile
|
|
504
|
+
from ..publishers.profiles import get_publisher_profile
|
|
505
|
+
|
|
506
|
+
cfg = load_config()
|
|
507
|
+
profile = None
|
|
508
|
+
domains = list(DEFAULT_SESSION_DOMAINS)
|
|
509
|
+
if publisher:
|
|
510
|
+
try:
|
|
511
|
+
profile = get_publisher_profile(publisher)
|
|
512
|
+
except ValueError as exc:
|
|
513
|
+
console.print(f"[red]{exc}[/red]")
|
|
514
|
+
raise typer.Exit(1) from exc
|
|
515
|
+
domains.extend(profile.base_domains)
|
|
516
|
+
domains = list(dict.fromkeys(domain for domain in domains if domain))
|
|
517
|
+
|
|
518
|
+
profiles = [Path(browser_profile)] if browser_profile else candidate_profile_dirs(cfg, workspace=Path.cwd())
|
|
519
|
+
reports = [inspect_browser_profile(path, domains) for path in profiles]
|
|
520
|
+
|
|
521
|
+
table = Table(title="InstSci Browser Session Doctor")
|
|
522
|
+
table.add_column("Profile", overflow="fold")
|
|
523
|
+
table.add_column("Exists", width=8)
|
|
524
|
+
table.add_column("Session Hosts", overflow="fold")
|
|
525
|
+
table.add_column("Latest Expiry", overflow="fold")
|
|
526
|
+
table.add_column("Notes", overflow="fold")
|
|
527
|
+
for report in reports:
|
|
528
|
+
present = []
|
|
529
|
+
expiries = []
|
|
530
|
+
seen_hosts: set[str] = set()
|
|
531
|
+
for domain, info in report["domains"].items():
|
|
532
|
+
latest = str(info.get("latest_expires_at") or "")
|
|
533
|
+
if latest:
|
|
534
|
+
expiries.append(f"{domain}: {latest}")
|
|
535
|
+
for host in info.get("hosts", []):
|
|
536
|
+
host_name = str(host.get("host") or "")
|
|
537
|
+
if host_name in seen_hosts:
|
|
538
|
+
continue
|
|
539
|
+
seen_hosts.add(host_name)
|
|
540
|
+
count = int(host.get("cookie_count") or 0)
|
|
541
|
+
if count:
|
|
542
|
+
session_count = int(host.get("session_cookie_count") or 0)
|
|
543
|
+
suffix = f", session={session_count}" if session_count else ""
|
|
544
|
+
present.append(f"{host_name}({count}{suffix})")
|
|
545
|
+
notes = report.get("error") or ("cookie DB missing" if report["exists"] and not report["cookies_db_exists"] else "")
|
|
546
|
+
table.add_row(
|
|
547
|
+
report["profile_dir"],
|
|
548
|
+
"yes" if report["exists"] else "no",
|
|
549
|
+
", ".join(present) or "-",
|
|
550
|
+
", ".join(expiries) or "-",
|
|
551
|
+
notes,
|
|
552
|
+
)
|
|
553
|
+
console.print(table)
|
|
554
|
+
|
|
555
|
+
if output:
|
|
556
|
+
output_path = Path(output)
|
|
557
|
+
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
558
|
+
payload = {
|
|
559
|
+
"publisher": profile.name if profile else "",
|
|
560
|
+
"domains": domains,
|
|
561
|
+
"reports": reports,
|
|
562
|
+
}
|
|
563
|
+
output_path.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
564
|
+
console.print(f"[dim]Report: {output_path}[/dim]")
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
@app.command("publisher-doctor")
|
|
568
|
+
def publisher_doctor(
|
|
569
|
+
publisher: str = typer.Option("all", "--publisher", "-p", help="Publisher profile key, or 'all'."),
|
|
570
|
+
output: str = typer.Option("", "--output", "-o", help="Optional JSON report path."),
|
|
571
|
+
probe_pdf: bool = typer.Option(True, "--probe-pdf/--no-probe-pdf", help="Probe PDF candidate URLs without saving files."),
|
|
572
|
+
max_candidates: int = typer.Option(4, "--max-candidates", min=0, max=10, help="Maximum PDF candidates to probe per publisher."),
|
|
573
|
+
timeout: int = typer.Option(20, "--timeout", min=3, max=120, help="Network timeout in seconds."),
|
|
574
|
+
):
|
|
575
|
+
"""HTTP preflight to verify reusable publisher PDF routes.
|
|
576
|
+
|
|
577
|
+
Browser-backed InstSci workflows are authoritative for publisher PDF
|
|
578
|
+
capability verdicts; this command only checks route templates and blockers.
|
|
579
|
+
"""
|
|
580
|
+
from ..publishers.access import verify_publishers
|
|
581
|
+
from ..publishers.profiles import list_publisher_profiles
|
|
582
|
+
|
|
583
|
+
keys = list_publisher_profiles() if publisher.strip().lower() == "all" else [publisher.strip()]
|
|
584
|
+
console.print(f"[bold]Verifying publisher access assets:[/bold] {', '.join(keys)}")
|
|
585
|
+
console.print(
|
|
586
|
+
"[yellow]HTTP preflight only:[/yellow] use the built-in browser workflow "
|
|
587
|
+
"for final publisher PDF capability verdicts."
|
|
588
|
+
)
|
|
589
|
+
results = verify_publishers(
|
|
590
|
+
keys,
|
|
591
|
+
probe_pdf=probe_pdf,
|
|
592
|
+
max_candidates=max_candidates,
|
|
593
|
+
timeout=timeout,
|
|
594
|
+
)
|
|
595
|
+
|
|
596
|
+
table = Table(title="Publisher Access Verification")
|
|
597
|
+
table.add_column("Publisher", width=18)
|
|
598
|
+
table.add_column("Landing", width=8)
|
|
599
|
+
table.add_column("PDF Links", width=9, justify="right")
|
|
600
|
+
table.add_column("Observed", width=22)
|
|
601
|
+
table.add_column("Final Host", overflow="fold")
|
|
602
|
+
needs_attention = False
|
|
603
|
+
for result in results:
|
|
604
|
+
if result["landing_status"] == 404 or not result["pdf_candidates"]:
|
|
605
|
+
needs_attention = True
|
|
606
|
+
table.add_row(
|
|
607
|
+
result["profile_key"],
|
|
608
|
+
str(result["landing_status"]),
|
|
609
|
+
str(len(result["pdf_candidates"])),
|
|
610
|
+
result["observed_access"],
|
|
611
|
+
urlparse(result["landing_url"]).hostname or result["landing_url"],
|
|
612
|
+
)
|
|
613
|
+
console.print(table)
|
|
614
|
+
|
|
615
|
+
if output:
|
|
616
|
+
output_path = Path(output)
|
|
617
|
+
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
618
|
+
output_path.write_text(json.dumps(results, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
619
|
+
console.print(f"[dim]Report: {output_path}[/dim]")
|
|
620
|
+
|
|
621
|
+
if needs_attention:
|
|
622
|
+
raise typer.Exit(2)
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
@app.command("identity-policy")
|
|
626
|
+
def identity_policy(
|
|
627
|
+
output: str = typer.Option("", "--output", "-o", help="Optional JSON report path."),
|
|
628
|
+
):
|
|
629
|
+
"""Show the institutional identity routing policy for publisher PDFs."""
|
|
630
|
+
from ..publishers.access import load_institutional_identity_policy
|
|
631
|
+
|
|
632
|
+
policy = load_institutional_identity_policy()
|
|
633
|
+
console.print("[bold]InstSci Institutional Identity Policy[/bold]")
|
|
634
|
+
console.print(f"Default mode: [cyan]{policy['default_mode']}[/cyan]")
|
|
635
|
+
console.print(f"Default identity: [cyan]{policy['default_identity']}[/cyan]")
|
|
636
|
+
required = "required" if policy["subscription_institution"]["required_for_closed_access"] else "optional"
|
|
637
|
+
console.print(f"Subscription institution: [cyan]{required}[/cyan]")
|
|
638
|
+
console.print(f"Preferred off-campus access: [cyan]{policy['preferred_off_campus_access']}[/cyan]")
|
|
639
|
+
console.print(f"Final PDF verdict requires: [cyan]{policy['final_pdf_verdict_requires']}[/cyan]")
|
|
640
|
+
|
|
641
|
+
table = Table(title="Identity Route Order")
|
|
642
|
+
table.add_column("Order", width=5, justify="right")
|
|
643
|
+
table.add_column("Identity", width=22)
|
|
644
|
+
table.add_column("Role", overflow="fold")
|
|
645
|
+
table.add_column("Global default", width=14)
|
|
646
|
+
for index, identity_key in enumerate(policy["identity_order"], 1):
|
|
647
|
+
section_key = "webvpn" if identity_key == "webvpn_broker" else identity_key
|
|
648
|
+
identity = policy["identities"].get(section_key, {})
|
|
649
|
+
table.add_row(
|
|
650
|
+
str(index),
|
|
651
|
+
identity_key,
|
|
652
|
+
str(identity.get("recommended_role", "")).replace("_", " "),
|
|
653
|
+
"yes" if identity.get("global_default") else "no",
|
|
654
|
+
)
|
|
655
|
+
console.print(table)
|
|
656
|
+
|
|
657
|
+
webvpn = policy["identities"]["webvpn"]
|
|
658
|
+
console.print(
|
|
659
|
+
"[yellow]WebVPN is optional:[/yellow] "
|
|
660
|
+
f"{webvpn['persistence_limits']['cookie_store']['notes']}"
|
|
661
|
+
)
|
|
662
|
+
console.print(
|
|
663
|
+
"[yellow]Use visible CloakBrowser:[/yellow] "
|
|
664
|
+
"keep the same live context for SSO, CAPTCHA, Cloudflare, and PDF-token flows."
|
|
665
|
+
)
|
|
666
|
+
|
|
667
|
+
if output:
|
|
668
|
+
output_path = Path(output)
|
|
669
|
+
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
670
|
+
output_path.write_text(json.dumps(policy, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
671
|
+
console.print(f"[dim]Report: {output_path}[/dim]")
|
|
672
|
+
|
|
673
|
+
|
|
674
|
+
@app.command()
|
|
675
|
+
def search(
|
|
676
|
+
query: str = typer.Argument(help="Search query."),
|
|
677
|
+
limit: int = typer.Option(10, "--limit", "-n", help="Maximum results."),
|
|
678
|
+
year: str = typer.Option("", "--year", "-y", help="Year range, e.g., '2020-2024' or '2020-'."),
|
|
679
|
+
do_fetch: bool = typer.Option(False, "--fetch", help="Also fetch full text for results with DOIs."),
|
|
680
|
+
verbose: bool = typer.Option(False, "--verbose", "-v", help="Verbose logging."),
|
|
681
|
+
):
|
|
682
|
+
"""Search for papers via Semantic Scholar."""
|
|
683
|
+
_setup_logging(verbose)
|
|
684
|
+
|
|
685
|
+
console.print(f"[bold]Searching:[/bold] {query}")
|
|
686
|
+
results = semantic_scholar.search(query, limit=limit, year_range=year or None)
|
|
687
|
+
|
|
688
|
+
if not results:
|
|
689
|
+
console.print("[yellow]No results found.[/yellow]")
|
|
690
|
+
raise typer.Exit(0)
|
|
691
|
+
|
|
692
|
+
# Display results in a table
|
|
693
|
+
table = Table(title=f"Search Results ({len(results)})")
|
|
694
|
+
table.add_column("#", style="dim", width=3)
|
|
695
|
+
table.add_column("Year", width=5)
|
|
696
|
+
table.add_column("Title", max_width=60)
|
|
697
|
+
table.add_column("Authors", max_width=30)
|
|
698
|
+
table.add_column("DOI", max_width=25)
|
|
699
|
+
table.add_column("Cites", width=5, justify="right")
|
|
700
|
+
|
|
701
|
+
for i, r in enumerate(results, 1):
|
|
702
|
+
authors_str = ", ".join(r.authors[:3])
|
|
703
|
+
if len(r.authors) > 3:
|
|
704
|
+
authors_str += " et al."
|
|
705
|
+
table.add_row(
|
|
706
|
+
str(i),
|
|
707
|
+
str(r.year or ""),
|
|
708
|
+
r.title[:60],
|
|
709
|
+
authors_str[:30],
|
|
710
|
+
r.doi[:25] if r.doi else r.arxiv_id[:25] if r.arxiv_id else "",
|
|
711
|
+
str(r.citation_count),
|
|
712
|
+
)
|
|
713
|
+
|
|
714
|
+
console.print(table)
|
|
715
|
+
|
|
716
|
+
# Optionally fetch full texts
|
|
717
|
+
if do_fetch:
|
|
718
|
+
fetchable = [r for r in results if r.doi or r.arxiv_id]
|
|
719
|
+
if fetchable:
|
|
720
|
+
console.print(f"\n[bold]Fetching {len(fetchable)} papers...[/bold]")
|
|
721
|
+
config = load_config()
|
|
722
|
+
fetcher = PaperFetcher(config)
|
|
723
|
+
try:
|
|
724
|
+
for r in fetchable:
|
|
725
|
+
identifier = r.doi or f"arxiv:{r.arxiv_id}"
|
|
726
|
+
console.print(f" Fetching: {identifier}")
|
|
727
|
+
try:
|
|
728
|
+
paper = fetcher.fetch(identifier)
|
|
729
|
+
status = "[green]OK[/green]" if paper.full_text else "[yellow]No text[/yellow]"
|
|
730
|
+
console.print(f" {status}")
|
|
731
|
+
except Exception as e:
|
|
732
|
+
console.print(f" [red]Error: {e}[/red]")
|
|
733
|
+
finally:
|
|
734
|
+
fetcher.close()
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
@app.command()
|
|
738
|
+
def cache(
|
|
739
|
+
action: str = typer.Argument(help="Action: 'clear' to remove cached results."),
|
|
740
|
+
):
|
|
741
|
+
"""Manage the paper cache."""
|
|
742
|
+
if action == "clear":
|
|
743
|
+
config = load_config()
|
|
744
|
+
fetcher = PaperFetcher(config)
|
|
745
|
+
fetcher.clear_cache()
|
|
746
|
+
console.print("[green]Cache cleared.[/green]")
|
|
747
|
+
else:
|
|
748
|
+
console.print(f"[red]Unknown action: {action}. Use 'clear'.[/red]")
|
|
749
|
+
raise typer.Exit(1)
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
@app.command()
|
|
753
|
+
def schools(
|
|
754
|
+
query: str = typer.Argument("", help="Search query (name, province, or host). Omit to list all."),
|
|
755
|
+
):
|
|
756
|
+
"""List or search supported universities."""
|
|
757
|
+
if query:
|
|
758
|
+
results = search_schools(query)
|
|
759
|
+
else:
|
|
760
|
+
results = list_schools()
|
|
761
|
+
|
|
762
|
+
if not results:
|
|
763
|
+
console.print(f"[yellow]No schools found matching '{query}'.[/yellow]")
|
|
764
|
+
raise typer.Exit(0)
|
|
765
|
+
|
|
766
|
+
table = Table(title=f"Supported Schools ({len(results)})")
|
|
767
|
+
table.add_column("#", style="dim", width=4)
|
|
768
|
+
table.add_column("Province", width=10)
|
|
769
|
+
table.add_column("School", max_width=25)
|
|
770
|
+
table.add_column("Type", width=12)
|
|
771
|
+
table.add_column("Host", max_width=40)
|
|
772
|
+
table.add_column("Custom Key", width=5, justify="center")
|
|
773
|
+
|
|
774
|
+
from ..institutional.schools import WEBVPN_DEFAULT_KEY
|
|
775
|
+
for i, s in enumerate(results, 1):
|
|
776
|
+
has_custom = "Y" if s.key != WEBVPN_DEFAULT_KEY else ""
|
|
777
|
+
table.add_row(str(i), s.province, s.name, _school_type_label(s.school_type), s.host, has_custom)
|
|
778
|
+
|
|
779
|
+
console.print(table)
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
@app.command()
|
|
783
|
+
def config_cmd(
|
|
784
|
+
show: bool = typer.Option(True, "--show", help="Show current config."),
|
|
785
|
+
set_email: str = typer.Option("", "--email", help="Set email for Unpaywall API."),
|
|
786
|
+
set_output: str = typer.Option("", "--output-dir", help="Set default output directory."),
|
|
787
|
+
set_access_url: str = typer.Option("", "--access-url", help="Set institutional access gateway URL."),
|
|
788
|
+
set_webvpn_url: str = typer.Option("", "--webvpn-url", help="Legacy gateway URL option.", hidden=True),
|
|
789
|
+
set_school: str = typer.Option("", "--school", help="Set school (use 'instsci schools' to list)."),
|
|
790
|
+
set_connector_url: str = typer.Option("", "--connector-url", help="Set local SOCKS5 connector URL for EasyConnect."),
|
|
791
|
+
set_proxy_url: str = typer.Option("", "--proxy-url", help="Legacy local connector URL option.", hidden=True),
|
|
792
|
+
set_elsevier_key: str = typer.Option("", "--elsevier-api-key", help="Set Elsevier API key."),
|
|
793
|
+
set_elsevier_token: str = typer.Option("", "--elsevier-inst-token", help="Set Elsevier institutional token."),
|
|
794
|
+
set_static_proxy: str = typer.Option("", "--static-proxy", help="Set browser static proxy (e.g. socks5://1.2.3.4:1080)."),
|
|
795
|
+
set_proxy_pool: str = typer.Option("", "--proxy-pool", help="Comma-separated proxy list for IP rotation (e.g. socks5://a:1080,http://b:8080)."),
|
|
796
|
+
set_remote_port: int = typer.Option(-1, "--remote-assist-port", help="Set remote assist HTTP port (0=disabled)."),
|
|
797
|
+
set_max_workers: int = typer.Option(-1, "--max-browser-workers", help="Set max parallel browser workers."),
|
|
798
|
+
set_federated_enable: bool = typer.Option(False, "--federated-enable", help="Enable federated institutional auth."),
|
|
799
|
+
set_federated_disable: bool = typer.Option(False, "--federated-disable", help="Disable federated institutional auth."),
|
|
800
|
+
set_federated_school: str = typer.Option("", "--federated-school", help="Set school name for federated login."),
|
|
801
|
+
set_carsi_enable: bool = typer.Option(False, "--carsi-enable", help="Legacy federated auth option.", hidden=True),
|
|
802
|
+
set_carsi_disable: bool = typer.Option(False, "--carsi-disable", help="Legacy federated auth option.", hidden=True),
|
|
803
|
+
set_carsi_school: str = typer.Option("", "--carsi-school", help="Legacy federated school option.", hidden=True),
|
|
804
|
+
):
|
|
805
|
+
"""View or update configuration."""
|
|
806
|
+
cfg = load_config()
|
|
807
|
+
changed = False
|
|
808
|
+
|
|
809
|
+
if set_email:
|
|
810
|
+
cfg["email"] = set_email
|
|
811
|
+
changed = True
|
|
812
|
+
console.print(f"[green]Email set to: {set_email}[/green]")
|
|
813
|
+
|
|
814
|
+
if set_output:
|
|
815
|
+
cfg["output_dir"] = set_output
|
|
816
|
+
changed = True
|
|
817
|
+
console.print(f"[green]Output dir set to: {set_output}[/green]")
|
|
818
|
+
|
|
819
|
+
access_url = set_access_url or set_webvpn_url
|
|
820
|
+
if access_url:
|
|
821
|
+
cfg["instsci_base_url"] = access_url.rstrip("/")
|
|
822
|
+
changed = True
|
|
823
|
+
console.print(f"[green]Institutional access URL set to: {access_url}[/green]")
|
|
824
|
+
|
|
825
|
+
if set_school:
|
|
826
|
+
try:
|
|
827
|
+
entry = _apply_school_config(cfg, set_school)
|
|
828
|
+
changed = True
|
|
829
|
+
type_label = _school_type_label(entry.school_type)
|
|
830
|
+
console.print(f"[green]School set to: {entry.name} ({type_label}, {entry.host})[/green]")
|
|
831
|
+
if entry.school_type == "easyconnect":
|
|
832
|
+
console.print("[yellow]This school uses a local campus connector. Please:[/yellow]")
|
|
833
|
+
console.print(" 1. Connect via zju-connect: [cyan]zju-connect -server {0}[/cyan]".format(entry.host))
|
|
834
|
+
console.print(" 2. Set connector: [cyan]instsci config-cmd --connector-url socks5://127.0.0.1:1080[/cyan]")
|
|
835
|
+
except ValueError as e:
|
|
836
|
+
console.print(f"[red]{e}[/red]")
|
|
837
|
+
raise typer.Exit(1)
|
|
838
|
+
|
|
839
|
+
connector_url = set_connector_url or set_proxy_url
|
|
840
|
+
if connector_url:
|
|
841
|
+
cfg["network_proxy"] = connector_url
|
|
842
|
+
changed = True
|
|
843
|
+
console.print(f"[green]Connector URL set to: {connector_url}[/green]")
|
|
844
|
+
|
|
845
|
+
if set_elsevier_key:
|
|
846
|
+
cfg["elsevier_api_key"] = set_elsevier_key
|
|
847
|
+
changed = True
|
|
848
|
+
console.print("[green]Elsevier API key saved.[/green]")
|
|
849
|
+
|
|
850
|
+
if set_elsevier_token:
|
|
851
|
+
cfg["elsevier_insttoken"] = set_elsevier_token
|
|
852
|
+
changed = True
|
|
853
|
+
console.print("[green]Elsevier institutional token saved.[/green]")
|
|
854
|
+
|
|
855
|
+
federated_enable = set_federated_enable or set_carsi_enable
|
|
856
|
+
federated_disable = set_federated_disable or set_carsi_disable
|
|
857
|
+
federated_school = set_federated_school or set_carsi_school
|
|
858
|
+
|
|
859
|
+
if federated_enable:
|
|
860
|
+
cfg["carsi_enabled"] = True
|
|
861
|
+
changed = True
|
|
862
|
+
console.print("[green]Federated institutional auth enabled.[/green]")
|
|
863
|
+
|
|
864
|
+
if federated_disable:
|
|
865
|
+
cfg["carsi_enabled"] = False
|
|
866
|
+
changed = True
|
|
867
|
+
console.print("[yellow]Federated institutional auth disabled.[/yellow]")
|
|
868
|
+
|
|
869
|
+
if federated_school:
|
|
870
|
+
cfg["carsi_idp_name"] = federated_school
|
|
871
|
+
changed = True
|
|
872
|
+
console.print(f"[green]Federated login school set to: {federated_school}[/green]")
|
|
873
|
+
|
|
874
|
+
if set_static_proxy:
|
|
875
|
+
cfg["browser_static_proxy"] = set_static_proxy
|
|
876
|
+
changed = True
|
|
877
|
+
console.print(f"[green]Browser static proxy set to: {set_static_proxy}[/green]")
|
|
878
|
+
|
|
879
|
+
if set_proxy_pool:
|
|
880
|
+
cfg["proxy_pool"] = set_proxy_pool
|
|
881
|
+
changed = True
|
|
882
|
+
from litloop.config import parse_proxy_pool
|
|
883
|
+
parsed = parse_proxy_pool(set_proxy_pool)
|
|
884
|
+
console.print(f"[green]Proxy rotation pool: {len(parsed)} proxies ({', '.join(parsed)})[/green]")
|
|
885
|
+
|
|
886
|
+
if set_remote_port >= 0:
|
|
887
|
+
cfg["remote_assist_port"] = set_remote_port
|
|
888
|
+
changed = True
|
|
889
|
+
status = f"port {set_remote_port}" if set_remote_port > 0 else "disabled"
|
|
890
|
+
console.print(f"[green]Remote assist: {status}[/green]")
|
|
891
|
+
|
|
892
|
+
if set_max_workers >= 1:
|
|
893
|
+
cfg["max_browser_workers"] = set_max_workers
|
|
894
|
+
changed = True
|
|
895
|
+
console.print(f"[green]Max browser workers set to: {set_max_workers}[/green]")
|
|
896
|
+
|
|
897
|
+
if changed:
|
|
898
|
+
save_config(cfg)
|
|
899
|
+
|
|
900
|
+
has_setter = any([set_email, set_output, set_access_url, set_webvpn_url, set_school,
|
|
901
|
+
set_connector_url, set_proxy_url,
|
|
902
|
+
set_elsevier_key, set_elsevier_token,
|
|
903
|
+
set_federated_enable, set_federated_disable, set_federated_school,
|
|
904
|
+
set_carsi_enable, set_carsi_disable, set_carsi_school,
|
|
905
|
+
set_static_proxy, set_proxy_pool,
|
|
906
|
+
set_remote_port >= 0, set_max_workers >= 1])
|
|
907
|
+
if show and not has_setter:
|
|
908
|
+
# Determine school type
|
|
909
|
+
try:
|
|
910
|
+
from ..institutional.schools import get_school as _get_school
|
|
911
|
+
school_entry = _get_school(cfg.get("instsci_school", ""))
|
|
912
|
+
school_type = school_entry.school_type
|
|
913
|
+
except ValueError:
|
|
914
|
+
school_type = "unknown"
|
|
915
|
+
|
|
916
|
+
_school = cfg.get("instsci_school", "")
|
|
917
|
+
_conn = cfg.get("network_proxy", "") or "(not set)"
|
|
918
|
+
_email = cfg.get("email", "")
|
|
919
|
+
_eak = cfg.get("elsevier_api_key", "")
|
|
920
|
+
_eit = cfg.get("elsevier_insttoken", "")
|
|
921
|
+
_fe = cfg.get("carsi_enabled", False)
|
|
922
|
+
_fs = cfg.get("carsi_idp_name", "") or "(not set)"
|
|
923
|
+
_od = cfg.get("output_dir", "")
|
|
924
|
+
_cd = cfg.get("cache_dir", "")
|
|
925
|
+
_cp = cfg.get("cookie_path", "")
|
|
926
|
+
console.print("[bold]Current configuration:[/bold]")
|
|
927
|
+
console.print(f" School: {_school} ({school_type})")
|
|
928
|
+
console.print(f" Access URL: {_access_url(cfg)}")
|
|
929
|
+
console.print(f" Connector URL: {_conn}")
|
|
930
|
+
console.print(f" Email: {_email}")
|
|
931
|
+
console.print(f" Elsevier API key: {'****' if _eak else '(not set)'}")
|
|
932
|
+
console.print(f" Elsevier inst tok: {'****' if _eit else '(not set)'}")
|
|
933
|
+
console.print(f" Federated login: {'Yes' if _fe else 'No'}")
|
|
934
|
+
console.print(f" Federated school: {_fs}")
|
|
935
|
+
console.print(f" Output dir: {_od}")
|
|
936
|
+
console.print(f" Cache dir: {_cd}")
|
|
937
|
+
console.print(f" Cookie path: {_cp}")
|
|
938
|
+
_sp = cfg.get("browser_static_proxy", "")
|
|
939
|
+
_rp = cfg.get("remote_assist_port", 0)
|
|
940
|
+
_mw = cfg.get("max_browser_workers", 2)
|
|
941
|
+
console.print(f" Static proxy: {_sp or '(not set)'}")
|
|
942
|
+
console.print(f" Remote assist: {f'port {_rp}' if _rp else 'disabled'}")
|
|
943
|
+
console.print(f" Browser workers: {_mw}")
|
|
944
|
+
|
|
945
|
+
|
|
946
|
+
def _run_federated_login(
|
|
947
|
+
publisher: str,
|
|
948
|
+
url: str,
|
|
949
|
+
force: bool,
|
|
950
|
+
verbose: bool,
|
|
951
|
+
) -> None:
|
|
952
|
+
"""Run the federated institutional login flow."""
|
|
953
|
+
_setup_logging(verbose)
|
|
954
|
+
config = load_config()
|
|
955
|
+
|
|
956
|
+
if not config.get("carsi_enabled", ""):
|
|
957
|
+
console.print("[red]Federated login is not enabled. Run: instsci config-cmd --federated-enable --federated-school \"你的学校名\"[/red]")
|
|
958
|
+
raise typer.Exit(1)
|
|
959
|
+
|
|
960
|
+
if not config.get("carsi_idp_name", ""):
|
|
961
|
+
console.print("[red]Federated login school not set. Run: instsci config-cmd --federated-school \"你的学校名\"[/red]")
|
|
962
|
+
raise typer.Exit(1)
|
|
963
|
+
|
|
964
|
+
if not publisher and url:
|
|
965
|
+
from ..institutional.carsi_login import detect_publisher
|
|
966
|
+
publisher = detect_publisher(url) or ""
|
|
967
|
+
|
|
968
|
+
if not publisher:
|
|
969
|
+
console.print("[yellow]Available publishers:[/yellow]")
|
|
970
|
+
console.print(" sciencedirect, springer, wiley, ieee, tandfonline, nature")
|
|
971
|
+
publisher = typer.prompt("Enter publisher name")
|
|
972
|
+
|
|
973
|
+
from ..institutional.carsi_login import CARSIClient
|
|
974
|
+
carsi = CARSIClient(config)
|
|
975
|
+
try:
|
|
976
|
+
console.print(f"[bold]Federated login for: {publisher}[/bold]")
|
|
977
|
+
console.print(f"[dim]School: {config.get('carsi_idp_name', '')}[/dim]")
|
|
978
|
+
if carsi.login(publisher, force=force):
|
|
979
|
+
console.print("[green]Federated access session established![/green]")
|
|
980
|
+
else:
|
|
981
|
+
console.print("[red]Federated login failed.[/red]")
|
|
982
|
+
raise typer.Exit(1)
|
|
983
|
+
finally:
|
|
984
|
+
carsi.close()
|
|
985
|
+
|
|
986
|
+
|
|
987
|
+
@app.command("federated-login")
|
|
988
|
+
def federated_login(
|
|
989
|
+
publisher: str = typer.Option("", "--publisher", "-p", help="Publisher (sciencedirect, springer, wiley, ieee, tandfonline, nature). Omit to pick from article URL."),
|
|
990
|
+
url: str = typer.Option("", "--url", "-u", help="Article URL to auto-detect publisher."),
|
|
991
|
+
force: bool = typer.Option(False, "--force", "-f", help="Force re-login."),
|
|
992
|
+
verbose: bool = typer.Option(False, "--verbose", "-v", help="Verbose logging."),
|
|
993
|
+
):
|
|
994
|
+
"""Authenticate via federated institutional login."""
|
|
995
|
+
_run_federated_login(publisher, url, force, verbose)
|
|
996
|
+
|
|
997
|
+
|
|
998
|
+
@app.command("carsi-login", hidden=True)
|
|
999
|
+
def carsi_login(
|
|
1000
|
+
publisher: str = typer.Option("", "--publisher", "-p", help="Publisher (sciencedirect, springer, wiley, ieee, tandfonline, nature). Omit to pick from article URL."),
|
|
1001
|
+
url: str = typer.Option("", "--url", "-u", help="Article URL to auto-detect publisher."),
|
|
1002
|
+
force: bool = typer.Option(False, "--force", "-f", help="Force re-login."),
|
|
1003
|
+
verbose: bool = typer.Option(False, "--verbose", "-v", help="Verbose logging."),
|
|
1004
|
+
):
|
|
1005
|
+
"""Legacy alias for federated-login."""
|
|
1006
|
+
_run_federated_login(publisher, url, force, verbose)
|
|
1007
|
+
|
|
1008
|
+
|
|
1009
|
+
@app.command()
|
|
1010
|
+
def elsevier_setup(
|
|
1011
|
+
api_key: str = typer.Option("", "--api-key", help="Elsevier API key (32-char hex)."),
|
|
1012
|
+
inst_token: str = typer.Option("", "--inst-token", help="Elsevier institutional token."),
|
|
1013
|
+
validate: bool = typer.Option(False, "--validate", help="Validate an existing API key."),
|
|
1014
|
+
):
|
|
1015
|
+
"""Set up Elsevier API key for ScienceDirect full-text retrieval.
|
|
1016
|
+
|
|
1017
|
+
Get a free key at: https://dev.elsevier.com/
|
|
1018
|
+
"""
|
|
1019
|
+
cfg = load_config()
|
|
1020
|
+
|
|
1021
|
+
if api_key:
|
|
1022
|
+
cfg["elsevier_api_key"] = api_key
|
|
1023
|
+
save_config(cfg)
|
|
1024
|
+
console.print("[green]Elsevier API key saved.[/green]")
|
|
1025
|
+
|
|
1026
|
+
if inst_token:
|
|
1027
|
+
cfg["elsevier_insttoken"] = inst_token
|
|
1028
|
+
save_config(cfg)
|
|
1029
|
+
console.print("[green]Elsevier institutional token saved.[/green]")
|
|
1030
|
+
|
|
1031
|
+
key = cfg.get("elsevier_api_key", "")
|
|
1032
|
+
if not key:
|
|
1033
|
+
console.print("[yellow]No Elsevier API key configured.[/yellow]")
|
|
1034
|
+
console.print()
|
|
1035
|
+
console.print("To get a free API key:")
|
|
1036
|
+
console.print(" 1. Go to [cyan]https://dev.elsevier.com/[/cyan]")
|
|
1037
|
+
console.print(" 2. Register/sign in, then open My API Key / API Key Settings")
|
|
1038
|
+
console.print(" 3. Create an API key; choose ScienceDirect / Article Retrieval if asked")
|
|
1039
|
+
console.print(" 4. Run: [cyan]litloop elsevier-setup --api-key YOUR_KEY --validate[/cyan]")
|
|
1040
|
+
console.print()
|
|
1041
|
+
console.print("Closed full text still depends on institutional subscription/IP entitlement.")
|
|
1042
|
+
console.print("If your library provides an Elsevier institutional token:")
|
|
1043
|
+
console.print(" [cyan]litloop elsevier-setup --api-key KEY --inst-token TOKEN[/cyan]")
|
|
1044
|
+
raise typer.Exit(0)
|
|
1045
|
+
|
|
1046
|
+
if validate:
|
|
1047
|
+
console.print("Validating Elsevier API key...")
|
|
1048
|
+
import requests
|
|
1049
|
+
try:
|
|
1050
|
+
resp = requests.get(
|
|
1051
|
+
"https://api.elsevier.com/content/serial/title",
|
|
1052
|
+
headers={"X-ELS-APIKey": key, "Accept": "application/json"},
|
|
1053
|
+
params={"issn": "0043-1354"}, # Water Research
|
|
1054
|
+
timeout=15,
|
|
1055
|
+
)
|
|
1056
|
+
if resp.status_code == 200:
|
|
1057
|
+
console.print("[green]API key is valid![/green]")
|
|
1058
|
+
data = resp.json()
|
|
1059
|
+
titles = data.get("search-results", {}).get("entry", [])
|
|
1060
|
+
if titles:
|
|
1061
|
+
console.print(f" Test query returned: {titles[0].get('dc:title', 'N/A')[:60]}")
|
|
1062
|
+
elif resp.status_code == 401:
|
|
1063
|
+
console.print("[red]API key is invalid (HTTP 401).[/red]")
|
|
1064
|
+
else:
|
|
1065
|
+
console.print(f"[yellow]Unexpected response: HTTP {resp.status_code}[/yellow]")
|
|
1066
|
+
except Exception as e:
|
|
1067
|
+
console.print(f"[red]Validation failed: {e}[/red]")
|
|
1068
|
+
|
|
1069
|
+
# Check Article Retrieval XML access. Closed object-PDF entitlement is tested by normal download.
|
|
1070
|
+
console.print()
|
|
1071
|
+
console.print("Testing Article Retrieval XML access...")
|
|
1072
|
+
try:
|
|
1073
|
+
resp = requests.get(
|
|
1074
|
+
"https://api.elsevier.com/content/article/doi/10.1016/j.nicl.2021.102600",
|
|
1075
|
+
headers={"X-ELS-APIKey": key, "Accept": "application/xml"},
|
|
1076
|
+
params={"view": "FULL"},
|
|
1077
|
+
timeout=30,
|
|
1078
|
+
)
|
|
1079
|
+
ct = resp.headers.get("content-type", "")
|
|
1080
|
+
if resp.status_code == 200 and "xml" in ct:
|
|
1081
|
+
console.print(f"[green]Article Retrieval XML: YES ({len(resp.content)} bytes)[/green]")
|
|
1082
|
+
console.print(
|
|
1083
|
+
" Closed Elsevier PDFs use: view=FULL XML -> attachment-eid -> "
|
|
1084
|
+
"Content Object API /object/eid/{eid}."
|
|
1085
|
+
)
|
|
1086
|
+
else:
|
|
1087
|
+
console.print(f"[yellow]Article Retrieval XML: HTTP {resp.status_code} ({ct[:40]})[/yellow]")
|
|
1088
|
+
except Exception as e:
|
|
1089
|
+
console.print(f"[red]Article Retrieval XML test failed: {e}[/red]")
|
|
1090
|
+
|
|
1091
|
+
console.print()
|
|
1092
|
+
console.print(f" API Key: {key[:8]}...{key[-4:]}" if len(key) > 12 else f" API Key: {key}")
|
|
1093
|
+
_it = cfg.get("elsevier_insttoken", "") or "(not set)"
|
|
1094
|
+
console.print(f" Inst Token: {_it}")
|
|
1095
|
+
|
|
1096
|
+
|
|
1097
|
+
if __name__ == "__main__":
|
|
1098
|
+
app()
|