#!/usr/bin/env python3 """tools/rulabel.py — каноническое кириллическое имя автора по Wikidata (ru label + ru aliases). Идея (2026-09-24, предложил пользователь): authority-записи (VIAF/ISNI/GND) хранят имя во всех транскрипциях — но с этой машины VIAF (403 + JS-app), ISNI (403), GND (API mёртв, services.dnb.de → 404), BnF SRU (403) НЕДОСТУПНЫ. Wikidata-ru label/aliases доступны свободно и для известных людей = тот же канонический вариант (обычно взят из русскоязычных sources). Использование: rulabel.py Q44847 # один QID rulabel.py Q1|Q2|Q3 # батч rulabel.py --find "Scholem, Gershom" # поиск (ОСТОРОЖНО: top-5, омонимы!) Выход: \t\t\t """ import json, sys, urllib.parse, urllib.request, time UA = "jung-research/1.0 (research script; 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}) for attempt in range(4): try: return json.load(urllib.request.urlopen(req, timeout=45)) except Exception as e: if attempt == 3: raise time.sleep(15 * (attempt + 1)) def show(qid, e): lab_en = e.get("labels", {}).get("en", {}).get("value", "") lab_ru = e.get("labels", {}).get("ru", {}).get("value") al_ru = [a["value"] for a in e.get("aliases", {}).get("ru", [])] print(f"{qid}\t{lab_en}\t{lab_ru or '—'}\t{' | '.join(al_ru) or '—'}") def main(): args = sys.argv[1:] if not args: sys.exit(__doc__) if args[0] == "--find": query = args[1] d = api({"action": "wbsearchentities", "search": query, "language": "en", "type": "item", "limit": 5, "format": "json"}) for r in d.get("search", []): print(f" {r['id']}\t{r.get('label','?')}\t{r.get('description','')[:70]}") qids = [r["id"] for r in d.get("search", [])] else: qids = args[0].split("|") if not qids: return d = api({"action": "wbgetentities", "ids": "|".join(qids), "props": "labels|aliases", "languages": "en|ru", "format": "json"}) for qid in qids: e = d.get("entities", {}).get(qid) if e: show(qid, e) else: print(f"{qid}\tMISSING\t\t") time.sleep(2) if __name__ == "__main__": main()