File size: 2,533 Bytes
a4054b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# MIT License, Bryan Cheng, 2026. Fetch metadata for references from arXiv (by id) and Crossref (by title) -> JSON.
import json, re, sys, time, urllib.parse, urllib.request
import xml.etree.ElementTree as ET
UA = {"User-Agent": "research-refcheck/1.0"}
def get(u):
    return urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=60).read().decode()
refs = json.load(open(sys.argv[1]))
out = {}
ns = {"a": "http://www.w3.org/2005/Atom"}
ax = {k: v["arxiv"] for k, v in refs.items() if v.get("arxiv")}
ids = list(ax.values())
for s in range(0, len(ids), 15):
    x = ET.fromstring(get("https://export.arxiv.org/api/query?" + urllib.parse.urlencode({"id_list": ",".join(ids[s:s+15]), "max_results": 20})))
    for e in x.findall("a:entry", ns):
        aid = re.sub(r"v\d+$", "", e.findtext("a:id", "", ns).split("/abs/")[-1])
        k = [k for k, v in ax.items() if v == aid]
        if k:
            out[k[0]] = {"source": "arXiv", "id": aid, "url": "https://arxiv.org/abs/" + aid,
                         "title": " ".join(e.findtext("a:title", "", ns).split()),
                         "authors": [a.findtext("a:name", "", ns) for a in e.findall("a:author", ns)],
                         "published": e.findtext("a:published", "", ns)[:10],
                         "abstract": " ".join(e.findtext("a:summary", "", ns).split())[:600]}
    time.sleep(3)
for k, v in refs.items():
    if k in out or not v.get("title"):
        continue
    try:
        d = json.loads(get("https://api.crossref.org/works?" + urllib.parse.urlencode({"query.bibliographic": v["title"], "rows": 1})))
        it = d["message"]["items"][0]
        out[k] = {"source": "Crossref", "doi": it.get("DOI"), "url": "https://doi.org/" + it.get("DOI", ""),
                  "title": (it.get("title") or [""])[0], "container": (it.get("container-title") or [""])[0],
                  "authors": [f"{a.get('given','')} {a.get('family','')}".strip() for a in it.get("author", [])],
                  "year": (it.get("issued", {}).get("date-parts") or [[None]])[0][0]}
    except Exception as e:
        out[k] = {"source": "none", "error": str(e)[:100]}
    time.sleep(1)
for k, v in refs.items():
    o = out.get(k, {})
    want, got = v.get("title", "").lower(), o.get("title", "").lower()
    o["title_match"] = bool(got) and (want[:40] in got or got[:40] in want)
    out[k] = o
json.dump(out, open(sys.argv[2], "w"), indent=1)
print({k: (o.get("source"), o.get("title_match")) for k, o in sorted(out.items(), key=lambda x: int(x[0]))})