[gnome-maps/wip/mlundblad/place-thumbnails: 1/2] WIP: Add module to query the Wikipedia API
- From: Marcus Lundblad <mlundblad src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-maps/wip/mlundblad/place-thumbnails: 1/2] WIP: Add module to query the Wikipedia API
- Date: Mon, 27 Feb 2017 22:24:01 +0000 (UTC)
commit 7379db7c05456d3c94859b6e5e9dd4216503b964
Author: Marcus Lundblad <ml update uu se>
Date: Mon Feb 27 23:22:06 2017 +0100
WIP: Add module to query the Wikipedia API
Functionallity to query for article thumbnails.
src/org.gnome.Maps.src.gresource.xml | 1 +
src/wikipedia.js | 74 ++++++++++++++++++++++++++++++++++
2 files changed, 75 insertions(+), 0 deletions(-)
---
diff --git a/src/org.gnome.Maps.src.gresource.xml b/src/org.gnome.Maps.src.gresource.xml
index f91a0bd..0951e9b 100644
--- a/src/org.gnome.Maps.src.gresource.xml
+++ b/src/org.gnome.Maps.src.gresource.xml
@@ -96,6 +96,7 @@
<file>userLocationBubble.js</file>
<file>userLocationMarker.js</file>
<file>utils.js</file>
+ <file>wikipedia.js</file>
<file>xmldom/dom.js</file>
<file>xmldom/domparser.js</file>
<file>xmldom/sax.js</file>
diff --git a/src/wikipedia.js b/src/wikipedia.js
new file mode 100644
index 0000000..ab328a2
--- /dev/null
+++ b/src/wikipedia.js
@@ -0,0 +1,74 @@
+/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
+/* vim: set et ts=4 sw=4: */
+/*
+ * Copyright (c) 2017 Marcus Lundblad
+ *
+ * GNOME Maps is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * GNOME Maps is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with GNOME Maps; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Marcus Lundblad <ml update uu se>
+ */
+
+const GLib = imports.gi.GLib;
+const Soup = imports.gi.Soup;
+
+const Format = imports.format;
+const Utils = imports.utils;
+
+let _soupSession = null;
+function _getSoupSession() {
+ if (_soupSession === null) {
+ _soupSession = new Soup.Session ();
+ }
+
+ return _soupSession;
+}
+
+/*
+ * Try to fetch the thumbnail given an article title and thumbnail size
+ * Calls callback with the Gdk.PixBuf of the icon when successful, otherwise
+ * undefined
+ */
+function fetchArticleThumbnail(article, size, callback) {
+ // TODO: consolidate this splitting and the one in PlaceBubble in here
+ let lang = article.split(':')[0];
+ let title = GLib.markup_escape_text(article.split(':').splice(1).join(':'), -1);
+ let uri = Format.vprintf('https://%s.wikipedia.org/w/api.php', [ lang ]);
+ let msg = Soup.form_request_new_from_hash('GET', uri, { action: 'query',
+ titles: title,
+ prop: 'pageimages',
+ format: 'json',
+ pithumbsize: size + ''});
+ let session = _getSoupSession();
+
+ session.queue_message(msg, (function (session, msg) {
+ if (msg.status_code !== Soup.KnownStatusCode.OK) {
+ log("Failed to request thumbnail: " + msg.reason_phrase);
+ callback(null);
+ return;
+ }
+
+ let response = JSON.parse(msg.response_body.data);
+
+ Utils.debug('Thumbnail response: ' + JSON.stringify(response, '', 2));
+ let pages = response.query.pages;
+
+ if (pages) {
+ for (let page in pages) {
+ Utils.debug('thumbnail: ' + pages[page].thumbnail.source);
+ }
+ } else {
+ Utils.debug('No thumbnail found');
+ }
+ }).bind(this));
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]