[polari] utils: Add fpaste() helper method



commit eb10445a68819ecdc74f09c6290db3d9851f0c75
Author: Florian Müllner <fmuellner gnome org>
Date:   Mon Jul 29 00:30:00 2013 +0200

    utils: Add fpaste() helper method

 src/utils.js |   72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 71 insertions(+), 1 deletions(-)
---
diff --git a/src/utils.js b/src/utils.js
index b54d4fc..465c723 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -15,14 +15,19 @@
  * with Polari; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  *
- * Author: Cosimo Cecchi <cosimoc redhat com>
+ * Authors: Cosimo Cecchi <cosimoc redhat com>
+ *          Florian Müllner <fmuellner gnome org>
  *
  */
 
 const GLib = imports.gi.GLib;
+const Gio = imports.gi.Gio;
+const Soup = imports.gi.Soup;
 
 const Signals = imports.signals;
 
+const FPASTE_BASEURL = 'http://paste.fedoraproject.org/'
+
 let debugInit = false;
 let debugEnabled = false;
 
@@ -45,3 +50,68 @@ function addJSSignalMethods(proto) {
     proto.emitJS = Signals._emit;
     proto.disconnectAllJS = Signals._disconnectAll;
 }
+
+function fpaste(text, user, callback) {
+    let getUrl = function(session, id) {
+        let longUrl = FPASTE_BASEURL + id;
+        session.queue_message(Soup.Message.new('POST', longUrl + '/json'),
+            function(session, message) {
+                if (message.status_code != Soup.KnownStatusCode.OK) {
+                    callback(null);
+                    return;
+                }
+
+                // workaround: the response contains the pasted data
+                // unescaped (e.g. newlines), which is not legal json;
+                // just grab the property we're interested in
+                let lines = message.response_body.data.split('\n');
+                let shortUrl = null;
+                for (let i = 0; i < lines.length; i++) {
+                    if (lines[i].indexOf('short_url') > -1) {
+                        shortUrl = lines[i];
+                        break;
+                    }
+                }
+
+                let info = {};
+                try {
+                    if (shortUrl)
+                        info = JSON.parse('{ %s }'.format(shortUrl));
+                } catch(e) {
+                    log(e.message);
+                }
+                if (info.short_url)
+                    callback(info.short_url);
+                else
+                    callback(longUrl);
+            });
+    };
+    let params = {
+        paste_data: text,
+        paste_lang: 'text',
+        paste_user: user,
+        api_submit: '1',
+        mode: 'json'
+    };
+
+    let session = new Soup.Session();
+    let message = Soup.form_request_new_from_hash('POST', FPASTE_BASEURL, params);
+    session.queue_message(message,
+        function(session, message) {
+            if (message.status_code != Soup.KnownStatusCode.OK) {
+                callback(null);
+                return;
+            }
+
+            let info = {};
+            try {
+                info = JSON.parse(message.response_body.data);
+            } catch(e) {
+                log(e.message);
+            }
+            if (info.result && info.result.id)
+                getUrl(session, info.result.id);
+            else
+                callback(null);
+        });
+}


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