Advertisement
Guest User

imgur upload script

a guest
Feb 6th, 2011
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. from sys import argv
  2. from os import system as command
  3. from json import loads as json_decode
  4. from urllib2 import Request, urlopen
  5. from urllib import urlencode
  6.  
  7. url = 'http://api.imgur.com/2/upload.json'
  8. key = 'bb3a42eaa83e13a1c8afdc87290674fa '
  9.  
  10. args = argv[1:]
  11.  
  12. import base64
  13. posts = []
  14. for path in args:
  15.     with file(path, 'rb') as f:
  16.         image = f.read()
  17.     data = {
  18.         'key': key,
  19.         'image': base64.b64encode(image),
  20.         'path': path
  21.     }
  22.     posts.append(data)
  23.  
  24. links = []
  25.  
  26. print '-' * 20
  27.  
  28. try:
  29.     for i, post in enumerate(posts):
  30.         data = urlencode(post)
  31.    
  32.         print 'Uploading %s...' % (post['path']),
  33.         try:
  34.             u = urlopen(Request(url, data))
  35.         except Exception, e:
  36.             u = e
  37.             print 'Failed.'
  38.             fail = True
  39.         else:
  40.             print 'Done.'
  41.             fail = False
  42.    
  43.         response = json_decode(u.read())
  44.        
  45.         if fail:
  46.             print 'Error: %s.' % response['error']['message']
  47.         else:
  48.             link = response['upload']['links']['original']
  49.             links.append(link)
  50.             print 'Link #%d:' % len(links), link
  51.        
  52.         print '-' * 20
  53. except KeyboardInterrupt:
  54.     print 'Interrupted.'
  55.     print '-' * 20
  56.  
  57. command('echo %s | clip' % links[i-1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement