sclass WikiData { S language = "en"; S site = "https://wikidata.org"; S apiPath = "/w/api.php"; transient FileBasedStringMap cache; transient double cacheExpirationDays = 7; *() { init(); } *(S *language) { init(); } void init { cache = new FileBasedStringMap(javaxCachesDir(hostNameFromURL(site) + "/" + assertIdentifier(language))); } class BasicNode { S id, label; LS aliases; S description; S url() { ret site + "/entity/" + id; } toString { ret formatColonProperties(paramsToPairs(+language, +id, +label, +aliases, +description, +url())); } } L search(S q) { ret (L) rmapGet("search", (Map) loadJSONPageWithParams(site + apiPath, action := "wbsearchentities", format := "json", +language, type := "item", continue := 0, search := q)); } // ids are separated with "|" Map getItems(S ids) { ret (Map) rmapGet("entities", (Map) loadJSONPageWithParams(site + apiPath, action := "wbgetentities", format := "json", languages := language, +ids)); } // itemID = e.g. "Q388" Map get(S itemID) { if (cache == null) ret get_uncached(itemID); else { S key = itemID; File f = cache.fileForKey(key); if (exists(f) && fileAgeInDays(f) < cacheExpirationDays) ret jsonDecodeMap(cache.get(key)); var result = get_uncached(itemID); cache.put(key, jsonEncode(result)); ret result; } } Map get_uncached(S itemID) { ret (Map) rmapGet(itemID, getItems(itemID)); } BasicNode basicNode(S itemID) { var data = get(itemID); if (data == null) null; new BasicNode node; node.id = (S) rgenget id(data); node.label = (S) rgenget value(rgenget(language, rgenget labels(data))); node.aliases = collectMapValues_gen value(rgenget(language, rgenget aliases(data))); node.description = (S) rgenget value(rgenget(language, rgenget descriptions(data))); ret node; } }