-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickbb.py
66 lines (54 loc) · 1.58 KB
/
quickbb.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import base64
import json
import os
import subprocess
import sys
import time
import requests
import yaml
from PIL import Image
api_key_file = os.path.join(os.path.dirname(__file__), 'api_key.yaml')
try:
with open(api_key_file, 'r') as api_key_load:
api_key_var = yaml.safe_load(api_key_load)
except FileNotFoundError:
with open(api_key_file, 'w+') as api_key_load:
api_key_load.write(f'key: ')
print(
f'"api_key.yaml" does not exist and has been created. '
'Please edit the newly created "api_key.yaml" file, add your ImgBB API key, and re-run quickbb.'
)
exit()
api_key = api_key_var['key']
# if context menu is used
try:
image_file = sys.argv[1]
# drag and drop/manual input method if context menu isn't used
except IndexError:
image_file = input("Enter image location: ")
# validate file is an image file
try:
Image.open(image_file)
except OSError:
print("This is not an image file, please close this window and try again.")
time.sleep(7)
sys.exit(1)
print('Uploading to ImgBB...')
with open(image_file, 'rb') as file:
url = 'https://api.imgbb.com/1/upload'
payload = {
'key': api_key,
'image': base64.b64encode(file.read()),
}
res = requests.post(url, payload)
if res.status_code == 400:
print('Error communicating with ImgBB. Check your API key.')
time.sleep(7)
sys.exit(1)
bb_data = res.text
bb_parsed = json.loads(bb_data)
img_url = bb_parsed['data']['image']['url']
# copies to clipboard
cmd='echo '+img_url.strip()+'|clip'
subprocess.check_call(cmd, shell=True)
print('Done!')