-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
278 lines (238 loc) · 11.3 KB
/
Copy pathmain.py
File metadata and controls
278 lines (238 loc) · 11.3 KB
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
import os
import sys
import time
import threading
import pyautogui
import tkinter as tk
from tkinter import ttk, messagebox
# Import your custom tools
from tools.audio_utils import mute_all_chrome, unmute_all_chrome
from tools.vision_utils import locate_image
from tools.window_utils import set_chrome_window_state, get_chrome_window_info, scroll_window
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and PyInstaller. """
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
class AutoVideoApp:
def __init__(self, root):
self.root = root
self.root.title("NoStopVideo Automation")
self.running = False
self.mute_enabled = True
self.image_dir = resource_path("loc")
# Window styling
self.root.geometry("600x300")
self.root.resizable(False, False)
self.root.configure(bg="#e8f1fd")
# Center initial position
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
window_width = 600
window_height = 400
x = (screen_width - window_width) // 2
y = (screen_height - window_height) // 2
self.root.geometry(f"{window_width}x{window_height}+{x}+{y}")
# ttk styles
style = ttk.Style()
style.theme_use("clam")
style.configure("TButton", font=("Helvetica", 12, "bold"), padding=8, background="#3498db", foreground="white")
style.map("TButton", background=[("active", "#2980b9")])
style.configure("TLabel", font=("Helvetica", 14, "bold"), foreground="#2c3e50")
# GUI layout
self.title_label = ttk.Label(root, text="NoStopVideo Automation", foreground="#2c3e50")
self.title_label.pack(pady=15)
button_frame = tk.Frame(root, bg="#e8f1fd")
button_frame.pack(pady=10)
self.start_button = ttk.Button(button_frame, text="Start", command=self.start_automation)
self.start_button.pack(side=tk.LEFT, padx=10)
self.stop_button = ttk.Button(button_frame, text="Stop", command=self.stop_automation, state="disabled")
self.stop_button.pack(side=tk.LEFT, padx=10)
self.mute_button = ttk.Button(button_frame, text="Unmute", command=self.toggle_mute)
self.mute_button.pack(side=tk.LEFT, padx=10)
self.status_text = tk.Text(root, height=10, width=50, font=("Verdana", 11), bg="#ffffff", fg="#e74c3c", borderwidth=2, relief="groove")
self.status_text.pack(pady=15, padx=20)
# Check required images
required_images = ["yellow_flag.png", "Task_com.png", "next_button.png", "on.png", "notice_flag.png"]
for img in required_images:
if not os.path.exists(os.path.join(self.image_dir, img)):
messagebox.showerror("Error", f"{img} not found in {self.image_dir}")
exit(1)
self.root.after(1000, self.update_window_position)
def log(self, message):
"""Send logs safely to the main GUI thread."""
self.root.after(0, self._safe_log, message)
def _safe_log(self, message):
self.status_text.insert(tk.END, message + "\n")
self.status_text.see(tk.END)
def update_window_position(self):
info = get_chrome_window_info()
if info:
chrome_x, chrome_y, chrome_width, chrome_height = info
new_x = chrome_x + (chrome_width - 600) // 2
new_y = chrome_y + chrome_height + 10
self.root.geometry(f"600x300+{new_x}+{new_y}")
self.root.after(1000, self.update_window_position)
def toggle_mute(self):
self.mute_enabled = not self.mute_enabled
if self.mute_enabled:
self.mute_button.config(text="Unmute")
self.log("Auto-mute ENABLED.")
mute_all_chrome(logger=self.log)
else:
self.mute_button.config(text="Mute")
self.log("Auto-mute DISABLED.")
unmute_all_chrome(logger=self.log)
def play_and_mute_window(self, win, region):
on_button = os.path.join(self.image_dir, "on.png")
on_pos = locate_image(on_button, region)
if on_pos:
center_x, center_y, _, _ = on_pos
pyautogui.click(center_x, center_y)
self.log("Clicked play button.")
else:
self.log("Play button missing, skipping.")
return False
if self.mute_enabled:
mute_all_chrome(logger=self.log)
else:
self.log("Audio kept on.")
return True
def automation_loop(self):
yellow_flag = os.path.join(self.image_dir, "yellow_flag.png")
task_com = os.path.join(self.image_dir, "Task_com.png")
next_button = os.path.join(self.image_dir, "next_button.png")
notice_flag = os.path.join(self.image_dir, "notice_flag.png")
tip_flag = os.path.join(self.image_dir, "tip_flag.png")
Ques1_in_v = os.path.join(self.image_dir, "True_or_false.png")
Ques2_in_v = os.path.join(self.image_dir, "select.png")
selector_button = os.path.join(self.image_dir, "selector.png")
submit_button = os.path.join(self.image_dir, "submit.png")
submit_button_in_v = os.path.join(self.image_dir, "submit_in_v.png")
table = os.path.join(self.image_dir, "table.png")
self.log("Loop started.")
try:
while self.running:
win = set_chrome_window_state(logger=self.log)
if not win:
break
region = (0, 0, 1280, 1000)
scroll_window(win, "top")
tb = locate_image(table, region)
if tb:
center_x, center_y, _, _ = tb
pyautogui.click(center_x, center_y)
self.log("Table collapsed.")
time.sleep(2)
if not locate_image(yellow_flag, region):
self.log("Video task completed.")
if not self.mute_enabled:
unmute_all_chrome(logger=self.log)
else:
self.log("Waiting for video...")
self.play_and_mute_window(win, region)
while self.running:
self.log(f"Checking status... {time.strftime('%H:%M:%S')}")
tc = locate_image(task_com, region)
if tc:
self.log("Video finished.")
break
ques1 = locate_image(Ques1_in_v, region)
ques2 = locate_image(Ques2_in_v, region)
if ques1 or ques2:
self.log("Question detected.")
try:
matches = list(pyautogui.locateAllOnScreen(selector_button, region=region, confidence=0.95))
if not matches:
self.log("No options found.")
time.sleep(3)
continue
matches.sort(key=lambda pos: pos.top)
for i, pos in enumerate(matches):
if not self.running: break
center_x = pos.left + pos.width // 2
center_y = pos.top + pos.height // 2
pyautogui.click(center_x, center_y)
self.log(f"Clicked option {i+1}")
time.sleep(0.2)
sub = locate_image(submit_button_in_v, region)
if sub:
center_x, center_y, _, _ = sub
pyautogui.click(center_x, center_y)
self.log("Submitted answer.")
time.sleep(2)
else:
self.log("Submit button missing.")
if not (locate_image(Ques1_in_v, region) or locate_image(Ques2_in_v, region)):
self.log("Question cleared.")
break
except Exception as e:
self.log(f"Question error: {e}")
time.sleep(5)
if not self.running: break
for i in range(2):
if not self.running: break
scroll_window(win, "bottom")
scroll_window(win, "bottom")
nb = locate_image(next_button, region)
if nb:
center_x, center_y, _, _ = nb
pyautogui.click(center_x, center_y)
self.log(f"Clicked 'Next' ({i+1}/2).")
time.sleep(2)
else:
self.log("'Next' button missing.")
break
if not self.running: break
tip = locate_image(tip_flag, region)
if tip:
self.log("Warning popup detected.")
time.sleep(5)
break
notice = locate_image(notice_flag, region)
if notice:
self.log("Notice popup detected.")
time.sleep(0.5)
tip = locate_image(tip_flag, region)
if tip:
self.log("Warning popup detected.")
time.sleep(5)
break
nb = locate_image(next_button, region)
if nb:
center_x, center_y, _, _ = nb
pyautogui.click(center_x, center_y)
self.log("Skipped via 'Next'.")
time.sleep(2)
else:
self.log("No 'Next' on popup.")
else:
self.log("No popups. Skipping.")
time.sleep(1)
self.log("Processing audio.")
self.play_and_mute_window(win, region)
self.log("Checking next task...")
time.sleep(2)
except pyautogui.FailSafeException:
self.log("FAILSAFE: Mouse in corner. Stopped.")
except Exception as e:
self.log(f"Error: {e}")
finally:
self.log("Task ended.")
self.root.after(0, self.stop_automation)
def start_automation(self):
if not self.running:
self.running = True
self.start_button.config(state="disabled")
self.stop_button.config(state="normal")
threading.Thread(target=self.automation_loop, daemon=True).start()
def stop_automation(self):
self.running = False
self.start_button.config(state="normal")
self.stop_button.config(state="disabled")
if __name__ == "__main__":
root = tk.Tk()
app = AutoVideoApp(root)
root.mainloop()