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

Bitbucket commits-noreply at bitbucket.org
Thu Aug 25 14:59:15 PDT 2011


1 new changeset in yt:

http://bitbucket.org/yt_analysis/yt/changeset/4ebba20c3f9b/
changeset:   4ebba20c3f9b
branch:      yt
user:        MatthewTurk
date:        2011-08-25 23:58:53
summary:     Adding imgur upload command "upload_image"
affected #:  1 file (1.4 KB)

--- a/yt/utilities/command_line.py	Thu Aug 25 17:28:47 2011 -0400
+++ b/yt/utilities/command_line.py	Thu Aug 25 17:58:53 2011 -0400
@@ -1409,7 +1409,44 @@
         req = urllib2.Request(uri, data)
         rv = urllib2.urlopen(req).read()
         print rv
-    
+
+    def do_upload_image(self, subcmd, opts, filename):
+        """
+        Upload an image to imgur.com.  Must be PNG.
+
+        ${cmd_usage} 
+        ${cmd_option_list}
+        """
+        if not filename.endswith(".png"):
+            print "File must be a PNG file!"
+            return 1
+        import base64, json, pprint
+        image_data = base64.b64encode(open(filename).read())
+        api_key = 'f62d550859558f28c4c214136bc797c7'
+        parameters = {'key':api_key, 'image':image_data, type:'base64',
+                      'caption': "",
+                      'title': "%s uploaded by yt" % filename}
+        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)
+        if 'upload' in rv and 'links' in rv['upload']:
+            print
+            print "Image successfully uploaded!  You can find it at:"
+            print "    %s" % (rv['upload']['links']['imgur_page'])
+            print
+            print "If you'd like to delete it, visit this page:"
+            print "    %s" % (rv['upload']['links']['delete_page'])
+            print
+        else:
+            print
+            print "Something has gone wrong!  Here is the server response:"
+            print
+            pprint.pprint(rv)
 
 def run_main():
     for co in ["--parallel", "--paste"]:

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