-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
300 lines (257 loc) · 10.6 KB
/
main.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
from art import tprint
from simple_term_menu import TerminalMenu
import sys
from yolo import Yolo
from datetime import datetime
from calculate_stat import get_labels, get_data, plot
import numpy as np
from termgraph import termgraph as tg
import os
import socket
import subprocess
class App:
def __init__(self):
tprint("awid")
self.yolo = Yolo()
self.machine = {"start" : self.yolo.start_session, "stop" : self.yolo.stop_session}
self.main_options = ["Url actions", "Analizer", "Get statistics", "Get videos", "Exit"]
self.options = self.main_options
self.urls = {}
self.actions = {
"Url actions" : self.url_actions,
"Add url" : self.add_url,
"Edit url" : self.edit_url,
"Delete url" : self.delete_url,
"Analizer" : self.analizer,
"Start analizer(s)" : self.start_analizer,
"Stop analizer(s)" : self.stop_analizer,
"Get statistics" : self.get_stat,
"Get videos" : self.get_videos,
"Exit" : self.exit
}
def url_actions(self):
self.options = ["Add url", "Edit url", "Delete url", "Back"]
self.update_menu()
if self.menu.chosen_menu_entry == "Back":
self.main()
else:
self.actions[self.menu.chosen_menu_entry]()
def add_url(self):
self.urls[input("Please, enter address: ")] = "not running"
input("Press Enter to continue...")
self.clear_console(2)
self.url_actions()
def edit_url(self):
if len(self.urls) != 0:
self.options = [*self.urls, "Back"]
self.update_menu()
if self.menu.chosen_menu_entry != "Back":
if self.urls[self.menu.chosen_menu_entry] == "running":
print("Stop analizer for this url first, then try to edit it again.")
input("Press Enter to continue...")
self.clear_console(2)
self.edit_url()
else:
url = input("Please, enter address: ")
if url != self.menu.chosen_menu_entry:
self.urls[url] = "not running"
del self.urls[self.menu.chosen_menu_entry]
input("Press Enter to continue...")
self.clear_console(2)
self.edit_url()
else:
self.url_actions()
else:
print("No urls to edit.")
input("Press Enter to continue...")
self.clear_console(2)
self.url_actions()
def delete_url(self):
if len(self.urls) != 0:
self.options = [*self.urls, "Back"]
self.update_menu()
if self.menu.chosen_menu_entry != "Back":
if self.urls[self.menu.chosen_menu_entry] == "running":
print("Stop analizer for this url first, then try to edit it again.")
input("Press Enter to continue...")
self.clear_console(2)
self.delete_url()
else:
answer = input("Are you sure you want to delete this url? [y/n]: ") or "n"
if answer.lower() == "y":
del self.urls[self.menu.chosen_menu_entry]
print("URL deleted.")
elif answer.lower() == "n":
print("Deletion cancelled.")
else:
print("Invalid key. Try again.")
input("Press Enter to continue...")
self.clear_console(3)
self.delete_url()
else:
self.url_actions()
else:
print("No urls to delete.")
input("Press Enter to continue...")
self.clear_console(2)
self.url_actions()
def analizer(self):
self.options = ["Start analizer(s)", "Stop analizer(s)", "Back"]
self.update_menu()
if self.menu.chosen_menu_entry == "Back":
self.main()
else:
self.actions[self.menu.chosen_menu_entry]()
def start_analizer(self):
if len(self.urls) != 0:
self.options = [*[url + f"[{self.urls[url]}]"for url in self.urls.keys()], "Back"]
self.update_menu()
choice = self.menu.chosen_menu_entry.replace("[running]", "").replace("[not running]", "")
if choice != "Back":
if self.urls[choice] == "not running":
self.urls[choice] = "running"
self.machine["start"](choice)
print(f"Analizer for {choice} is running now.")
else:
print("Analizer is already running.")
input("Press Enter to continue...")
self.clear_console(2)
self.start_analizer()
else:
self.analizer()
else:
print("No analizers to start.")
input("Press Enter to continue...")
self.clear_console(2)
self.main()
def stop_analizer(self):
if len(self.urls) != 0:
self.options = [*[url + f"[{self.urls[url]}]"for url in self.urls.keys()], "Back"]
self.update_menu()
choice = self.menu.chosen_menu_entry.replace("[running]", "").replace("[not running]", "")
if choice != "Back":
if self.urls[choice] == "running":
self.urls[choice] = "not running"
self.machine["stop"](choice)
print(f"Analizer for {choice} is stopped.")
else:
print("Analizer is already not running.")
input("Press Enter to continue...")
self.clear_console(2)
self.stop_analizer()
else:
self.analizer()
else:
print("No analizers to start.")
input("Press Enter to continue...")
self.clear_console(2)
self.main()
def get_stat(self):
if len(self.urls) != 0:
self.options = [*self.urls, "Back"]
self.update_menu()
if self.menu.chosen_menu_entry != "Back":
print("The time from which you want to display statistics:")
date_from = self.get_datetime_from_input()
print("The time until which you want to display statistics:")
date_until = self.get_datetime_from_input()
input("Press Enter to continue...")
self.clear_console(5)
labels = get_labels()
data = get_data(date_from, date_until, self.menu.chosen_menu_entry.split('/')[-1])
plot(labels, data, tg.normalize(np.log1p(data), 25))
input("Press Enter to continue...")
self.clear_console(16)
self.get_stat()
else:
self.main()
else:
print("No urls to get statistic.")
input("Press Enter to continue...")
self.clear_console(2)
self.main()
def get_datetime_from_input(self):
while True:
try:
datetime_str = input('Enter date and time in YYYY-MM-DD HH:MM format: ')
selected_datetime = datetime.strptime(datetime_str, '%Y-%m-%d %H:%M')
return selected_datetime
except ValueError:
input('Invalid date or time format. Press enter to continue.')
self.clear_console(2)
def is_local(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
sock.connect(("127.0.0.1", 1))
return True
except socket.error:
return False
finally:
sock.close()
def get_videos(self):
if len(self.urls) != 0:
self.options = [*self.urls, "Back"]
self.update_menu()
if self.menu.chosen_menu_entry != "Back":
print("The time from which you want to search video of bad situations:")
date_from = self.get_datetime_from_input()
print("The time until which you want to search video of bad situations:")
date_until = self.get_datetime_from_input()
input("Press Enter to continue...")
self.clear_console(5)
folder_path = "videos"
file_list = os.listdir(folder_path)
self.options = []
for file in file_list:
if file.startswith(self.menu.chosen_menu_entry.split('/')[-1]):
file_path = os.path.join(folder_path, file)
if os.path.isfile(file_path):
creation_time = datetime.fromtimestamp(os.path.getctime(file_path))
if date_from <= creation_time <= date_until:
self.options.append(file)
if len(self.options) == 0:
"No videos were found for this period."
input("Press Enter to continue...")
self.clear_console(2)
self.main()
else:
self.options.append("Back")
self.start_video()
else:
self.main()
else:
print("No urls to get statistic.")
input("Press Enter to continue...")
self.clear_console(2)
self.main()
def start_video(self):
self.update_menu()
if self.menu.chosen_menu_entry != "Back":
if self.is_local():
vlc_command = ['vlc', '--quiet', 'videos/' + self.menu.chosen_menu_entry]
with open(os.devnull, 'w') as devnull:
subprocess.run(vlc_command, stdout=devnull, stderr=devnull)
self.start_video()
else:
pass
# TODO: downloading video from remote server
else:
self.get_videos()
def exit(self):
self.clear_console(7)
sys.exit()
def update_menu(self):
self.menu = TerminalMenu(self.options)
self.menu.show()
def clear_console(self, n):
for _ in range(n):
sys.stdout.write("\033[F")
sys.stdout.write("\033[J")
sys.stdout.flush()
def main(self):
self.options = self.main_options
self.update_menu()
self.actions[self.menu.chosen_menu_entry]()
if __name__ == "__main__":
a = App()
a.main()