-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pymgur.py
49 lines (45 loc) · 1.31 KB
/
Pymgur.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import clipboard
import json
import requests
import base64
import sys
import os
import datetime
import notification
from PIL import Image
from console import hud_alert
API_KEY = "6fc84ae9833a1b66fb5d7e171cdbe71d204fc41a"
CLIENT_ID = "2693d5d109271c9"
headers = {"Authorization": "Client-ID {0}".format(CLIENT_ID)}
url = "https://api.imgur.com/3/upload.json"
img = clipboard.get_image(idx=0)
def main():
try:
img.save("pymgur_tmp.png", 'PNG')
except AttributeError:
print "There is no image on the clipboard'"
sys.exit()
f = open("pymgur_tmp.png", "rb")
binary_data = f.read()
b64image = base64.b64encode(binary_data)
f.close()
os.remove("pymgur_tmp.png")
payload = {'key': API_KEY,
'image': b64image,
'type': 'base64',
'title': 'Pymgur Upload {}'.format(datetime.datetime.today().strftime("%c"))
}
print "Uploading..."
try:
r = requests.post(url, headers = headers, data = payload)
except requests.ConnectionError:
hud_alert('No internet connection', icon = 'error')
sys.exit()
j = json.loads(r.text)
link = j["data"]["link"]
clipboard.set(link)
hud_alert('Link Copied!')
notification.schedule("Pymgur Upload Complete!", delay=0, action_url=link)
print "The image link ({}) has been copied to the clipboard!".format(link)
if __name__ == "__main__":
main()