[gnome-maps/wip/mlundblad/place-thumbnails: 1/2] WIP: Add module to query the Wikipedia API



commit cacc4ff072f186a20bf3be3c2e4db9d22ee1c36b
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                     |  118 ++++++++++++++++++++++++++++++++++
 2 files changed, 119 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..d5c24f1
--- /dev/null
+++ b/src/wikipedia.js
@@ -0,0 +1,118 @@
+/* -*- 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 GdkPixbuf = imports.gi.GdkPixbuf;
+const Gio = imports.gi.Gio;
+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) {
+            /* we know there should be only one object instance in the "pages"
+             * object, but the API specifies the sub-object as the page ID,
+             * so we'll have to use this iteration approach here
+             */
+            for (let page in pages) {
+                let thumbnail = pages[page].thumbnail;
+
+                if (thumbnail) {
+                    let source = pages[page].thumbnail.source;
+                    Utils.debug('thumbnail: ' + source);
+                    _fetchThumbnailImage(source, callback);
+                } else {
+                    callback(null);
+                }
+                return;
+            }
+        } else {
+            Utils.debug('No thumbnail found');
+            callback(null);
+        }
+    });
+}
+
+function _fetchThumbnailImage(source, callback) {
+    let uri = new Soup.URI(source);
+    let msg = new Soup.Message({ method: 'GET', uri: uri });
+    let session = _getSoupSession();
+
+    session.queue_message(msg, function (session, msg) {
+        if (msg.status_code !== Soup.KnownStatusCode.OK) {
+            log("Failed to download thumbnail: " + msg.reason_phrase);
+            callback(null);
+            return;
+        }
+
+        let contents = msg.response_body_data;
+        let stream = Gio.MemoryInputStream.new_from_bytes(contents);
+
+        try {
+            let pixbuf = GdkPixbuf.Pixbuf.new_from_stream(stream, null);
+
+            callback(pixbuf);
+        } catch(e) {
+            log("Failed to load pixbuf: " + e);
+            callback(null);
+        }
+
+        stream.close(null);
+    });
+}


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