Created
May 20, 2010 22:10
-
-
Save koenbollen/408179 to your computer and use it in GitHub Desktop.
Commandline tool for uploading images to imgur.com.
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/env python | |
# Koen Bollen <meneer koenbollen nl> | |
# 2010 GPL | |
# | |
import sys | |
import os | |
import optparse | |
import base64 | |
import urllib2 | |
from urllib import urlencode | |
try: | |
import json | |
except ImportError: | |
try: | |
import simplejson as json | |
except ImportError: | |
print >>sys.stderr, "error: no json module found!" | |
sys.exit( 1 ) | |
def main(): | |
parser = optparse.OptionParser( | |
"usage: %prog [options] IMAGE...\n\n Upload image(s) to imgur.com", | |
version="%prog 1.0\nKoen Bollen <meneer koenbollen nl>\n2010 GPL" | |
) | |
parser.add_option( "-k", "--key", | |
help="the imgur.com API key.", metavar="KEY" | |
) | |
parser.add_option( "-c", "--cfg", default="~/.imgur.cfg", | |
help="load a different configfile.", metavar="FILE" | |
) | |
parser.add_option( "-q", "--quiet", default=False, action="store_true", | |
help="don't output anything" | |
) | |
parser.add_option( "-a", "--hash", default=False, action="store_true", | |
help="output image hash only" | |
) | |
parser.add_option( "-d", "--delete", | |
help="delete a image by delete hash", metavar="DHASH" | |
) | |
parser.add_option( "-n", "--nolog", default=True, action="store_false", | |
dest="log", help="don't log every upload to ~/.imgur.log" | |
) | |
options, args = parser.parse_args() | |
try: | |
fp = open( os.path.expanduser( options.cfg ) ) | |
except IOError, e: | |
print >>sys.stderr, "error: %s: %s" % ( e.filename, e.strerror ) | |
sys.exit( 1 ) | |
for line in fp: | |
line = line.strip() | |
if not line or line[0] in ("#", ";") or "=" not in line: | |
continue | |
key, value = line.split( "=", 1 ) | |
key = key.strip().lower() | |
if key in ("key",): | |
current = getattr( options, key ) | |
if current == parser.defaults[key]: | |
setattr( options, key, value.strip() ) | |
fp.close() | |
if not options.key: | |
print >>sys.stderr, "error: missing api key!" | |
sys.exit( 1 ) | |
if options.delete: | |
response = urllib2.urlopen( | |
"http://imgur.com/api/delete/%s.json" % options.delete | |
) | |
reply = json.load( response ) | |
response.close() | |
if "error" in reply: | |
print >>sys.stderr, "error:", reply['error']['error_msg'] | |
else: | |
print reply['rsp']['message'] | |
sys.exit( 0 ) | |
url = "http://imgur.com/api/upload.json" | |
if len(args) < 1: | |
args.append( "-" ) | |
for file in args: | |
try: | |
if file == "-": | |
fp = sys.stdin | |
else: | |
fp = open( file, "rb" ) | |
except IOError, e: | |
print >>sys.stderr, "warning: %s: %s" % ( e.filename, e.strerror ) | |
print >>sys.stderr, "skipping", file | |
continue | |
data = base64.b64encode( fp.read() ) | |
fp.close() | |
values = { | |
'key': options.key, | |
'image': data | |
} | |
req = urllib2.Request( url, urlencode( values ) ) | |
try: | |
response = urllib2.urlopen( req ) | |
except urllib2.URLError, e: | |
if hasattr(e, 'reason'): | |
print >>sys.stderr, 'error:', e.reason | |
elif hasattr(e, 'code'): | |
print >>sys.stderr, 'error:', e.code | |
sys.exit( 1 ) | |
try: | |
reply = json.load( response ) | |
reply = reply['rsp'] | |
except (ValueError, KeyError), e: | |
print >>sys.stderr, "error: invalid reply: ", e | |
sys.exit( 1 ) | |
response.close() | |
if reply['stat'] != "ok": | |
print >>sys.stderr, "error: failed:", reply['error_msg'] | |
sys.exit( 1 ) | |
image = reply['image'] | |
if options.log: | |
try: | |
fp = open( os.path.expanduser( "~/.imgur.log" ), "a" ) | |
except IOError, e: | |
print >>sys.stderr, "warning: %s: %s" % ( e.filename, e.strerror ) | |
else: | |
msg = "%s %s %s %s\n" % ( | |
os.path.basename( file ), image['image_hash'], | |
image['delete_hash'], image['original_image'] | |
) | |
fp.write( msg ) | |
fp.close() | |
if not options.quiet: | |
if options.hash: | |
print image['image_hash'] | |
else: | |
print image['original_image'] | |
if __name__ == "__main__": | |
main() | |
# vim: expandtab shiftwidth=4 softtabstop=4 textwidth=79: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment