Skip to content

Instantly share code, notes, and snippets.

@Garciat
Created December 28, 2010 17:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Garciat/757429 to your computer and use it in GitHub Desktop.
Save Garciat/757429 to your computer and use it in GitHub Desktop.
A simple cross-platform CLI Python imgur uploader.
#!/usr/bin/env xdg-open
# Make sure the Python script has +x
[Desktop Entry]
Version=1.0
Name=Imgur Uploader
Exec=/path/to/imgur_upload %U
Terminal=true
Type=Application
#! /usr/bin/env python
from sys import argv
from os import system as command
from json import loads as json_decode
from urllib2 import Request, urlopen
from urllib import urlencode
from platform import system
if system() in ['Linux']:
if command('echo "test" | xclip -sel clip'):
print 'Error: This program requires xclip to be installed. Try: sudo apt-get xclip'
raw_input('Press enter to exit.')
exit()
elif system() in ['Windows']:
if command('echo test | clip'):
print 'Error: This program requires access to the clipboard through an external program. Please download http://www.petri.co.il/software/clip.zip'
raw_input('Press enter to exit.')
exit()
url = 'http://api.imgur.com/2/upload.json'
key = 'Get one.'
args = argv[1:]
if not args:
print 'No images passed to the program.'
raw_input('Press enter to exit.')
exit()
if args[0].startswith('http'):
data = {
'key': key,
'image': args[0],
'path': args[0]
}
posts = [data]
else:
import base64
posts = []
for path in args:
with file(path, 'rb') as f:
image = f.read()
data = {
'key': key,
'image': base64.b64encode(image),
'path': path
}
posts.append(data)
if len(posts) > 1:
while True:
answer = raw_input('Uploading %d files. Proceed? ' % len(posts))
if answer in ['no', 'n']:
exit()
if answer not in ['yes', 'ye', 'y']:
print '"yes" or "no" please.'
else:
break
links = []
print '-' * 20
try:
for i, post in enumerate(posts):
data = urlencode(post)
print 'Uploading %s...' % (post['path']),
try:
u = urlopen(Request(url, data))
except Exception, e:
u = e
print 'Failed.'
fail = True
else:
print 'Done.'
fail = False
response = json_decode(u.read())
if fail:
print 'Error: %s.' % response['error']['message']
else:
link = response['upload']['links']['original']
links.append(link)
print 'Link #%d:' % len(links), link
print '-' * 20
except KeyboardInterrupt:
print 'Interrupted.'
print '-' * 20
print 'Successfully uploaded %d/%d files.' % (len(links), len(posts))
print 'NOTE: After exiting, all links will be lost and clipboard will be cleared.'
while True:
a = raw_input('Select a link to copy to clipboard. 0 to exit. Link number: ')
try:
i = int(a)
except:
print 'Numbers only please.'
continue
if i == 0:
print 'Exiting...'
exit()
if i < 0 or i > len(links):
print 'Numbers 0-%d only.' % len(links)
continue
if system() in ['Darwin']:
command('echo "%s" | pbcopy' % links[i-1])
elif system() in ['Linux']:
command('echo "%s" | xclip -sel clip' % links[i-1])
elif system() in ['Windows']:
command('echo %s | clip' % links[i-1])
print 'Copied!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment