-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen_tacker.py
214 lines (175 loc) · 6.63 KB
/
screen_tacker.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import os
from time import sleep
import datetime
import tkinter as tk
from multiprocessing import Process, Queue
from mss import mss
import requests
class GUI:
def __init__(self, command_queue, save_screenshots_queue):
self.running = False
self.master = tk.Tk()
self.master.geometry('{}x{}'.format(290, 400))
self.master.resizable(width=False, height=False)
self.master.winfo_toplevel().title('Time tracker')
self.master.protocol("WM_DELETE_WINDOW", self.on_closing)
row = 0
self.time_variable = tk.StringVar()
self.time_variable.set('00:00:00')
self.time_label = tk.Label(
self.master,
font=("Times New Roman", 20),
height=2,
width=19,
textvariable=self.time_variable
)
self.time_label.grid(row=row, sticky=tk.W + tk.E)
self.master.grid_rowconfigure(row, minsize=70)
row += 1
self.description_title_label = tk.Label(
self.master,
font=("Times New Roman", 16),
text='Description:',
anchor=tk.W
)
self.description_title_label.grid(row=row, sticky=tk.W + tk.E)
row += 1
self.description_variable = tk.StringVar()
self.description = tk.Text(
self.master,
font=("Times New Roman", 11),
height=4,
width=10
)
self.description.grid(row=row, sticky=tk.W + tk.E + tk.N + tk.S)
self.description.bind('<Return>', self.add_description)
self.description.bind('<FocusOut>', self.add_description)
self.description.bind('<Button-1>', self.change_description)
self.master.grid_rowconfigure(row, minsize=50)
row += 1
buttons_height = 4
self.start_button = tk.Button(
self.master,
text="Start tracking",
height=buttons_height,
command=self.start_tracking
)
self.start_button.grid(row=row, sticky=tk.W + tk.E + tk.S)
self.master.grid_rowconfigure(row, minsize=110)
row += 1
self.stop_button = tk.Button(
self.master,
text="Stop tracking",
height=buttons_height,
command=self.stop_tracking
)
self.stop_button.grid(row=row, sticky=tk.W + tk.E)
row += 1
self.command_queue = command_queue
self.save_screenshots_queue = save_screenshots_queue
self.screenshots_process = self.screenshots_sender = None
self.time_tracking_process = None
self.hour = self.minute = self.second = 0
def add_description(self, *args):
self.description_variable.set(self.description.get('0.0', tk.END))
self.description.config(state=tk.DISABLED)
def change_description(self, *args):
if not self.running:
self.description.config(state=tk.NORMAL)
def update_time(self):
if self.running:
self.time_variable.set(
datetime.time(hour=self.hour, minute=self.minute, second=self.second).strftime('%H:%M:%S')
)
self.master.after(1000, self.update_time)
self.second += 1
if self.second == 60:
self.minute += 1
self.second = 0
if self.minute == 60:
self.hour += 1
self.minute = 0
def on_closing(self):
if self.screenshots_process:
self.screenshots_process.terminate()
if self.screenshots_sender:
self.screenshots_sender.terminate()
self.master.destroy()
def start_tracking(self):
if self.running:
return
self.add_description()
self.running = True
self.screenshots_process = Process(
target=making_screenshots,
args=(self.command_queue, self.save_screenshots_queue)
)
self.screenshots_process.start()
self.screenshots_sender = Process(
target=sending_screenshots,
args=(self.save_screenshots_queue, self.description_variable.get())
)
self.screenshots_sender.start()
self.command_queue.put('start')
self.update_time()
def stop_tracking(self):
if not self.running:
return
self.running = False
self.hour = self.minute = self.second = 0
self.time_variable.set(
datetime.time(hour=self.hour, minute=self.minute, second=self.second).strftime('%H:%M:%S')
)
self.command_queue.put('stop')
if self.screenshots_process:
self.screenshots_process.terminate()
if self.screenshots_sender:
self.screenshots_sender.terminate()
def making_screenshots(command_queue, save_screenshots_queue, period=600, destination_dir='screenshots'):
try:
os.mkdir(destination_dir)
except FileExistsError:
pass
action = None
sleep(3)
with mss() as sct:
while True:
if not command_queue.empty():
action = command_queue.get()
if action == 'start':
date = datetime.datetime.now()
filename = '{}/{}.png'.format(
destination_dir,
date.strftime('%M-%H-%d-%m-%Y')
)
sct.shot(output=filename)
save_screenshots_queue.put((date.strftime('%Y-%m-%dT%H:%M'), filename))
sleep(period)
def sending_screenshots(save_screenshots_queue, description):
url = 'http://127.0.0.1:8000/work_diary/api/upload_screenshots/'
while True:
if not save_screenshots_queue.empty():
create_date, filename = save_screenshots_queue.get()
with open(filename, 'rb') as file:
resp = requests.post(
url=url,
data={
'description': description or str(),
'create_date': create_date
},
files={
'image': (filename, file)
}
)
if resp.json()['status'] == 'success':
try:
os.remove(filename)
except FileNotFoundError:
pass
if __name__ == '__main__':
command_queue = Queue()
save_screenshots_queue = Queue()
command_queue.cancel_join_thread()
save_screenshots_queue.cancel_join_thread()
gui = GUI(command_queue=command_queue, save_screenshots_queue=save_screenshots_queue)
gui.master.mainloop()