[Yt-svn] commit/yt: MatthewTurk: Adding imgur upload

Bitbucket commits-noreply at bitbucket.org
Wed Apr 13 12:42:48 PDT 2011


1 new changeset in yt:

http://bitbucket.org/yt_analysis/yt/changeset/4228ba5d527f/
changeset:   r4121:4228ba5d527f
branch:      yt
user:        MatthewTurk
date:        2011-04-13 21:42:28
summary:     Adding imgur upload
affected #:  2 files (1.9 KB)

--- a/yt/gui/reason/extdirect_repl.py	Wed Apr 13 13:33:54 2011 -0400
+++ b/yt/gui/reason/extdirect_repl.py	Wed Apr 13 15:42:28 2011 -0400
@@ -31,6 +31,9 @@
 import uuid
 import numpy as na
 import time
+import urllib
+import urllib2
+import pprint
 
 from yt.funcs import *
 from yt.utilities.logger import ytLogger, ufstring
@@ -252,6 +255,24 @@
         site = "http://paste.enzotools.org/show/%s" % ret
         return {'status': 'SUCCESS', 'site': site}
 
+    _api_key = 'f62d550859558f28c4c214136bc797c7'
+    def upload_image(self, image_data):
+        if not image_data.startswith("data:"): return {'uploaded':False}
+        prefix = "data:image/png;base64,"
+        image_data = image_data[len(prefix):]
+        parameters = {'key':self._api_key, 'image':image_data, type:'base64'}
+        data = urllib.urlencode(parameters)
+        req = urllib2.Request('http://api.imgur.com/2/upload.json', data)
+        try:
+            response = urllib2.urlopen(req).read()
+        except urllib2.HTTPError as e:
+            print "ERROR", e
+            return {'uploaded':False}
+        rv = json.loads(response)
+        rv['uploaded'] = True
+        pprint.pprint(rv)
+        return rv
+
     @lockit
     def _session_py(self):
         cs = cStringIO.StringIO()


--- a/yt/gui/reason/html/js/widget_plotwindow.js	Wed Apr 13 13:33:54 2011 -0400
+++ b/yt/gui/reason/html/js/widget_plotwindow.js	Wed Apr 13 15:42:28 2011 -0400
@@ -201,6 +201,30 @@
                             {code:python_varname + '.zoom(0.1)'},
                         cell_finished); 
                     }
+                },{
+                    xtype: 'button',
+                    text: 'Upload Image',
+                    x: 10,
+                    y: 285,
+                    width: 80,
+                    handler: function(b,e) {
+                        img_data = image_dom.src;
+                        yt_rpc.ExtDirectREPL.upload_image(
+                            {image_data:img_data},
+                        function(rv) {
+                            var alert_text;
+                            if(rv['uploaded'] == false) {
+                                alert_text = "Failure uploading image!";
+                            } else {
+                                alert_text = "Uploaded to " +
+                                        rv['upload']['links']['imgur_page'];
+                            }
+                            Ext.Msg.alert('imgur.com', alert_text);
+                            var record = new logging_store.recordType(
+                                {record: alert_text });
+                            logging_store.add(record, number_log_records++);
+                        }); 
+                    }
                 }
             ]
         }
@@ -211,6 +235,7 @@
     this.panel = viewport.get("center-panel").get("pw_" + python_varname);
     this.panel.doLayout();
     this.image_panel = this.panel.get("image_panel_"+python_varname);
+    var image_dom = this.image_panel.el.dom;
 
     this.accept_results = function(payload) {
         this.image_panel.el.dom.src = "data:image/png;base64," + payload['image_data'];

Repository URL: https://bitbucket.org/yt_analysis/yt/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.



More information about the yt-svn mailing list