Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import sys
- import base64
- import json
- import os
- import urllib.request as request
- import urllib.parse
- import urllib.error
- CLIENT_ID = 'c707e3d4b9311f2'
- def base64_image(path):
- with open(path, 'rb') as f:
- return base64.b64encode(f.read())
- def imgur_upload(path):
- params = urllib.parse.urlencode({
- 'image': base64_image(path)
- })
- params = params.encode('utf8')
- req = request.Request('https://api.imgur.com/3/image', params)
- req.add_header('Authorization', 'Client-ID ' + CLIENT_ID)
- res = None
- try:
- res = request.urlopen(req)
- res = res.read()
- except urllib.error.HTTPError as ex:
- print(ex)
- if res:
- res = json.loads(str(res)[2:-1]) # strip b'' from the str()
- url = res['data']['link'].replace('\\', '')
- delete = res['data']['deletehash']
- os.system('echo "' + url + '" | xclip -i')
- os.system('echo "' + delete + '\n" >> ~/.imgur_deletes')
- if __name__ == "__main__":
- print(imgur_upload(sys.argv[1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement