[gnome-maps/wip/mlundblad/wikidata: 7/10] WIP: wikipedia: Add function to fetch Wikidata tag for an article
- From: Marcus Lundblad <mlundblad src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-maps/wip/mlundblad/wikidata: 7/10] WIP: wikipedia: Add function to fetch Wikidata tag for an article
- Date: Tue, 18 Oct 2022 19:40:00 +0000 (UTC)
commit 28ec83a987cd4db8f7cdf1d1890c83aa6796ef58
Author: Marcus Lundblad <ml dfupdate se>
Date: Tue Oct 11 23:09:16 2022 +0200
WIP: wikipedia: Add function to fetch Wikidata tag for an article
src/wikipedia.js | 28 ++++++++++++++++++++++++++++
1 file changed, 28 insertions(+)
---
diff --git a/src/wikipedia.js b/src/wikipedia.js
index b5f17a15..5530d246 100644
--- a/src/wikipedia.js
+++ b/src/wikipedia.js
@@ -225,6 +225,34 @@ export function fetchArticleInfoForWikidata(wikidata, defaultArticle,
});
}
+export function fetchWikidataForArticle(wiki, cancellable, callback) {
+ let lang = getLanguage(wiki);
+ let title = getHtmlEntityEncodedArticle(wiki);
+ let uri = 'https://www.wikidata.org/w/api.php';
+ let encodedForm = Soup.form_encode_hash({ action: 'wbgetentities',
+ sites: lang + 'wiki',
+ titles: title,
+ 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, cancellable,
+ (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('response: ' + JSON.stringify(response, '', 2));
+ let id = Object.values(response.entities ?? [])?.[0]?.id;
+
+ callback(id);
+ });
+}
+
function _onWikidataFetched(wikidata, defaultArticle, response, size,
metadataCb, thumbnailCb) {
let sitelinks = response?.entities?.[wikidata]?.sitelinks;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]