Libraryless. Click here for Pure Java version (6720L/40K).
1 | sclass WikiData { |
2 | S language = "en"; |
3 | S site = "https://wikidata.org"; |
4 | S apiPath = "/w/api.php"; |
5 | |
6 | transient FileBasedStringMap cache; |
7 | transient double cacheExpirationDays = 7; |
8 | |
9 | *() { init(); } |
10 | *(S *language) { init(); } |
11 | |
12 | void init { |
13 | cache = new FileBasedStringMap(javaxCachesDir(hostNameFromURL(site) + "/" + assertIdentifier(language))); |
14 | } |
15 | |
16 | class BasicNode { |
17 | S id, label; |
18 | LS aliases; |
19 | S description; |
20 | |
21 | S url() { |
22 | ret site + "/entity/" + id; |
23 | } |
24 | |
25 | toString { |
26 | ret formatColonProperties(paramsToPairs(+language, +id, +label, +aliases, +description, +url())); |
27 | } |
28 | } |
29 | |
30 | L<Map> search(S q) { |
31 | ret (L) rmapGet("search", (Map) loadJSONPageWithParams(site + apiPath, action := "wbsearchentities", |
32 | format := "json", |
33 | +language, |
34 | type := "item", |
35 | continue := 0, |
36 | search := q)); |
37 | } |
38 | |
39 | // ids are separated with "|" |
40 | Map getItems(S ids) { |
41 | ret (Map) rmapGet("entities", (Map) loadJSONPageWithParams(site + apiPath, |
42 | action := "wbgetentities", |
43 | format := "json", |
44 | languages := language, |
45 | +ids)); |
46 | } |
47 | |
48 | // itemID = e.g. "Q388" |
49 | Map get(S itemID) { |
50 | if (cache == null) |
51 | ret get_uncached(itemID); |
52 | else { |
53 | S key = itemID; |
54 | File f = cache.fileForKey(key); |
55 | if (exists(f) && fileAgeInDays(f) < cacheExpirationDays) |
56 | ret jsonDecodeMap(cache.get(key)); |
57 | |
58 | var result = get_uncached(itemID); |
59 | cache.put(key, jsonEncode(result)); |
60 | ret result; |
61 | } |
62 | } |
63 | |
64 | Map get_uncached(S itemID) { |
65 | ret (Map) rmapGet(itemID, getItems(itemID)); |
66 | } |
67 | |
68 | BasicNode basicNode(S itemID) { |
69 | var data = get(itemID); |
70 | if (data == null) null; |
71 | new BasicNode node; |
72 | node.id = (S) rgenget id(data); |
73 | node.label = (S) rgenget value(rgenget(language, rgenget labels(data))); |
74 | node.aliases = collectMapValues_gen value(rgenget(language, rgenget aliases(data))); |
75 | node.description = (S) rgenget value(rgenget(language, rgenget descriptions(data))); |
76 | ret node; |
77 | } |
78 | } |
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1032555 |
Snippet name: | WikiData |
Eternal ID of this version: | #1032555/26 |
Text MD5: | 972b25a474a96da530d04f7ba76d1055 |
Transpilation MD5: | 0cccdc373a583738c577315bbd5434e7 |
Author: | stefan |
Category: | javax / networking |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-09-24 15:09:22 |
Source code size: | 2108 bytes / 78 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 213 / 431 |
Version history: | 25 change(s) |
Referenced in: | [show references] |