Created
October 29, 2015 11:43
-
-
Save eyalzek/386a9bf0af4a37f97fbf to your computer and use it in GitHub Desktop.
Upload image to imgur using python (credit to lxskllr from reddit)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import pycurl | |
import xml.dom.minidom | |
import StringIO | |
import sys | |
import gtk | |
import os | |
import locale | |
import gettext | |
from subprocess import call | |
APP = "Imgur Uploader" | |
DIR = "locale" | |
locale.setlocale(locale.LC_ALL, '') | |
gettext.bindtextdomain(APP, DIR) | |
gettext.textdomain(APP) | |
_ = gettext.gettext | |
##STRINGS | |
notimg = _("is not an image.") | |
notallowed = _("is not an allowed f type. Skipping.") | |
uploading = _("Uploading") | |
oneimage = _("1 image has been uploaded.") | |
multimages = _("images have been uploaded.") | |
uploadfailed = _("Unable to upload") | |
class Uploadr: | |
def __init__(self): | |
self.allowedTypes = (".jpeg", ".jpg", ".gif", ".png", ".apng", ".tiff", ".bmp", ".pdf", ".xcf") | |
self.images = [] | |
self.urls = [] | |
self.broadcasts = [] | |
def verify(self, args): | |
if len(args) == 1: | |
return | |
else: | |
for f in args: | |
if f == args[0] or f == "": | |
continue | |
if os.path.splitext(f)[-1] not in self.allowedTypes: | |
self.broadcasts.append("%s %s %s." % (self.type, notallowed, f)) | |
else: | |
self.images.append(f) | |
for f in self.images: | |
self.upload(f) | |
self.setClipBoard() | |
self.broadcast(self.broadcasts) | |
def broadcast(self, l): | |
try: | |
st = "\n".join(l) | |
call(["zenity", "--info", "--text", st]) | |
except: | |
for line in l: | |
print line | |
def upload(self, f): | |
c = pycurl.Curl() | |
values = [ | |
("key", "e85c0044b9222bc9a2813679a452f54f"), | |
("image", (c.FORM_FILE, f)) | |
] | |
buf = StringIO.StringIO() | |
c.setopt(c.URL, "http://imgur.com/api/upload.xml") | |
c.setopt(c.HTTPPOST, values) | |
c.setopt(c.WRITEFUNCTION, buf.write) | |
if c.perform(): | |
self.broadcasts.append("%s %s." % (uploadfailed, f)) | |
c.close() | |
return | |
self.result = buf.getvalue() | |
c.close() | |
doc = xml.dom.minidom.parseString(self.result) | |
self.urls.append(doc.getElementsByTagName("original_image")[0].childNodes[0].nodeValue) | |
def setClipBoard(self): | |
c = gtk.Clipboard() | |
c.set_text("\n".join(self.urls)) | |
c.store() | |
if len(self.urls) == 1: | |
self.broadcasts.append(oneimage) | |
elif len(self.urls) != 0: | |
self.broadcasts.append("%d %s" % (len(self.urls), multimages)) | |
self.broadcasts.append("\n".join(self.urls)) | |
if __name__ == "__main__": | |
uploadr = Uploadr() | |
uploadr.verify(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment