-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathyoutube-nav.py
229 lines (197 loc) · 8.96 KB
/
youtube-nav.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
import os
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains # Import ActionChains
from selenium.webdriver.common.keys import Keys # Import Keys
from tkinter import Tk # To get screen dimensions dynamically
# Set up Chrome options
chrome_options = Options()
chrome_options.add_argument("--disable-notifications") # Disable native pop-ups
# Path to your chromedriver executable
chromedriver_path = "C:\\Users\\avery\\OneDrive\\Documents\\GitHub\\Neuro-Stress-Monitor\\chromedriver.exe" # Update this with your chromedriver path
# Initialize WebDriver
service = Service(chromedriver_path)
driver = webdriver.Chrome(service=service, options=chrome_options)
# Function to set browser position to occupy 50% of the right side of the screen
def set_browser_position(driver):
# Get the screen width and height
root = Tk()
root.withdraw() # Hide the Tkinter window
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# Calculate window dimensions and position
window_width = screen_width // 2 # 50% of the screen width
window_height = screen_height # Full screen height
window_x = screen_width // 2 # Start at the middle of the screen (right side)
window_y = 0 # Start at the top of the screen
# Set the window size and position
driver.set_window_rect(window_x, window_y, window_width, window_height)
print(f"Browser positioned at x={window_x}, y={window_y}, width={window_width}, height={window_height}")
# Initialize counts
focused_count = 0
unfocused_count = 0
# Path to the `file.txt`
file_path = os.path.join(os.path.dirname(__file__), "renderer", "file.txt")
# Initialize ActionChains for keyboard interactions
actions = ActionChains(driver)
def show_browser_notification(driver, title, message):
"""
Display a notification directly in the browser using the Notification API.
Falls back to a custom modal if notifications are not supported or denied.
Automatically closes the notification after 2 seconds.
"""
# Ensure title and message are properly escaped for JavaScript
title_js = json.dumps(title)
message_js = json.dumps(message)
script = f"""
(function() {{
function createCustomModal(title, message) {{
// Check if the modal already exists
if (document.getElementById('custom-modal')) {{
document.getElementById('custom-modal').remove();
}}
// Create modal elements
var modal = document.createElement('div');
modal.id = 'custom-modal';
modal.style.position = 'fixed';
modal.style.top = '0';
modal.style.left = '0';
modal.style.width = '100%';
modal.style.height = '100%';
modal.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
modal.style.display = 'flex';
modal.style.alignItems = 'center';
modal.style.justifyContent = 'center';
modal.style.zIndex = '9999';
var modalContent = document.createElement('div');
modalContent.style.backgroundColor = '#fff';
modalContent.style.padding = '20px';
modalContent.style.borderRadius = '5px';
modalContent.style.textAlign = 'center';
modalContent.style.maxWidth = '80%';
var modalTitle = document.createElement('h2');
modalTitle.innerText = title;
var modalMessage = document.createElement('p');
modalMessage.innerText = message;
modalContent.appendChild(modalTitle);
modalContent.appendChild(modalMessage);
modal.appendChild(modalContent);
document.body.appendChild(modal);
// Auto-close the modal after 2 seconds
setTimeout(function() {{
if (modal) {{
modal.remove();
}}
}}, 2000);
}}
if (typeof Notification !== "undefined") {{
if (Notification.permission === "granted") {{
var notification = new Notification({title_js}, {{
body: {message_js},
icon: "https://cdn-icons-png.flaticon.com/512/633/633600.png"
}});
setTimeout(function() {{ notification.close(); }}, 2000);
}} else if (Notification.permission !== "denied") {{
Notification.requestPermission().then(permission => {{
if (permission === "granted") {{
var notification = new Notification({title_js}, {{
body: {message_js},
icon: "https://cdn-icons-png.flaticon.com/512/633/633600.png"
}});
setTimeout(function() {{ notification.close(); }}, 2000);
}} else {{
createCustomModal({title_js}, {message_js});
}}
}});
}} else {{
createCustomModal({title_js}, {message_js});
}}
}} else {{
createCustomModal({title_js}, {message_js});
}}
}})();
"""
driver.execute_script(script)
try:
# Set browser position to 50% of the right side
set_browser_position(driver)
# Step 1: Go to YouTube
driver.get("https://www.youtube.com")
print("Navigated to YouTube.")
# Wait for the page to load
time.sleep(5)
# Step 2: Click the menu button to open the left menu and navigate to Shorts
try:
# Locate the menu button and click it
menu_button = driver.find_element(By.XPATH, '//button[contains(@aria-label, "Guide")]')
menu_button.click()
print("Menu button clicked to open the left menu.")
# Wait briefly for the menu to open
time.sleep(2)
# Locate the Shorts button and click it
shorts_button = driver.find_element(By.XPATH, '//a[@title="Shorts"]')
shorts_button.click()
print("Shorts button clicked.")
# Wait for the Shorts page to load by waiting for a unique element
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "ytd-reel-video-renderer"))
)
print("Shorts page is loaded.")
except Exception as e:
print(f"Error locating or interacting with the menu or Shorts button: {e}")
driver.quit()
exit()
# Step 3: Monitor "file.txt" every 10 seconds
while True:
time.sleep(10) # Wait for 10 seconds
# Read the content of "file.txt"
try:
with open(file_path, "r") as f:
status = f.read().strip()
print(f"Status read from file.txt: {status}")
except Exception as e:
print(f"Error reading file.txt: {e}")
continue # Skip this iteration if file cannot be read
if status == "Focused":
focused_count += 1
unfocused_count = 0 # Reset unfocused count
if focused_count % 2 == 0: # Trigger every 2nd "Focused"
# Show congratulatory notification
title = "Great Job!"
message = "Congratulations! You are being focused. Keep up the pace!"
show_browser_notification(driver, title, message)
elif status == "Unfocused":
unfocused_count += 1
focused_count = 0 # Reset focused count
if unfocused_count == 1:
# First "Unfocused": Mimic pressing the down arrow key
try:
actions.send_keys(Keys.ARROW_DOWN).perform()
print("Pressed down arrow key to go to next video.")
except Exception as e:
print(f"Error pressing down arrow key: {e}")
elif unfocused_count == 2:
# Second "Unfocused": Show a warning notification
title = "Attention!"
message = "You are not focused. Please refocus or the session will end soon."
show_browser_notification(driver, title, message)
elif unfocused_count == 3:
# Third "Unfocused": Show a final notification and close the browser
title = "Session Ended"
message = "You are not focused. Take a 5-10 minute break to re-energize!"
show_browser_notification(driver, title, message)
print("Closing the browser in 3 seconds.")
time.sleep(3) # Wait before closing
break # Exit the loop to close the browser
else:
print(f"Unrecognized status in file.txt: {status}")
finally:
# Close the browser
driver.quit()
print("Browser closed.")