[gnome-maps/wip/mlundblad/wikidata: 2/3] WIP: wikipedia: Add function to fetch article from Wikidata




commit 3a3c6b4063082b062098afbbbe36953c35b62c08
Author: Marcus Lundblad <ml dfupdate se>
Date:   Tue Oct 4 23:23:49 2022 +0200

    WIP: wikipedia: Add function to fetch article from Wikidata

 src/wikipedia.js | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
---
diff --git a/src/wikipedia.js b/src/wikipedia.js
index 80ad3a7d..02d04117 100644
--- a/src/wikipedia.js
+++ b/src/wikipedia.js
@@ -155,6 +155,29 @@ export function fetchArticleInfo(wiki, size, metadataCb, thumbnailCb) {
     });
 }
 
+export function fetchArticleForWikidata(wikidata, callback) {
+    let uri = 'https://www.wikidata.org/w/api.php';
+    let encodedForm = Soup.form_encode_hash({ action: 'wbgetentities',
+                                              ids:    wikidata,
+                                              format: 'json' });
+    let msg = Soup.Message.new_from_encoded_form('GET', uri, encodedForm);
+    let session = _getSoupSession();
+
+    session.send_and_read_async(msg, GLib.PRIORIRY_DEFAULT, null,
+                                     (source, res) => {
+        if (msg.get_status() !== Soup.Status.OK) {
+            log("Failed to request Wikidata entities: " + msg.reason_phrase);
+            callback(null);
+            return;
+        }
+
+        let buffer = session.send_and_read_finish(res).get_data();
+        let response = JSON.parse(Utils.getBufferText(buffer));
+
+        Utils.debug('entities: ' + JSON.stringify(response, '', 2));
+    }
+}
+
 function _onMetadataFetched(wiki, page, size, metadataCb, thumbnailCb) {
     /* Try to get a thumbnail *before* following language links--the primary
        article probably has the best thumbnail image */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]