-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuush2imgur.py
107 lines (78 loc) · 2.51 KB
/
puush2imgur.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import time
import yaml
import sys
import os
import pyimgur
import argparse
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
# You can insert imgur api secrets here
# or set them as env var - IMGUR_ID and IMGUR_SECRET
# or set them in secrets.yaml
client_id = ''
client_secret = ''
home = os.path.expanduser("~")
path = os.path.join(home, 'Pictures')
def show_notification(msg):
if sys.platform == "linux" or sys.platform == "linux2":
os.system('notify-send -t 3 puush2imgur: ' + msg)
else:
print('puush2imgur: ' + msg)
def upload_photo(client, path):
print("Upload photo initialized: {0}".format(path))
try:
uploaded_image = client.upload_image(path, title="Uploaded with script")
except Exception as e:
show_notification("[failed]" + str(e))
return
show_notification(uploaded_image.link)
class PuushEventHandler(FileSystemEventHandler):
def __init__(self, client):
self.client = client
def on_created(self, event):
upload_photo(self.client, event.src_path)
def get_secret_credentials():
global client_id
global client_secret
try:
with open('secrets.yaml', 'r') as stream:
try:
cfg = yaml.load(stream)
client_id = cfg['client_id']
client_secret = cfg['client_secret']
except yaml.YAMLError as exc:
exit(exc)
except FileNotFoundError:
exit("Please insert imgur api credentials")
def main():
global client_id
global client_secret
global path
client_id = os.environ.get("IMGUR_ID")
client_secret = os.environ.get("IMGUR_SECRET")
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--path')
parser.add_argument('-i', '--client_id')
parser.add_argument('-s', '--client_secret')
args = parser.parse_args()
if args.path:
path = os.path.expanduser(args.path)
if args.client_id:
client_id = args.client_id
if args.client_secret:
client_secret = args.client_secret
if client_id is None and client_secret is None:
get_secret_credentials()
imgur_client = pyimgur.Imgur(client_id)
observer = Observer()
event_handler = PuushEventHandler(imgur_client)
observer.schedule(event_handler, path, recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == "__main__":
main()