Last active
August 10, 2024 22:25
-
-
Save kstep/996278 to your computer and use it in GitHub Desktop.
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 | |
from poster.encode import multipart_encode | |
from poster.streaminghttp import register_openers | |
import urllib2 | |
import simplejson | |
import sys | |
try: | |
input_file = open(sys.argv[1], 'rb') | |
except IndexError: | |
input_file = sys.stdin | |
API_KEY = "your_imgur_apikey_here" | |
API_URL = 'http://api.imgur.com/2/upload.json' | |
register_openers() | |
datagen, headers = multipart_encode(dict(image=input_file, key=API_KEY)) | |
request = urllib2.Request(API_URL, datagen, headers) | |
try: | |
result = urllib2.urlopen(request).read() | |
except urllib2.HTTPError, e: | |
sys.stderr.write("Error uploading image: %s\n" % e.msg) | |
sys.exit(e.code) | |
result = simplejson.loads(result) | |
links = result['upload']['links'] | |
print "Original:", links['original'] | |
print "Page:", links['imgur_page'] | |
print "Delete:", links['delete_page'] | |
print "Small:", links['small_square'] | |
print "Large:", links['large_thumbnail'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment