-
Notifications
You must be signed in to change notification settings - Fork 0
/
capture2imgur.py
63 lines (47 loc) · 1.54 KB
/
capture2imgur.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
#!/usr/bin/env python
# Captures a screenshot, uploads it to imgur, copies the URL to clipboard and dumps it into a history.csv file.
from subprocess import call, check_output
from imgurpython import ImgurClient
from os.path import expanduser
from time import strftime
import pyperclip
import csv
import os
home = expanduser("~")
_path = os.path.join(home, "imgur")
def take_screen():
path = os.path.join(_path, "imgur.png")
win_id = int(check_output(['xdotool', 'getactivewindow']))
screen = check_output(["maim", path, "-i " + str(win_id)])
notify("active window captured")
return path
def upload(path):
client = ImgurClient("1cc97dbda52da82", "")
image = client.upload_from_path(path, config=None, anon=True)
if not image["link"]:
notify("error uploading")
else:
notify("uploaded")
return image["link"]
def save_data(date, link):
path = os.path.join(_path, "history")
if not os.path.exists(path):
os.mkdir(path)
with open(os.path.join(path, "uploads.csv"), 'a', newline='') as uploads:
writer = csv.writer(uploads, delimiter=';')
writer.writerow([date, link])
def copy_text(text):
pyperclip.copy(text)
pyperclip.paste()
def notify(message):
call(["/usr/bin/notify-send", message])
if __name__ == '__main__':
if not os.path.exists(_path):
os.mkdir(_path)
now = strftime("%d.%m.%Y %H:%M:%S")
path = take_screen()
if path:
link = upload(path)
if link:
copy_text(link)
save_data(now, link)