Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created February 19, 2012 13:07
Show Gist options
  • Save zeffii/1863726 to your computer and use it in GitHub Desktop.
Save zeffii/1863726 to your computer and use it in GitHub Desktop.
imgur upload python 3 without external modules
from xml.dom import minidom
from urllib.parse import urlencode
from urllib.request import urlopen
import base64
import os
def upload_image(path_to_image):
url = 'http://api.imgur.com/2/upload.xml'
apikey = '-------- your API key ---------'
identifiers = 'imgur_page|original|delete_page|large_thumbnail'.split('|')
def xml_reader(xml_to_read):
doc_ob = minidom.parseString(xml_to_read)
for identifier in identifiers:
co = doc_ob.getElementsByTagName(identifier)
content = co[0].lastChild.toxml().strip()
print(content + ' ('+identifier+')')
def encoded_image():
source = open(path_to_image, 'rb')
return base64.b64encode(source.read())
def pack_data():
parameters = { 'key': apikey, 'image': encoded_image()}
return urlencode(parameters).encode('utf-8')
data = pack_data()
print('encoded, sending...')
xml_to_parse = urlopen(url, data)
print('received response from server')
xml_reader(xml_to_parse.readall().decode())
image_name = '-------- your image name --------'
path_to_image = os.path.join(os.getcwd(), image_name)
upload_image(path_to_image)
@zeffii
Copy link
Author

zeffii commented Sep 4, 2012

encoded, sending... received response from server http://imgur.com/X1L5M (imgur_page) http://i.imgur.com/X1L5M.png (original) http://imgur.com/delete/ZZKi7NuypaKuEdd (delete_page) http://i.imgur.com/X1L5Ml.jpg (large_thumbnail)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment