Skip to content

Commit

Permalink
Switched to https calls for uploading and deleting. Cleaned up whites…
Browse files Browse the repository at this point in the history
…pace and some other issues from old code transition. Getting prepped for a little overhaul.
  • Loading branch information
mutaku committed May 30, 2013
1 parent 6cd446f commit da59220
Showing 1 changed file with 3 additions and 38 deletions.
41 changes: 3 additions & 38 deletions pyimgur.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,16 @@
anon_key = local_settings.ANNON_KEY

class UploadImage():
"""
Upload images to imgur
"""Upload images to imgur
returns either .error or .imageURL
TO DO:
POSSIBLE TO DO:
- add stats and image info functions
- allow for full JSON responses instead of having to manually parse XML
"""

# Not very verbose at the moment in terms of errors - will build on that later
def __init__(self,image="",dhash="",delete=False):

# setup some initial items and placeholders we will need
self.c = pycurl.Curl()
self.response = cStringIO.StringIO()
self.minidom = minidom
Expand All @@ -39,84 +33,55 @@ def __init__(self,image="",dhash="",delete=False):
self.error = []

if self.dhash and self.delete:
# if we have a hash and a delete trigger, lets try to wipe the image
self.wipe()

else:
# fire away an upload - we will return an imageURL dictionary with attributes or an error
self.upload()


def upload(self):
"Upload anonymously to imgur"

# setup the basic parameters
params = [
("key", anon_key),
("image", (self.c.FORM_FILE, self.image))
]

# setup the url and pipe in our key and image
self.c.setopt(self.c.URL, "http://api.imgur.com/2/upload.xml")
self.c.setopt(self.c.URL, "https://api.imgur.com/2/upload.xml")
self.c.setopt(self.c.HTTPPOST, params)

# we want to capture the output so lets set the write output to go to our cStringIO so we can parse it
self.c.setopt(self.c.WRITEFUNCTION, self.response.write)

try:
# run it
self.c.perform()
self.c.close()

except:
self.error.append("Problem uploading image.")

if not self.error:
# parse the xml
self.parseXML()


return self.message,self.imageURL,self.error


def wipe(self):
"Wipe an anonymouse image from imgur"

deleteURL = "http://api.imgur.com/2/delete/%s" % self.dhash

deleteURL = "https://api.imgur.com/2/delete/%s" % self.dhash
self.c.setopt(self.c.URL, deleteURL)

self.c.setopt(self.c.WRITEFUNCTION, self.response.write)

try:
self.c.perform()
self.c.close()

except:
self.error.append("Problem deleting image.")

if not self.error:
self.parseXML(delete=True)


def parseXML(self,delete=False):
"Parse the XML ouput from IMGUR and write to the imageURL dictionary"

try:
# parse the XML string into the dom
xml = self.minidom.parseString(self.response.getvalue())

if delete:
self.message = xml.getElementsByTagName("message")[0].firstChild.data

else:
# grab out some helpful/interesting data and setup in the imageURL dictionary
self.imageURL['url'] = xml.getElementsByTagName("original")[0].firstChild.data
self.imageURL['hash'] = xml.getElementsByTagName("hash")[0].firstChild.data
self.imageURL['deletehash'] = xml.getElementsByTagName("deletehash")[0].firstChild.data
self.imageURL['smallthumb'] = xml.getElementsByTagName("small_square")[0].firstChild.data
self.imageURL['bigthumb'] = xml.getElementsByTagName("large_thumbnail")[0].firstChild.data

except:
self.error.append("Problem parsing XML output.")

0 comments on commit da59220

Please sign in to comment.