forked from c55Math4833/ResultsExportForWebOfScience
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwos_export.py
More file actions
452 lines (386 loc) · 15.1 KB
/
Copy pathwos_export.py
File metadata and controls
452 lines (386 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#!/usr/bin/env python3
"""Export Web of Science search results from a summary URL."""
from __future__ import annotations
import argparse
import json
import os
import re
import time
import urllib.error
import urllib.parse
import urllib.request
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path
FIELDS = [
"AUTHORS",
"TITLE",
"SOURCE",
"DOI",
"CONFERENCE_INFO",
"CITTIMES",
"ACCESSION_NUM",
"AUTHORSIDENTIFIERS",
"ISSN",
"PMID",
"ABSTRACT",
"ADDRS",
"AFFILIATIONS",
"DOCTYPE",
"KEYWORDS",
"JCR_CATEGORY",
"SUBJECT_CATEGORY",
"WOS_EDITIONS",
"CITREF",
"CITREFC",
"USAGEIND",
"HOT_PAPER",
"HIGHLY_CITED",
"FUNDING",
"PUBINFO",
"OPEN_ACCESS",
"PAGEC",
"SABBR",
"IDS",
"LANG",
]
UUID_LIKE_ID_RE = re.compile(
r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}(?:-[0-9a-f]+)?$",
re.IGNORECASE,
)
STORAGE_SID_RE = re.compile(rb"\b[A-Za-z0-9][A-Za-z0-9._~=-]{15,80}\b")
def build_referer(origin: str, parent_qid: str, sid: str) -> str:
if sid:
return f"{origin}/wos/woscc/summary/{parent_qid}/{sid}/relevance/1(overlay:export/exp)"
return f"{origin}/wos/woscc/summary/{parent_qid}/relevance/1(overlay:export/exp)"
def is_likely_export_sid(value: str) -> bool:
value = value.strip()
if not value or "/" in value or "\\" in value or any(ch.isspace() for ch in value):
return False
if UUID_LIKE_ID_RE.fullmatch(value):
return False
return bool(re.fullmatch(r"[A-Za-z0-9._~=-]{8,}", value))
def is_likely_storage_sid(value: str) -> bool:
if not is_likely_export_sid(value):
return False
if not (20 <= len(value) <= 64):
return False
if not re.fullmatch(r"[A-Za-z0-9._~=-]+", value):
return False
lowered = value.lower()
if lowered.startswith(("wos_", "snowplow", "current", "notice", "stats", "tabmanager")):
return False
return any(ch.isalpha() for ch in value) and any(ch.isdigit() for ch in value)
def parse_summary_input(value: str) -> dict[str, str]:
value = value.strip()
if not value:
raise ValueError("empty QID or URL")
if "://" not in value:
origin = "https://www.webofscience.com"
return {
"parent_qid": value,
"sid": "",
"route_id": "",
"origin": origin,
"referer": build_referer(origin, value, ""),
}
parsed = urllib.parse.urlparse(value)
if not parsed.scheme or not parsed.netloc:
raise ValueError("invalid Web of Science URL")
parts = [part for part in parsed.path.split("/") if part]
try:
index = parts.index("summary")
except ValueError as exc:
raise ValueError("could not find '/summary/' in URL") from exc
if index + 1 >= len(parts):
raise ValueError("could not find QID after '/summary/'")
parent_qid = parts[index + 1]
route_id = ""
if index + 2 < len(parts) and parts[index + 2] != "relevance":
route_id = parts[index + 2]
sid = route_id if is_likely_export_sid(route_id) else ""
origin = f"{parsed.scheme}://{parsed.netloc}"
return {
"parent_qid": parent_qid,
"sid": sid,
"route_id": route_id,
"origin": origin,
"referer": build_referer(origin, parent_qid, route_id),
}
def extract_sid_from_url(url: str) -> str:
parsed = urllib.parse.urlparse(url)
query = urllib.parse.parse_qs(parsed.query)
referrer = query.get("referrer", [""])[0]
match = re.search(r"SID=([^&]+)", referrer)
if match:
return match.group(1)
return query.get("SID", [""])[0]
def browser_user_data_roots() -> list[Path]:
local_app_data = Path(os.environ.get("LOCALAPPDATA", ""))
if not local_app_data:
return []
return [
local_app_data / "Google" / "Chrome" / "User Data",
local_app_data / "Microsoft" / "Edge" / "User Data",
local_app_data / "BraveSoftware" / "Brave-Browser" / "User Data",
]
def browser_storage_leveldb_dirs() -> list[Path]:
dirs = []
for root in browser_user_data_roots():
if not root.exists():
continue
for profile in root.iterdir():
leveldb = profile / "Local Storage" / "leveldb"
if leveldb.is_dir():
dirs.append(leveldb)
return dirs
def sid_storage_needles(origin: str) -> list[bytes]:
origins = {
"https://webofscience.clarivate.cn",
"https://www.webofscience.com",
"https://www.webofknowledge.com",
}
parsed = urllib.parse.urlparse(origin)
if parsed.scheme and parsed.netloc:
origins.add(f"{parsed.scheme}://{parsed.netloc}")
needles = set()
for item in origins:
parsed_item = urllib.parse.urlparse(item)
needles.add(item.lower().encode("utf-8"))
if parsed_item.netloc:
needles.add(parsed_item.netloc.lower().encode("utf-8"))
return sorted(needles)
def iter_browser_storage_sid_candidates(origin: str) -> list[str]:
needles = sid_storage_needles(origin)
candidates: dict[str, float] = {}
for leveldb in browser_storage_leveldb_dirs():
for path in list(leveldb.glob("*.ldb")) + list(leveldb.glob("*.log")):
try:
if path.stat().st_size > 50_000_000:
continue
data = path.read_bytes()
except OSError:
continue
lowered = data.lower()
if not any(needle in lowered for needle in needles):
continue
interesting_offsets: list[int] = []
for needle in [*needles, b"sid"]:
start = 0
while len(interesting_offsets) < 300:
index = lowered.find(needle, start)
if index < 0:
break
interesting_offsets.append(index)
start = index + 1
modified_at = path.stat().st_mtime
for index in interesting_offsets:
snippet = data[max(0, index - 300) : index + 800]
snippet_lowered = snippet.lower()
if b"sid" not in snippet_lowered and not any(needle in snippet_lowered for needle in needles):
continue
for match in STORAGE_SID_RE.finditer(snippet):
try:
candidate = match.group(0).decode("ascii")
except UnicodeDecodeError:
continue
if is_likely_storage_sid(candidate):
candidates[candidate] = max(candidates.get(candidate, 0), modified_at)
def priority(item: tuple[str, float]) -> tuple[int, float, int, str]:
candidate, modified_at = item
has_upper = any(ch.isupper() for ch in candidate)
has_lower = any(ch.islower() for ch in candidate)
has_digit = any(ch.isdigit() for ch in candidate)
rank = 100
if candidate.isalnum() and 24 <= len(candidate) <= 40 and has_upper and has_lower and has_digit:
rank -= 60
if candidate.startswith(("USW", "USE", "EUW", "EUC", "AP")):
rank -= 30
if not candidate.isalnum():
rank += 20
if len(candidate) > 48:
rank += 20
if candidate.lower().startswith(("wos", "snow", "queue", "track", "suite")):
rank += 40
return (rank, -modified_at, abs(len(candidate) - 30), candidate)
return [candidate for candidate, _ in sorted(candidates.items(), key=priority)][:80]
def validate_sid(origin: str, sid: str) -> bool:
if not sid:
return False
origin = origin.strip().rstrip("/") or "https://webofscience.clarivate.cn"
body = b"{}"
headers = {
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126 Safari/537.36"
),
"Accept": "application/json",
"Content-Type": "application/json",
"Origin": origin,
"Referer": f"{origin}/wos/",
"x-1p-wos-sid": sid,
}
request = urllib.request.Request(
f"{origin}/api/esti/Session/getSessionData",
data=body,
headers=headers,
method="POST",
)
try:
with urllib.request.urlopen(request, timeout=12) as response:
payload = json.loads(response.read().decode("utf-8", errors="replace"))
except (urllib.error.URLError, ValueError, json.JSONDecodeError):
return False
return isinstance(payload, dict) and (
"BasicProperties" in payload or "Products" in payload or "CustomerProperties" in payload
)
def get_sid_from_browser_storage(origin: str) -> str:
for candidate in iter_browser_storage_sid_candidates(origin):
if validate_sid(origin, candidate):
return candidate
return ""
def get_sid(origin: str) -> str:
candidates = []
origin = origin.strip().rstrip("/")
sid = get_sid_from_browser_storage(origin)
if sid:
return sid
if origin:
candidates.append(f"{origin}/")
candidates.extend(
[
"https://webofscience.clarivate.cn/",
"https://www.webofscience.com/",
"https://www.webofknowledge.com/",
"http://www.webofknowledge.com/",
]
)
seen = set()
headers = {
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126 Safari/537.36"
),
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.7,en;q=0.3",
}
for candidate in candidates:
if candidate in seen:
continue
seen.add(candidate)
try:
request = urllib.request.Request(candidate, headers=headers)
response = urllib.request.urlopen(request, timeout=30)
sid = extract_sid_from_url(response.geturl())
response.close()
except (urllib.error.URLError, ValueError):
continue
if sid:
return sid
return ""
def request_range(ctx: dict[str, str], sid: str, start: int, end: int, retries: int = 3) -> bytes:
payload = {
"parentQid": ctx["parent_qid"],
"sortBy": "relevance",
"displayTimesCited": "true",
"displayCitedRefs": "true",
"product": "UA",
"colName": "WOS",
"displayUsageInfo": "true",
"fileOpt": "othersoftware",
"action": "saveToFieldTagged",
"markFrom": str(start),
"markTo": str(end),
"view": "summary",
"isRefQuery": "false",
"locale": "en_US",
"fieldList": FIELDS,
}
body = json.dumps(payload).encode("utf-8")
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; rv:109.0) Gecko/20100101 Firefox/119.0",
"Accept": "application/json, text/plain, */*",
"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.7,en;q=0.3",
"X-1P-WOS-SID": sid,
"Content-Type": "application/json",
"Origin": ctx["origin"],
"DNT": "1",
"Connection": "keep-alive",
"Referer": ctx["referer"],
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
}
url = f"{ctx['origin']}/api/wosnx/indic/export/saveToFile"
for attempt in range(1, retries + 1):
request = urllib.request.Request(url, data=body, headers=headers, method="POST")
try:
with urllib.request.urlopen(request, timeout=90) as response:
return response.read()
except urllib.error.HTTPError as exc:
if exc.code == 504 and attempt < retries:
time.sleep(2 * attempt)
continue
detail = exc.read().decode("utf-8", errors="replace")
raise RuntimeError(f"HTTP {exc.code} for {start}-{end}: {detail[:500]}") from exc
except urllib.error.URLError as exc:
if attempt < retries:
time.sleep(2 * attempt)
continue
raise RuntimeError(f"Request failed for {start}-{end}: {exc}") from exc
raise RuntimeError(f"Request failed for {start}-{end}")
def export_range(ctx: dict[str, str], sid: str, output_dir: Path, start: int, end: int, overwrite: bool) -> Path:
output_path = output_dir / f"{start:06d}_{end:06d}.txt"
if output_path.exists() and output_path.stat().st_size > 0 and not overwrite:
print(f"Skipping existing file: {output_path}")
return output_path
data = request_range(ctx, sid, start, end)
if not data:
raise RuntimeError(f"Empty response for {start}-{end}")
output_path.write_bytes(data)
print(f"Saved: {output_path}")
return output_path
def ranges(total: int) -> list[tuple[int, int]]:
return [(start, min(start + 999, total)) for start in range(1, total + 1, 1000)]
def sanitize_folder_name(value: str) -> str:
value = re.sub(r"[<>:\"/\\|?*\x00-\x1f]+", "_", value.strip())
value = re.sub(r"\s+", "_", value)
return value.strip(" ._")[:120]
def default_output_dir(parent_qid: str) -> Path:
stamp = time.strftime("%Y%m%d_%H%M%S")
short_qid = sanitize_folder_name(parent_qid[:8] or "wos")
return Path(__file__).resolve().parent / "exports" / f"wos_{stamp}_{short_qid}"
def main() -> int:
parser = argparse.ArgumentParser(description="Export Web of Science results in 1000-record chunks.")
parser.add_argument("summary", help="Full Web of Science summary URL, or the old single QID.")
parser.add_argument("total", type=int, help="Total number of records to export.")
parser.add_argument("-o", "--output-dir", help="Output directory. Default: the parsed parent QID.")
parser.add_argument("--sid", default="", help="Optional SID override for single-QID URLs.")
parser.add_argument("--workers", type=int, default=6, help="Concurrent export requests. Default: 6.")
parser.add_argument("--overwrite", action="store_true", help="Overwrite existing split files.")
args = parser.parse_args()
ctx = parse_summary_input(args.summary)
sid = args.sid.strip() or ctx["sid"] or get_sid(ctx["origin"])
if not sid:
raise SystemExit(
"SID not found. Single-QID mode requires the tool to auto-fetch a SID from Web of Knowledge, "
"but this environment did not return one. Paste a URL like "
"/wos/woscc/summary/<QID>/<SID>/relevance/1 or pass --sid manually."
)
output_dir = Path(args.output_dir).resolve() if args.output_dir else default_output_dir(ctx["parent_qid"]).resolve()
output_dir.mkdir(parents=True, exist_ok=True)
print(f"Origin: {ctx['origin']}")
print(f"Parent QID: {ctx['parent_qid']}")
print(f"SID: {sid}")
print(f"Output directory: {output_dir}")
chunks = ranges(args.total)
with ThreadPoolExecutor(max_workers=args.workers) as executor:
futures = [
executor.submit(export_range, ctx, sid, output_dir, start, end, args.overwrite)
for start, end in chunks
]
for future in as_completed(futures):
future.result()
print("Export complete.")
return 0
if __name__ == "__main__":
raise SystemExit(main())