Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

78
LINES

< > BotCompany Repo | #1032555 // WikiData

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (6720L/40K).

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<Map> 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;
  }
}

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: 212 / 429
Version history: 25 change(s)
Referenced in: [show references]