51 lines
2.4 KiB
Python
Executable file
51 lines
2.4 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
"""Wikidata identity check — EN author -> life dates + OFFICIAL RU name.
|
|
|
|
Usage: tools/wd.py "Erich Neumann" [top]
|
|
|
|
Resolves the Q-entity, prints birth/death (P569/P570), EN/DE/RU labels, RU aliases
|
|
(P725-equivalent via aliases block), and the P50 works list (often sparse).
|
|
Primary value: (1) disambiguation via life dates, (2) the canonical RU label — the
|
|
single most reliable RU spelling for RSL/libgen queries.
|
|
"""
|
|
import json, sys, urllib.parse, urllib.request
|
|
|
|
UA = "research-tools/1.0 (jung booklist research; contact: dmitry@kokorin.org)"
|
|
|
|
def api(params):
|
|
url = "https://www.wikidata.org/w/api.php?" + urllib.parse.urlencode(params)
|
|
req = urllib.request.Request(url, headers={"User-Agent": UA})
|
|
with urllib.request.urlopen(req, timeout=40) as r:
|
|
return json.load(r)
|
|
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
sys.exit(__doc__)
|
|
name = " ".join(sys.argv[1:-1]) if len(sys.argv) > 2 and sys.argv[-1].isdigit() else " ".join(sys.argv[1:])
|
|
top = int(sys.argv[-1]) if len(sys.argv) > 2 and sys.argv[-1].isdigit() else 3
|
|
d = api({"action": "wbsearchentities", "search": name, "language": "en",
|
|
"limit": top, "format": "json"})
|
|
for r in d.get("search", []):
|
|
qid = r["id"]
|
|
label = r.get("label")
|
|
desc = (r.get("description") or "")[:100]
|
|
try:
|
|
ed = api({"action": "wbgetentities", "ids": qid, "format": "json"})
|
|
ent = ed["entities"][qid]
|
|
c = ent.get("claims", {})
|
|
bd = c.get("P569", [{}])[0].get("mainsnak", {}).get("datavalue", {}).get("value", {}).get("time", "?")
|
|
dd = c.get("P570", [{}])[0].get("mainsnak", {}).get("datavalue", {}).get("value", {}).get("time", "?")
|
|
labels = {k: v["value"] for k, v in ent.get("labels", {}).items() if k in ("en", "de", "ru")}
|
|
ru_al = [a["value"] for a in ent.get("aliases", {}).get("ru", [])]
|
|
works = [w["mainsnak"]["datavalue"]["value"].get("id") for w in c.get("P50", [])]
|
|
print(f"{qid} | {label} | {desc}")
|
|
print(f" born/died: {bd} / {dd}")
|
|
print(f" labels: {labels}")
|
|
if ru_al: print(f" RU aliases: {ru_al}")
|
|
if works: print(f" P50 works: {works[:15]}")
|
|
except Exception as e:
|
|
print(f"{qid} | {label} | (entity fetch failed: {e})")
|
|
print()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|