Advertisement
Guest User

imgur

a guest
Mar 24th, 2013
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  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]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement