-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtiktok-nav.py
230 lines (199 loc) · 8.95 KB
/
tiktok-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
230
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.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from tkinter import Tk # To get screen dimensions
# 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)
# Initialize ActionChains for keyboard interactions
actions = ActionChains(driver)
# Set the browser window to 50% of the screen width and position it on the right
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
# Display a notification directly in the browser using the Notification API
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.
"""
script = '''
(function(title, message) {
function createCustomModal(modalTitleText, modalMessageText) {
// Check if the modal already exists
var existingModal = document.getElementById('custom-modal');
if (existingModal) {
existingModal.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%';
modalContent.style.color = '#000'; // Ensure text color is black
var modalTitle = document.createElement('h2');
modalTitle.innerText = modalTitleText;
modalTitle.style.color = '#000'; // Set text color to black
var modalMessage = document.createElement('p');
modalMessage.innerText = modalMessageText;
modalMessage.style.color = '#000'; // Set text color to black
modalContent.appendChild(modalTitle);
modalContent.appendChild(modalMessage);
modal.appendChild(modalContent);
document.body.appendChild(modal);
// Auto-close the modal after 2 seconds
setTimeout(function() {
var modal = document.getElementById('custom-modal');
if (modal) {
modal.remove();
}
}, 2000);
}
if (typeof Notification !== "undefined") {
if (Notification.permission === "granted") {
var notification = new Notification(title, {
body: message,
icon: "https://cdn-icons-png.flaticon.com/512/633/633600.png"
});
setTimeout(function() { notification.close(); }, 2000);
} else if (Notification.permission !== "denied") {
Notification.requestPermission().then(function(permission) {
if (permission === "granted") {
var notification = new Notification(title, {
body: message,
icon: "https://cdn-icons-png.flaticon.com/512/633/633600.png"
});
setTimeout(function() { notification.close(); }, 2000);
} else {
createCustomModal(title, message);
}
});
} else {
createCustomModal(title, message);
}
} else {
createCustomModal(title, message);
}
})(arguments[0], arguments[1]);
'''
driver.execute_script(script, title, message)
# 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")
try:
# Set browser position
set_browser_position(driver)
# Step 1: Go to TikTok
driver.get("https://www.tiktok.com/foryou")
print("Navigated to TikTok.")
# Wait for the page to load
time.sleep(5)
# Accept cookies if prompted (TikTok may display a cookie consent dialog)
try:
consent_button = driver.find_element(By.XPATH, '//button[contains(text(), "Accept all cookies")]')
consent_button.click()
print("Accepted cookies.")
except Exception as e:
print("No cookie consent dialog found or error accepting cookies.")
# Wait a bit after accepting cookies
time.sleep(2)
# Optional: Close any pop-ups or modals (e.g., login prompts)
try:
close_button = driver.find_element(By.XPATH, '//button[@aria-label="Close"]')
close_button.click()
print("Closed pop-up modal.")
except Exception as e:
print("No pop-up modal found or error closing it.")
# Ensure the page is ready
time.sleep(2)
# Step 2: Start monitoring "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": Go to the next video
try:
# Simulate pressing the down arrow key
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.")