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




commit ecfb2441e66383ad7495f2aa714399206dcfa485
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 | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 59 insertions(+), 1 deletion(-)
---
diff --git a/src/wikipedia.js b/src/wikipedia.js
index 80ad3a7d..24aa52fe 100644
--- a/src/wikipedia.js
+++ b/src/wikipedia.js
@@ -155,6 +155,60 @@ export function fetchArticleInfo(wiki, size, metadataCb, thumbnailCb) {
     });
 }
 
+export function fetchArticleInfoForWikidata(wikidata, defaultArticle,
+                                            size, metadataCb,
+                                            thumbnailCb) {
+    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);
+            metadataCb(null, {});
+            thumbnailCb(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));
+        let labels = entities?.[wikidata]?.labels;
+
+        if (!labels) {
+            Utils.debug('No labels tag in response');
+            metadataCb(null, {});
+            thumbnailCb(null);
+        }
+
+        for (let language of _getLanguages()) {
+            if (labels[language]) {
+                let article = `${language}:${labels[language].value}`;
+
+                fetchArticleInfo(article, size, metadataCb, thumbnailCb);
+                return;
+            }
+        }
+
+        // if no article reference matches a preferred language
+        if (defaultArticle) {
+            // if there's a default article from the "wikipedia" tag, use it
+            fetchArticleInfo(defaultArticle, size, metadataCb, thumbnailCb);
+        } else {
+            // otherwise use the first listed reference as fallback
+            let language = labels[0].language;
+            let article = `${language}:${labels[language].value}`;
+
+            fetchArticleInfo(article, size, metadataCb, thumbnailCb);
+        }
+    });
+}
+
 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 */
@@ -218,7 +272,7 @@ function _fetchThumbnailImage(wiki, size, source, callback) {
    the original article should be used. */
 function _findLanguageLink(wiki, page) {
     let originalLang = getLanguage(wiki);
-    let languages = GLib.get_language_names().map((lang) => lang.split(/[\._\-]/)[0]);
+    let languages = _getLanguages();
 
     if (!languages.includes(originalLang)) {
         let langlinks = {};
@@ -233,3 +287,7 @@ function _findLanguageLink(wiki, page) {
         }
     }
 }
+
+function _getLanguages() {
+    return GLib.get_language_names().map((lang) => lang.split(/[\._\-]/)[0]);
+}


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