papersearch 0.5.14 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +28 -9
- package/README.md +28 -9
- package/carsi/paper_search_carsi/adapters/base.py +10 -0
- package/carsi/paper_search_carsi/adapters/elsevier/__init__.py +23 -3
- package/carsi/paper_search_carsi/adapters/ieee/__init__.py +1 -1
- package/carsi/paper_search_carsi/adapters/platforms/atypon/__init__.py +14 -11
- package/carsi/paper_search_carsi/adapters/platforms/figshare/__init__.py +8 -2
- package/carsi/paper_search_carsi/adapters/sage/__init__.py +2 -4
- package/carsi/paper_search_carsi/adapters/thieme/__init__.py +12 -10
- package/carsi/paper_search_carsi/adapters/world_scientific/__init__.py +2 -1
- package/carsi/paper_search_carsi/assets/capsule.html +0 -1
- package/carsi/paper_search_carsi/cli.py +65 -22
- package/carsi/paper_search_carsi/cold_start.py +3 -1
- package/carsi/paper_search_carsi/config.py +10 -3
- package/carsi/paper_search_carsi/download_diagnostics.py +1 -1
- package/carsi/paper_search_carsi/engine/batch.py +13 -3
- package/carsi/paper_search_carsi/engine/batch_access.py +3 -2
- package/carsi/paper_search_carsi/engine/batch_diagnostics.py +1 -1
- package/carsi/paper_search_carsi/engine/batch_institution.py +199 -79
- package/carsi/paper_search_carsi/engine/batch_login.py +39 -24
- package/carsi/paper_search_carsi/engine/batch_waits.py +2 -1
- package/carsi/paper_search_carsi/engine/bridge.py +31 -30
- package/carsi/paper_search_carsi/engine/login_wait.py +25 -3
- package/carsi/paper_search_carsi/engine/page_supplements.py +67 -0
- package/carsi/paper_search_carsi/federations/__init__.py +155 -0
- package/carsi/paper_search_carsi/federations/builtin/carsi.json +22 -0
- package/carsi/paper_search_carsi/federations/builtin/gakunin.json +11 -0
- package/carsi/paper_search_carsi/federations/builtin/hkaf.json +12 -0
- package/carsi/paper_search_carsi/federations/builtin/kafe.json +11 -0
- package/carsi/paper_search_carsi/federations/builtin/openathens.json +21 -0
- package/carsi/paper_search_carsi/federations/builtin/sgaf.json +11 -0
- package/carsi/paper_search_carsi/federations/builtin/sifulan.json +11 -0
- package/carsi/paper_search_carsi/federations/schools.py +393 -0
- package/carsi/paper_search_carsi/progress_view.py +0 -2
- package/carsi/paper_search_carsi/report_sanitizer.py +15 -0
- package/carsi/pyproject.toml +1 -1
- package/dist/capabilities/pdf-discovery/carsi/CarsiMcpClient.js +1 -1
- package/dist/capabilities/pdf-discovery/carsi/CarsiMcpClient.js.map +1 -1
- package/dist/management/carsi/installer.d.ts.map +1 -1
- package/dist/management/carsi/installer.js +23 -16
- package/dist/management/carsi/installer.js.map +1 -1
- package/package.json +1 -1
- package/skills/paper-search/SKILL.md +4 -3
- package/skills/paper-search/references/browser-download.md +1 -1
- package/skills/paper-search/references/carsi-setup.md +9 -8
- package/skills/paper-search/references/cli-contract.md +2 -2
- package/skills/paper-search/references/federation-support.md +31 -0
- package/skills/paper-search/references/publisher-support.md +2 -2
- package/skills/paper-search/references/setup.md +1 -1
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"code": "carsi",
|
|
3
|
+
"name": "CARSI",
|
|
4
|
+
"country": "CN",
|
|
5
|
+
"registration_authorities": [
|
|
6
|
+
"https://www.carsi.edu.cn"
|
|
7
|
+
],
|
|
8
|
+
"chooser_labels": [
|
|
9
|
+
"China CERNET Federation (CARSI)",
|
|
10
|
+
"China (CARSI)"
|
|
11
|
+
],
|
|
12
|
+
"chooser_pattern": "\\bCARSI\\b",
|
|
13
|
+
"discovery_hosts": [
|
|
14
|
+
"ds.carsi.edu.cn"
|
|
15
|
+
],
|
|
16
|
+
"service_domains": [
|
|
17
|
+
"carsi.edu.cn"
|
|
18
|
+
],
|
|
19
|
+
"metadata_url": "",
|
|
20
|
+
"member_list": "carsi",
|
|
21
|
+
"without_school": true
|
|
22
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"code": "gakunin",
|
|
3
|
+
"name": "GakuNin",
|
|
4
|
+
"country": "JP",
|
|
5
|
+
"registration_authorities": ["https://www.gakunin.jp"],
|
|
6
|
+
"chooser_labels": ["Academic Access Management Federation in Japan (GakuNin)"],
|
|
7
|
+
"chooser_pattern": "\\bGakuNin\\b|\\bAcademic\\s+Access\\s+Management\\s+Federation\\s+in\\s+Japan\\b",
|
|
8
|
+
"discovery_hosts": [],
|
|
9
|
+
"service_domains": ["gakunin.nii.ac.jp"],
|
|
10
|
+
"metadata_url": "https://metadata.gakunin.nii.ac.jp/gakunin-edugain-metadata.xml"
|
|
11
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"code": "hkaf",
|
|
3
|
+
"name": "HKAF",
|
|
4
|
+
"country": "HK",
|
|
5
|
+
"registration_authorities": ["https://hkaf.edu.hk"],
|
|
6
|
+
"chooser_labels": ["Hong Kong Access Federation"],
|
|
7
|
+
"chooser_pattern": "\\bHKAF\\b|\\bHong\\s+Kong\\s+Access\\s+Federation\\b",
|
|
8
|
+
"discovery_hosts": [],
|
|
9
|
+
"service_domains": ["hkaf.edu.hk"],
|
|
10
|
+
"metadata_url": "https://md.hkaf.edu.hk/hkaf-edugain-metadata.xml",
|
|
11
|
+
"member_list": ""
|
|
12
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"code": "kafe",
|
|
3
|
+
"name": "KAFE",
|
|
4
|
+
"country": "KR",
|
|
5
|
+
"registration_authorities": ["http://kafe.kreonet.net"],
|
|
6
|
+
"chooser_labels": ["Korean Access Federation"],
|
|
7
|
+
"chooser_pattern": "\\bKAFE\\b|\\bKorean\\s+Access\\s+Federation\\b",
|
|
8
|
+
"discovery_hosts": [],
|
|
9
|
+
"service_domains": ["kreonet.net"],
|
|
10
|
+
"metadata_url": "https://fedinfo.kreonet.net/signedmetadata/federation/Upstream-eduGAIN/metadata.xml"
|
|
11
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"code": "openathens",
|
|
3
|
+
"name": "OpenAthens",
|
|
4
|
+
"country": "",
|
|
5
|
+
"kind": "service",
|
|
6
|
+
"secondary": true,
|
|
7
|
+
"openathens_entries": true,
|
|
8
|
+
"registration_authorities": [
|
|
9
|
+
"https://openathens.net"
|
|
10
|
+
],
|
|
11
|
+
"chooser_labels": [
|
|
12
|
+
"OpenAthens Federation"
|
|
13
|
+
],
|
|
14
|
+
"chooser_pattern": "\\bOpenAthens\\b",
|
|
15
|
+
"discovery_hosts": [],
|
|
16
|
+
"service_domains": [],
|
|
17
|
+
"metadata_url": "",
|
|
18
|
+
"school_login_pages": [
|
|
19
|
+
"https://login.openathens.net/auth/*/o/"
|
|
20
|
+
]
|
|
21
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"code": "sgaf",
|
|
3
|
+
"name": "SGAF",
|
|
4
|
+
"country": "SG",
|
|
5
|
+
"registration_authorities": ["https://www.singaren.net.sg"],
|
|
6
|
+
"chooser_labels": ["Singapore Access Federation"],
|
|
7
|
+
"chooser_pattern": "\\bSGAF\\b|\\bSingapore\\s+Access\\s+Federation\\b",
|
|
8
|
+
"discovery_hosts": [],
|
|
9
|
+
"service_domains": ["sgaf.org.sg"],
|
|
10
|
+
"metadata_url": "https://ds.sgaf.org.sg/distribution/metadata/sgaf-edugain-export-metadata.xml"
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"code": "sifulan",
|
|
3
|
+
"name": "SIFULAN",
|
|
4
|
+
"country": "MY",
|
|
5
|
+
"registration_authorities": ["https://sifulan.my"],
|
|
6
|
+
"chooser_labels": ["Malaysian Access Federation (SIFULAN)"],
|
|
7
|
+
"chooser_pattern": "\\bSIFULAN\\b|\\bMalaysian\\s+Access\\s+Federation\\b",
|
|
8
|
+
"discovery_hosts": [],
|
|
9
|
+
"service_domains": ["sifulan.my"],
|
|
10
|
+
"metadata_url": "https://sifulan.my/metadata/edugain-metadata.xml"
|
|
11
|
+
}
|
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
"""The user's school: what setup saved, and finding it by the name the user gave.
|
|
2
|
+
|
|
3
|
+
Setup saves the school's names, its SAML entityID, the hosts of its login
|
|
4
|
+
pages and the federation it belongs to. The config keys keep their first names
|
|
5
|
+
(``carsi_idp_name``, ``carsi_idp_name_en``): they hold the school of any federation.
|
|
6
|
+
|
|
7
|
+
Finding a school reads only the federations' own data (the user, 2026-11-11):
|
|
8
|
+
CARSI's member list, SeamlessAccess's index of the schools of eduGAIN's
|
|
9
|
+
federations, and a federation's own member list (its SAML metadata). A school
|
|
10
|
+
without a login entry there cannot sign in through a federation at all, so no
|
|
11
|
+
other source is asked. The names a school is listed under are its federation's
|
|
12
|
+
(the local language and English); a name in another language comes with the
|
|
13
|
+
school's English name from the caller (``also``).
|
|
14
|
+
|
|
15
|
+
One rule decides: one school whose name (or domain, or entityID) is exactly
|
|
16
|
+
what was given is the school; otherwise the matches are candidates for the
|
|
17
|
+
user to pick from once, a single partial match too (see ``_Found.decide``).
|
|
18
|
+
"""
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import hashlib
|
|
22
|
+
import re
|
|
23
|
+
import time
|
|
24
|
+
import xml.etree.ElementTree as ET
|
|
25
|
+
from dataclasses import dataclass, field
|
|
26
|
+
from pathlib import Path
|
|
27
|
+
from typing import Any, Iterable
|
|
28
|
+
from urllib.parse import quote, urlparse
|
|
29
|
+
|
|
30
|
+
from . import Federation, all_federations, by_code, by_registration_authority, configured
|
|
31
|
+
|
|
32
|
+
SEAMLESSACCESS = "https://md.seamlessaccess.org/entities/"
|
|
33
|
+
METADATA_MAX_AGE_SEC = 7 * 24 * 3600
|
|
34
|
+
_NS = {"md": "urn:oasis:names:tc:SAML:2.0:metadata", "shibmd": "urn:mace:shibboleth:metadata:1.0",
|
|
35
|
+
"mdui": "urn:oasis:names:tc:SAML:metadata:ui", "mdrpi": "urn:oasis:names:tc:SAML:metadata:rpi"}
|
|
36
|
+
_LANG = "{http://www.w3.org/XML/1998/namespace}lang"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass(frozen=True)
|
|
40
|
+
class School:
|
|
41
|
+
name: str
|
|
42
|
+
name_en: str
|
|
43
|
+
entity_id: str
|
|
44
|
+
federation: str
|
|
45
|
+
domain: str = ""
|
|
46
|
+
login_hosts: tuple[str, ...] = ()
|
|
47
|
+
# Every domain the school's entry is registered for (tells the same school in two routes).
|
|
48
|
+
scopes: tuple[str, ...] = field(default=(), compare=False)
|
|
49
|
+
|
|
50
|
+
def summary(self) -> dict[str, str]:
|
|
51
|
+
federation = by_code(self.federation)
|
|
52
|
+
return {"name": self.name, "name_en": self.name_en, "federation": federation.name if federation else self.federation,
|
|
53
|
+
"country": federation.country if federation else "", "domain": self.domain, "entity_id": self.entity_id}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass
|
|
57
|
+
class Lookup:
|
|
58
|
+
school: School | None = None
|
|
59
|
+
candidates: list[School] = field(default_factory=list)
|
|
60
|
+
# Exact matches in federations not supported yet: (school name, its registrationAuthority).
|
|
61
|
+
unsupported: list[tuple[str, str]] = field(default_factory=list)
|
|
62
|
+
# Sources that could not be read, by name.
|
|
63
|
+
unavailable: list[str] = field(default_factory=list)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
# --------------------------------------------------------------------------- saved
|
|
67
|
+
|
|
68
|
+
def names(config: dict[str, Any]) -> tuple[str, ...]:
|
|
69
|
+
"""The saved school's names: as its federation lists it, then in English."""
|
|
70
|
+
found = [str(config.get(key) or "").strip() for key in ("carsi_idp_name", "carsi_idp_name_en")]
|
|
71
|
+
return tuple(dict.fromkeys(n for n in found if n))
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def name(config: dict[str, Any]) -> str:
|
|
75
|
+
"""The saved school's name, typed into choosers ("" when none is saved)."""
|
|
76
|
+
return str(config.get("carsi_idp_name") or "").strip()
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def login_hosts(config: dict[str, Any]) -> tuple[str, ...]:
|
|
80
|
+
"""The hosts of the saved school's login pages: its entityID's host
|
|
81
|
+
(``https://idp.example.edu.cn/idp/shibboleth`` -> ``idp.example.edu.cn``) and
|
|
82
|
+
the login hosts its federation lists for it; empty without one."""
|
|
83
|
+
hosts = [str(h).strip().lower() for h in config.get("idp_login_hosts") or () if str(h).strip()]
|
|
84
|
+
entity_id = str(config.get("idp_entity_id", "") or "").strip()
|
|
85
|
+
if entity_id.startswith("https://"):
|
|
86
|
+
hosts.insert(0, (urlparse(entity_id).hostname or "").lower())
|
|
87
|
+
return tuple(dict.fromkeys(h for h in hosts if h))
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def domains(config: dict[str, Any]) -> tuple[str, ...]:
|
|
91
|
+
"""The saved school's own domains (its entry's scopes): any page under them is
|
|
92
|
+
the school's side of the login, its portal and single sign-on included
|
|
93
|
+
(a portal on another host of the school's domain, 2026-11-11)."""
|
|
94
|
+
return tuple(str(d).strip().lower().lstrip(".") for d in config.get("idp_domains") or () if str(d).strip())
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def login_pages(config: dict[str, Any]) -> tuple[str, ...]:
|
|
98
|
+
"""Where the saved school's own login pages begin on a service shared by many
|
|
99
|
+
schools (its OpenAthens sign-in page), saved by setup from its route's data;
|
|
100
|
+
``*`` stands for any one path segment."""
|
|
101
|
+
return tuple(str(p).strip().lower() for p in config.get("idp_login_pages") or () if str(p).strip())
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def on_login_page(config: dict[str, Any], url: str) -> bool:
|
|
105
|
+
"""``url`` begins with one of the saved school's login pages (``login_pages``)."""
|
|
106
|
+
lower = str(url or "").lower()
|
|
107
|
+
for page in login_pages(config):
|
|
108
|
+
pattern = re.escape(page).replace(r"\*", "[^/?#]+")
|
|
109
|
+
if re.match(pattern, lower):
|
|
110
|
+
return True
|
|
111
|
+
return False
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def entity_id(config: dict[str, Any], data_dir: Path) -> str:
|
|
115
|
+
"""The saved school's entityID: ``idp_entity_id``, else, for a CARSI school
|
|
116
|
+
saved by name only (before setup used the member list), CARSI's entry for
|
|
117
|
+
that name in the copy of its list setup saved (a download never fetches it)."""
|
|
118
|
+
configured_id = str(config.get("idp_entity_id") or "").strip()
|
|
119
|
+
if configured_id:
|
|
120
|
+
return configured_id
|
|
121
|
+
saved = name(config)
|
|
122
|
+
if not saved:
|
|
123
|
+
return ""
|
|
124
|
+
from ..carsi_schools import CACHE_FILE, choose_school, parse_school_list
|
|
125
|
+
|
|
126
|
+
cache = Path(data_dir) / CACHE_FILE
|
|
127
|
+
if not cache.is_file():
|
|
128
|
+
return ""
|
|
129
|
+
try:
|
|
130
|
+
school, _ = choose_school(parse_school_list(cache.read_text(encoding="utf-8")), saved)
|
|
131
|
+
except ValueError:
|
|
132
|
+
return ""
|
|
133
|
+
return school.entity_id if school else ""
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def save(config: dict[str, Any], school: School) -> None:
|
|
137
|
+
"""Make ``school`` the user's school, with its federation."""
|
|
138
|
+
federation = by_code(school.federation)
|
|
139
|
+
scopes = school.scopes or ((school.domain,) if school.domain else ())
|
|
140
|
+
pages = [page for template in (federation.school_login_pages if federation else ())
|
|
141
|
+
for page in ([template.replace("{scope}", scope) for scope in scopes] if "{scope}" in template
|
|
142
|
+
else [template])]
|
|
143
|
+
config.update(carsi_idp_name=school.name, carsi_idp_name_en=school.name_en, idp_entity_id=school.entity_id,
|
|
144
|
+
idp_login_hosts=list(school.login_hosts), idp_login_pages=pages,
|
|
145
|
+
idp_domains=[d for d in scopes if "." in d],
|
|
146
|
+
publisher_institution_federation=school.federation, carsi_enabled=school.federation == "carsi")
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def institutional_configured(config: dict[str, Any]) -> bool:
|
|
150
|
+
"""Some institutional route is configured: a federation, or a school saved
|
|
151
|
+
under the old switch (``carsi_enabled``) before federations were named."""
|
|
152
|
+
return bool(configured(config) or (config.get("carsi_enabled") and name(config)))
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
# --------------------------------------------------------------------------- finding
|
|
156
|
+
|
|
157
|
+
def _key(text: str) -> str:
|
|
158
|
+
"""Case, spaces, hyphens, punctuation and a leading "The" do not count."""
|
|
159
|
+
return re.sub(r"[\W_]+", "", re.sub(r"^the\s+", "", str(text or "").casefold().strip()))
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _host(text: str) -> str:
|
|
163
|
+
text = str(text or "").strip().lower()
|
|
164
|
+
return (urlparse(text).hostname or "") if "://" in text else text.removeprefix("www.")
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def _is_entity_id(text: str) -> bool:
|
|
168
|
+
return text.startswith(("https://", "http://", "urn:"))
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
class _Found:
|
|
172
|
+
"""Candidates by entityID, an exact match outranking a partial one."""
|
|
173
|
+
|
|
174
|
+
def __init__(self) -> None:
|
|
175
|
+
self.rows: dict[str, tuple[School, bool]] = {}
|
|
176
|
+
|
|
177
|
+
def add(self, school: School, exact: bool) -> None:
|
|
178
|
+
known = self.rows.get(school.entity_id)
|
|
179
|
+
if known is None or (exact and not known[1]):
|
|
180
|
+
self.rows[school.entity_id] = (school, exact)
|
|
181
|
+
|
|
182
|
+
def _primary(self) -> None:
|
|
183
|
+
"""A school also listed in a secondary route (OpenAthens) under the same
|
|
184
|
+
domain or name signs in through its federation: that entry goes."""
|
|
185
|
+
primary = [s for s, _ in self.rows.values() if not _secondary(s)]
|
|
186
|
+
domains = {d for s in primary for d in s.scopes}
|
|
187
|
+
names = {_key(s.name_en) for s in primary}
|
|
188
|
+
for entity, (school, _) in list(self.rows.items()):
|
|
189
|
+
if _secondary(school) and (set(school.scopes) & domains or _key(school.name_en) in names):
|
|
190
|
+
del self.rows[entity]
|
|
191
|
+
|
|
192
|
+
def decide(self) -> tuple[School | None, list[School]]:
|
|
193
|
+
"""The one exact match; else the candidates, a single partial match too:
|
|
194
|
+
across federations a near name can be another school (a Chinese name
|
|
195
|
+
matches a campus in CARSI, the school itself is listed in English)."""
|
|
196
|
+
self._primary()
|
|
197
|
+
exact = [s for s, e in self.rows.values() if e]
|
|
198
|
+
if len(exact) == 1:
|
|
199
|
+
return exact[0], []
|
|
200
|
+
if exact:
|
|
201
|
+
return None, exact
|
|
202
|
+
partial = [s for s, e in self.rows.values() if not e]
|
|
203
|
+
return None, sorted(partial, key=lambda s: (len(s.name), s.name))
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def _secondary(school: School) -> bool:
|
|
207
|
+
federation = by_code(school.federation)
|
|
208
|
+
return bool(federation and federation.secondary)
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
# An entry its federation marks as not for use (an old server, a test).
|
|
212
|
+
_RETIRED = re.compile(r"\bdo not use\b|\btest\b|\blegacy\b", re.I)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
_PARTS = re.compile(r"\s+[-\u2013\u2014]\s+")
|
|
216
|
+
_BRACKETED = re.compile(r"^(.*?)\s*\(([^()]+)\)\s*$")
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def _names(titles: Iterable[str]) -> list[str]:
|
|
220
|
+
"""The names a school is listed under, and their parts: "NHU - National
|
|
221
|
+
Harbour University" (an abbreviation before the name), "北大学 (Kita
|
|
222
|
+
University) – OpenAthens" (a second name in brackets, the route after a dash)."""
|
|
223
|
+
names = [str(t).strip() for t in titles if str(t or "").strip()]
|
|
224
|
+
found = list(names)
|
|
225
|
+
for name in names:
|
|
226
|
+
for part in _PARTS.split(name):
|
|
227
|
+
bracketed = _BRACKETED.match(part.strip())
|
|
228
|
+
found += [part.strip(), *([bracketed.group(1).strip(), bracketed.group(2).strip()] if bracketed else [])]
|
|
229
|
+
return list(dict.fromkeys(n for n in found if n))
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def _matches(given: Iterable[str], listed_names: Iterable[str], scopes: Iterable[str], entity: str) -> tuple[bool, bool]:
|
|
233
|
+
"""(exact, partial) for what was given against one listed school."""
|
|
234
|
+
keys = {_key(n) for n in listed_names} - {""}
|
|
235
|
+
hosts = {s.lower() for s in scopes if s}
|
|
236
|
+
exact = partial = False
|
|
237
|
+
for text in given:
|
|
238
|
+
key, host = _key(text), _host(text)
|
|
239
|
+
if not key:
|
|
240
|
+
continue
|
|
241
|
+
if text == entity or key in keys or host in hosts:
|
|
242
|
+
exact = True
|
|
243
|
+
elif any(key in k for k in keys) or any(host and host in h for h in hosts):
|
|
244
|
+
partial = True
|
|
245
|
+
return exact, partial and not exact
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def _from_carsi_list(given: list[str], config: dict[str, Any], data_dir: Path, found: _Found, lookup: Lookup) -> None:
|
|
249
|
+
from ..carsi_schools import find_schools, load_school_list
|
|
250
|
+
|
|
251
|
+
try:
|
|
252
|
+
schools = load_school_list(config, data_dir)
|
|
253
|
+
except Exception:
|
|
254
|
+
lookup.unavailable.append("CARSI")
|
|
255
|
+
return
|
|
256
|
+
for text in given:
|
|
257
|
+
exact, partial = find_schools(schools, text)
|
|
258
|
+
for school, is_exact in [(s, True) for s in exact] + [(s, False) for s in partial]:
|
|
259
|
+
found.add(School(school.name, school.name_en, school.entity_id, "carsi", school.domain), is_exact)
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def _seamlessaccess(text: str, config: dict[str, Any]) -> list[dict[str, Any]]:
|
|
263
|
+
from ..network import fetch
|
|
264
|
+
|
|
265
|
+
url = (SEAMLESSACCESS + quote("{sha1}" + hashlib.sha1(text.encode()).hexdigest(), safe="")
|
|
266
|
+
if _is_entity_id(text) else SEAMLESSACCESS + "?q=" + quote(text))
|
|
267
|
+
response = fetch(url, config, headers={"Accept": "application/json"})
|
|
268
|
+
if response.status_code == 404:
|
|
269
|
+
return []
|
|
270
|
+
response.raise_for_status()
|
|
271
|
+
rows = response.json()
|
|
272
|
+
return [rows] if isinstance(rows, dict) else [r for r in rows if isinstance(r, dict)]
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def _from_seamlessaccess(given: list[str], config: dict[str, Any], found: _Found, lookup: Lookup,
|
|
276
|
+
carsi_listed: bool) -> None:
|
|
277
|
+
for text in given:
|
|
278
|
+
try:
|
|
279
|
+
rows = _seamlessaccess(text, config)
|
|
280
|
+
except Exception:
|
|
281
|
+
if "SeamlessAccess" not in lookup.unavailable:
|
|
282
|
+
lookup.unavailable.append("SeamlessAccess")
|
|
283
|
+
return
|
|
284
|
+
for row in rows:
|
|
285
|
+
entity = str(row.get("entity_id") or row.get("entityID") or "")
|
|
286
|
+
title = str(row.get("title") or "")
|
|
287
|
+
if (row.get("type") != "idp" or not entity or str(row.get("hidden")).lower() == "true"
|
|
288
|
+
or _RETIRED.search(title)):
|
|
289
|
+
continue
|
|
290
|
+
titles = _names([*(row.get("title_langs") or {}).values(), title])
|
|
291
|
+
scopes = [s.strip().lower() for s in str(row.get("scope") or row.get("domain") or "").split(",") if s.strip()]
|
|
292
|
+
exact, partial = _matches([text], titles, scopes, entity)
|
|
293
|
+
if not (exact or partial):
|
|
294
|
+
continue
|
|
295
|
+
authorities = [str(a) for a in row.get("registrationAuthority") or ()]
|
|
296
|
+
federation = next((f for f in map(by_registration_authority, authorities) if f), None)
|
|
297
|
+
if federation is None:
|
|
298
|
+
if exact:
|
|
299
|
+
lookup.unsupported.append((title or entity, ", ".join(authorities)))
|
|
300
|
+
continue
|
|
301
|
+
if federation.member_list == "carsi" and carsi_listed:
|
|
302
|
+
continue # CARSI's own list is the source for its schools
|
|
303
|
+
english = str((row.get("title_langs") or {}).get("en") or title)
|
|
304
|
+
found.add(School(english, english, entity, federation.code, scopes[0] if scopes else "",
|
|
305
|
+
scopes=tuple(scopes)), exact)
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def _metadata(federation: Federation, config: dict[str, Any], data_dir: Path) -> ET.Element:
|
|
309
|
+
"""The federation's member list (SAML metadata), from a copy under a week old."""
|
|
310
|
+
cache = Path(data_dir) / f"federation-{federation.code}.xml"
|
|
311
|
+
if cache.exists() and time.time() - cache.stat().st_mtime < METADATA_MAX_AGE_SEC:
|
|
312
|
+
return ET.fromstring(cache.read_bytes())
|
|
313
|
+
from ..network import fetch
|
|
314
|
+
|
|
315
|
+
response = fetch(federation.metadata_url, config)
|
|
316
|
+
response.raise_for_status()
|
|
317
|
+
root = ET.fromstring(response.content)
|
|
318
|
+
cache.parent.mkdir(parents=True, exist_ok=True)
|
|
319
|
+
cache.write_bytes(response.content)
|
|
320
|
+
return root
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
def _metadata_schools(federation: Federation, root: ET.Element) -> list[tuple[School, list[str], list[str]]]:
|
|
324
|
+
"""(school, its names, its scopes) for every school in a federation's metadata."""
|
|
325
|
+
schools = []
|
|
326
|
+
for entity in root.iter(f"{{{_NS['md']}}}EntityDescriptor"):
|
|
327
|
+
idp = entity.find("md:IDPSSODescriptor", _NS)
|
|
328
|
+
entity_id = entity.get("entityID") or ""
|
|
329
|
+
if idp is None or not entity_id:
|
|
330
|
+
continue
|
|
331
|
+
display = {n.get(_LANG) or "": (n.text or "").strip()
|
|
332
|
+
for n in idp.findall("md:Extensions/mdui:UIInfo/mdui:DisplayName", _NS)}
|
|
333
|
+
organization = [(n.text or "").strip() for n in entity.findall("md:Organization/md:OrganizationDisplayName", _NS)]
|
|
334
|
+
listed = _names([*display.values(), *organization])
|
|
335
|
+
english = display.get("en") or (organization[0] if organization else entity_id)
|
|
336
|
+
if _RETIRED.search(english):
|
|
337
|
+
continue
|
|
338
|
+
scopes = [(s.text or "").strip().lower() for s in idp.findall("md:Extensions/shibmd:Scope", _NS)]
|
|
339
|
+
locations = ((urlparse(s.get("Location") or "").hostname or "").lower()
|
|
340
|
+
for s in idp.findall("md:SingleSignOnService", _NS))
|
|
341
|
+
hosts = tuple(dict.fromkeys(h for h in locations if h))
|
|
342
|
+
schools.append((School(english, english, entity_id, federation.code, scopes[0] if scopes else "", hosts,
|
|
343
|
+
tuple(scopes)), listed, scopes))
|
|
344
|
+
return schools
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
def _from_metadata(given: list[str], config: dict[str, Any], data_dir: Path, found: _Found, lookup: Lookup) -> None:
|
|
348
|
+
"""Each federation's own member list, when SeamlessAccess could not be read."""
|
|
349
|
+
for federation in all_federations():
|
|
350
|
+
if not federation.metadata_url:
|
|
351
|
+
continue
|
|
352
|
+
try:
|
|
353
|
+
root = _metadata(federation, config, data_dir)
|
|
354
|
+
except Exception:
|
|
355
|
+
lookup.unavailable.append(federation.name)
|
|
356
|
+
continue
|
|
357
|
+
for school, listed, scopes in _metadata_schools(federation, root):
|
|
358
|
+
exact, partial = _matches(given, listed, scopes, school.entity_id)
|
|
359
|
+
if exact or partial:
|
|
360
|
+
found.add(school, exact)
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
def _with_login_hosts(school: School, config: dict[str, Any], data_dir: Path) -> School:
|
|
364
|
+
"""The school with the hosts of its login pages from its federation's metadata."""
|
|
365
|
+
federation = by_code(school.federation)
|
|
366
|
+
if school.login_hosts or federation is None or not federation.metadata_url:
|
|
367
|
+
return school
|
|
368
|
+
try:
|
|
369
|
+
root = _metadata(federation, config, data_dir)
|
|
370
|
+
except Exception:
|
|
371
|
+
return school # its entityID's host still tells its login page
|
|
372
|
+
listed = next((s for s, _, _ in _metadata_schools(federation, root) if s.entity_id == school.entity_id), None)
|
|
373
|
+
if listed is None:
|
|
374
|
+
return school
|
|
375
|
+
return School(school.name, school.name_en, school.entity_id, school.federation,
|
|
376
|
+
school.domain or listed.domain, listed.login_hosts, school.scopes)
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
def find(query: str, config: dict[str, Any], data_dir: Path, *, also: Iterable[str] = ()) -> Lookup:
|
|
380
|
+
"""The school named ``query`` (any language, its domain or entityID), with
|
|
381
|
+
``also`` its other names (the English one, given by the caller)."""
|
|
382
|
+
given = list(dict.fromkeys(t.strip() for t in (query, *also) if t and t.strip()))
|
|
383
|
+
lookup, found = Lookup(), _Found()
|
|
384
|
+
if not given:
|
|
385
|
+
return lookup
|
|
386
|
+
_from_carsi_list(given, config, data_dir, found, lookup)
|
|
387
|
+
_from_seamlessaccess(given, config, found, lookup, carsi_listed="CARSI" not in lookup.unavailable)
|
|
388
|
+
if "SeamlessAccess" in lookup.unavailable:
|
|
389
|
+
_from_metadata(given, config, data_dir, found, lookup)
|
|
390
|
+
school, candidates = found.decide()
|
|
391
|
+
lookup.school = _with_login_hosts(school, config, data_dir) if school else None
|
|
392
|
+
lookup.candidates = candidates[:10]
|
|
393
|
+
return lookup
|
|
@@ -376,8 +376,6 @@ def view(state: dict[str, Any] | None, now: float) -> dict[str, Any]:
|
|
|
376
376
|
"title": str(state.get("title") or ""), "current": current,
|
|
377
377
|
"run_id": str(state.get("run_id") or ""),
|
|
378
378
|
"step": step, "steps": list(STEPS), "line": status_text(state, running) if running else "",
|
|
379
|
-
# What each lane got in a batch checking open access beside its downloads, on one line.
|
|
380
|
-
"lanes": str(state.get("lanes") or "") if running else "",
|
|
381
379
|
"elapsed": elapsed, "attention": prompt, "notice": notice,
|
|
382
380
|
"completion_detail": str(state.get("phase") or "")
|
|
383
381
|
if not running and state.get("stage") == "supplements_checked" else "",
|
|
@@ -56,6 +56,21 @@ class Redactor:
|
|
|
56
56
|
host = host_of(value) or (value if key.endswith('_domain') else '')
|
|
57
57
|
if host:
|
|
58
58
|
self.school_hosts.add(host)
|
|
59
|
+
# The hosts of the school's login pages, as its federation lists them.
|
|
60
|
+
for value in cfg.get('idp_login_hosts') or ():
|
|
61
|
+
host = str(value or '').strip().lower()
|
|
62
|
+
if len(host) > 2:
|
|
63
|
+
self.terms.add(host)
|
|
64
|
+
self.school_hosts.add(host)
|
|
65
|
+
for value in cfg.get('idp_domains') or ():
|
|
66
|
+
host = str(value or '').strip().lower()
|
|
67
|
+
if len(host) > 3:
|
|
68
|
+
self.terms.add(host)
|
|
69
|
+
self.school_hosts.add(host)
|
|
70
|
+
for value in cfg.get('idp_login_pages') or ():
|
|
71
|
+
page = str(value or '').strip()
|
|
72
|
+
if len(page) > 2:
|
|
73
|
+
self.terms.add(page)
|
|
59
74
|
# The configured IdP often lives below the institution's domain.
|
|
60
75
|
for host in list(self.school_hosts):
|
|
61
76
|
if host.split('.')[0] in {'idp', 'sso', 'login', 'auth'} and host.count('.') >= 2:
|
package/carsi/pyproject.toml
CHANGED
|
@@ -39,7 +39,7 @@ where = ["."]
|
|
|
39
39
|
include = ["paper_search_carsi*"]
|
|
40
40
|
|
|
41
41
|
[tool.setuptools.package-data]
|
|
42
|
-
paper_search_carsi = ["publishers/builtin/*.json", "publishers/schema.json", "data/*.json", "assets/*.png", "assets/*.html", "assets/*.json", "assets/papersearch-notifier"]
|
|
42
|
+
paper_search_carsi = ["publishers/builtin/*.json", "federations/builtin/*.json", "publishers/schema.json", "data/*.json", "assets/*.png", "assets/*.html", "assets/*.json", "assets/papersearch-notifier"]
|
|
43
43
|
|
|
44
44
|
[tool.pytest.ini_options]
|
|
45
45
|
testpaths = ["tests"]
|
|
@@ -18,7 +18,7 @@ export const CARSI_DOWNLOAD_TIMEOUT_MS = 1_200_000;
|
|
|
18
18
|
export const CARSI_CLEANUP_TIMEOUT_MS = 5_000;
|
|
19
19
|
const CARSI_BACKEND_SOURCE = "carsi";
|
|
20
20
|
// MCP clientInfo is informational only; keep in step with package.json on release.
|
|
21
|
-
const PAPER_SEARCH_MCP_CLIENT_VERSION = "0.
|
|
21
|
+
const PAPER_SEARCH_MCP_CLIENT_VERSION = "0.6.0";
|
|
22
22
|
const NO_BATCH_SUPPORT = {
|
|
23
23
|
downloadFields: false,
|
|
24
24
|
supplementsField: false,
|