-
Notifications
You must be signed in to change notification settings - Fork 0
/
record_test_2.py
321 lines (253 loc) · 11.9 KB
/
record_test_2.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
from pynput import mouse
from pynput import keyboard
import pyautogui
from tkinter import *
import time
import json
import os
class Record:
def __init__(self, filename, failsafekey, pause_key, img_selection_key, text_extraction_key, pool):
#class variable declaration
self.filename = filename
self.failsafekey = failsafekey
self.pause_key = pause_key
self.img_selection_key = img_selection_key
self.text_extraction_key = text_extraction_key
self.keypressed = set()
self.during_pause = []
self.data = []
self.paused = False
self.img_extraction_step = 1
self.img_extraction_name = 1
self.text_extraction_step = 1
self.text_extraction_name = 1
self.tempx = 0
self.tempy = 0
self.t = time.time()
self.pool = pool
try:
os.mkdir(f"screenshot/{self.filename}")
except:
pass
self.start()
import playsound
self.pool.submit(playsound.playsound,"sounds/recording start.wav")
self.run()
self.data = self.data[2:-1]
for index, block in enumerate(self.data):
if block == "record paused":
self.data.pop(index)
self.data.pop(index-1)
for index, block in enumerate(self.data):
if block[0] == 2 and block[3] == 2 and len(block)==7:
self.data[index].pop()
if block[0] == 3 and block[3] == 2 and len(block)==7:
self.data[index].pop()
if block == "record de-paused":
self.data.pop(index)
for i in range(100000):
if self.data[index+i][0] == 1:
self.data.pop(index+i)
self.data.pop(index+i)
self.data.pop(index+i)
break
self.pool.submit(playsound.playsound,"sounds/recording stop.wav")
def output(self):
return self.data
#start check
def start(self):
def on_press(key):
k = str(key).replace("Key.","").replace("'",'').replace("\\\\",'\\')
self.keypressed.add(k)
if self.keypressed == self.failsafekey:
self.kl.stop()
def on_release(key):
k = str(key).replace("Key.","").replace("'",'').replace("\\\\",'\\')
self.keypressed.discard(k)
self.kl = keyboard.Listener(on_press=on_press,on_release=on_release)
self.kl.start()
self.kl.join()
def run(self): ###a bug
try:
self.oslist = os.listdir(f"screenshot/{self.filename}")
except:
self.oslist = []
for i in range(1,10000):
if str(i)+".png" not in self.oslist:
self.count = i
break
def on_click(x,y,button,pressed):
try:
self.oslist = os.listdir(f"screenshot/{self.filename}")
except:
self.oslist = []
if not self.paused:
if self.during_pause:
self.data = self.data + self.during_pause
self.during_pause = []
for i in range(1,10000):
if str(i)+".png" not in self.oslist:
self.count = i
break
pyautogui.screenshot(f'screenshot/{self.filename}/{self.count}.png',region=(x-20,y-20,40,40))
self.count += 1
if pressed:
if str(button) == "Button.left":
try:
self.data[-1].append(time.time()-self.t)
except:
pass
self.data.append([0,x,y,"left",1,self.count])
self.t = time.time()
elif str(button) == "Button.right":
try:
self.data[-1].append(time.time()-self.t)
except:
pass
self.data.append([0,x,y,"right",1,self.count])
self.t = time.time()
if not pressed:
if str(button) == "Button.left":
try:
self.data[-1].append(time.time()-self.t)
except:
pass
self.data.append([0,x,y,"left",0,self.count])
self.t = time.time()
elif str(button) == "Button.right":
try:
self.data[-1].append(time.time()-self.t)
except:
pass
self.data.append([0,x,y,"right",0,self.count])
self.t = time.time()
elif self.paused:
if not pressed:
if str(button) == "Button.left":
if self.keypressed == self.img_selection_key and self.img_extraction_step == 1:
print("img1")
self.img_extraction_step = 2
self.during_pause.append([2,x,y,1,self.img_extraction_name,0])
elif self.keypressed == self.img_selection_key and self.img_extraction_step == 2:
x1 = self.during_pause[-1][1]
y1 = self.during_pause[-1][2]
for i in range(1,10000):
if "img_extraction_"+str(i)+".png" not in self.oslist:
self.img_extraction_name = i
break
print("img 2")
minx = min(x1,x)
miny = min(y1,y)
maxx = max(x1,x)
maxy = max(y1,y)
pyautogui.screenshot(f'screenshot/{self.filename}/img_extraction_{self.img_extraction_name}.png',region=(minx,miny,maxx-minx,maxy-miny))
self.img_extraction_step = 1
self.during_pause.append([2,x,y,2,self.img_extraction_name,0.5])
if self.keypressed == self.text_extraction_key and self.text_extraction_step == 1:
print("txt1")
self.text_extraction_step = 2
self.during_pause.append([3,x,y,1,self.text_extraction_name,0])
elif self.keypressed == self.text_extraction_key and self.text_extraction_step == 2:
x1 = self.during_pause[-1][1]
y1 = self.during_pause[-1][2]
for i in range(1,10000):
if "text_extraction_"+str(i)+".png" not in self.oslist:
self.text_extraction_name = i
break
print("txt 2")
minx = min(x1,x)
miny = min(y1,y)
maxx = max(x1,x)
maxy = max(y1,y)
pyautogui.screenshot(f'screenshot/{self.filename}/text_extraction_{self.text_extraction_name }.png',region=(minx,miny,maxx-minx,maxy-miny))
self.text_extraction_step = 1
self.during_pause.append([3,x,y,2,self.text_extraction_name,0.5])
def on_scroll(x,y,dx,dy):
if not self.paused:
try:
self.oslist = os.listdir(f"screenshot/{self.filename}")
except:
self.oslist = []
if self.during_pause:
self.data = self.data + self.during_pause
self.during_pause = []
for i in range(1,10000):
if str(i)+".png" not in self.oslist:
self.count = i
break
pyautogui.screenshot(f'screenshot/{self.filename}/{self.count}.png',region=(x-20,y-20,40,40))
self.count = self.count+ 1
try:
self.data[-1].append(time.time()-self.t)
except:
pass
if dy == 1:
self.data.append([0,x,y,120,2,self.count])
else:
self.data.append([0,x,y,-120,2,self.count])
self.t = time.time()
def on_press(key):
try:
self.oslist = os.listdir(f"screenshot/{self.filename}")
except:
self.oslist = []
k = str(key).replace("Key.","").replace("'",'').replace("\\\\",'\\')
self.keypressed.add(k)
if self.keypressed == self.failsafekey:
if self.during_pause:
self.data = self.data + self.during_pause
self.during_pause = []
self.paused = True
self.kl.stop()
self.ml.stop()
if self.keypressed == self.pause_key:
self.paused = not self.paused
import playsound
if self.paused:
self.pool.submit(playsound.playsound,"sounds/recording pause.wav")
self.data.append("record paused")
else:
self.pool.submit(playsound.playsound,"sounds/recording resume.wav")
self.data.append("record de-paused")
if not self.paused:
if self.during_pause:
self.data = self.data + self.during_pause
self.during_pause = []
try:
self.data[-1].append(time.time()-self.t)
except:
pass
self.data.append([1,1,k,self.count])
self.t = time.time()
def on_release(key):
try:
self.oslist = os.listdir(f"screenshot/{self.filename}")
except:
self.oslist = []
k = str(key).replace("Key.","").replace("'",'').replace("\\\\",'\\')
self.keypressed.discard(k)
if not self.paused:
if self.during_pause:
self.data = self.data + self.during_pause
self.during_pause = []
try:
self.data[-1].append(time.time()-self.t)
except:
pass
self.data.append([1,0,k,self.count])
self.t = time.time()
self.kl = keyboard.Listener(on_press=on_press,on_release=on_release)
self.ml = mouse.Listener(on_click=on_click,on_scroll=on_scroll)
self.kl.start()
self.ml.start()
self.kl.join()
self.ml.join()
def main(filename,failsafekey,pause_key,img_extraction_key,text_extraction_key,record_file_name):
from concurrent import futures
pool = futures.ThreadPoolExecutor(max_workers=10)
a = Record(filename,failsafekey,pause_key,img_extraction_key,text_extraction_key, pool)
block = a.output()
with open(f"record_folder/{record_file_name}.json","w") as f:
json.dump(block,f, indent=4)
if __name__ == "__main__":
main("record_10",["ctrl_l","f10"],["ctrl_l","f9"],["ctrl_l","f1"],["ctrl_l","f2"],"record_1")