Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

imgur

By: a guest on Mar 24th, 2013  |  syntax: Python  |  size: 1.05 KB  |  views: 252  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/python3
  2. import sys
  3. import base64
  4. import json
  5. import os
  6. import urllib.request as request
  7. import urllib.parse
  8. import urllib.error
  9.  
  10. CLIENT_ID = 'c707e3d4b9311f2'
  11.  
  12. def base64_image(path):
  13.     with open(path, 'rb') as f:
  14.         return base64.b64encode(f.read())
  15.  
  16. def imgur_upload(path):
  17.     params = urllib.parse.urlencode({
  18.         'image': base64_image(path)
  19.     })
  20.     params = params.encode('utf8')
  21.     req = request.Request('https://api.imgur.com/3/image', params)
  22.     req.add_header('Authorization', 'Client-ID ' + CLIENT_ID)
  23.     res = None
  24.     try:
  25.         res = request.urlopen(req)
  26.         res = res.read()
  27.     except urllib.error.HTTPError as ex:
  28.         print(ex)
  29.  
  30.     if res:
  31.         res = json.loads(str(res)[2:-1]) # strip b'' from the str()
  32.         url = res['data']['link'].replace('\\', '')
  33.         delete = res['data']['deletehash']
  34.         os.system('echo "' + url + '" | xclip -i')
  35.         os.system('echo "' + delete + '\n" >> ~/.imgur_deletes')
  36.  
  37. if __name__ == "__main__":
  38.     print(imgur_upload(sys.argv[1]))