Skip to content

Commit e1abc46

Browse files
authored
Update ActivityMonitor.py
fixed the shit opus missed
1 parent 1129cad commit e1abc46

File tree

1 file changed

+109
-23
lines changed

1 file changed

+109
-23
lines changed

ActivityMonitor.py

Lines changed: 109 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1+
import time
2+
import os
3+
import platform
4+
import subprocess
5+
import threading
16
import customtkinter as ctk
27
import keyboard
38
import clipboard
4-
import pygetwindow as gw
5-
import pyscreenshot as ImageGrab
6-
import time
7-
import markdown2
8-
import threading
9-
import os
9+
import re
1010
import ollama
1111

12+
if platform.system() == "Linux":
13+
import mss
14+
import mss.tools
15+
elif platform.system() == "Windows":
16+
import pyscreenshot as ImageGrab
17+
import pygetwindow as gw
18+
19+
screenshot_directory = "screenshots"
20+
archive_directory = "archives"
21+
1222
def generate_answers(model_name, prompt):
1323
model_name = "vicuna:13b-16k"
1424
messages = [
@@ -23,6 +33,62 @@ def generate_answers(model_name, prompt):
2333
except Exception as e:
2434
print(f"There was an error communicating with the Ollama model: {e}")
2535
return None
36+
37+
def get_active_window_title():
38+
"""
39+
Get the title of the active window.
40+
41+
Returns:
42+
str: The title of the active window.
43+
"""
44+
if platform.system() == "Linux":
45+
try:
46+
# Get the window ID of the currently focused window
47+
window_id = subprocess.check_output(["xdotool", "getactivewindow"]).decode().strip()
48+
49+
# Get the name of the window with this ID
50+
window_name = subprocess.check_output(["xdotool", "getwindowname", window_id]).decode().strip()
51+
52+
# Get the geometry of the window (position and size)
53+
geom_output = subprocess.check_output(["xdotool", "getwindowgeometry", "--shell", window_id]).decode()
54+
55+
# Use a dictionary to store variables from the shell output
56+
geom_vars = dict(re.findall(r'(\w+)=(\S+)', geom_output))
57+
58+
# Convert values to integers
59+
x = int(geom_vars['X'])
60+
y = int(geom_vars['Y'])
61+
width = int(geom_vars['WIDTH'])
62+
height = int(geom_vars['HEIGHT'])
63+
64+
with mss.mss() as sct:
65+
# The screenshot region is a tuple: (x, y, width, height)
66+
monitor = {"top": y, "left": x, "width": width, "height": height}
67+
screenshot_path = os.path.join(screenshot_directory, f"{time.time()}_screenshot.png")
68+
# Capture the specified region
69+
sct_img = sct.grab(monitor)
70+
mss.tools.to_png(sct_img.rgb, sct_img.size, output=screenshot_path)
71+
72+
return window_name
73+
except subprocess.CalledProcessError as e:
74+
print(f"Error retrieving window title: {e}")
75+
return None
76+
elif platform.system() == "Windows":
77+
# Capture active window and screenshot
78+
active_window = gw.getActiveWindow()
79+
if active_window:
80+
try:
81+
# save screenshot
82+
screenshot_path = os.path.join(screenshot_directory, f"{time.time()}_screenshot.png")
83+
screenshot = ImageGrab.grab(bbox=active_window.box)
84+
screenshot.save(screenshot_path)
85+
86+
# save title and screenshot in markdown log
87+
window_title = active_window.title if active_window.title else "Unknown Window"
88+
return window_title
89+
except Exception as e:
90+
print(f"Error getting active window: {e}")
91+
return None
2692

2793
class ActivityMonitor(ctk.CTk):
2894
def __init__(self):
@@ -70,8 +136,8 @@ def stop_monitoring(self):
70136
self.status_label.configure(text="Status: Stopped")
71137

72138
def analyze_with_ollama(self):
73-
prompt = f"Please analyze the following activities and add your comments:\n\n{self.markdown_log}"
74-
detailed_description = generate_answers("vicuna:13b-16k", prompt)
139+
prompt: str = f"Please analyze the following activities and add your comments:\n\n{self.markdown_log}"
140+
detailed_description = generate_answers(model_name="vicuna:13b-16k", prompt=prompt)
75141
if detailed_description:
76142
self.markdown_log += f"\n\n## Ollama's Analysis\n\n{detailed_description}"
77143
self.write_markdown_file()
@@ -84,8 +150,9 @@ def quit_monitoring(self):
84150
def monitor_activities(self):
85151
previous_clipboard = ""
86152
previous_window = None
87-
screenshot_directory = "screenshots"
88153
os.makedirs(screenshot_directory, exist_ok=True)
154+
os.makedirs(archive_directory, exist_ok=True)
155+
window_title = None
89156

90157
while self.is_running:
91158
# Capture clipboard content
@@ -95,20 +162,10 @@ def monitor_activities(self):
95162
previous_clipboard = current_clipboard
96163

97164
# Capture active window and screenshot
98-
active_window = gw.getActiveWindow()
99-
if active_window and (active_window != previous_window):
100-
try:
101-
# save screenshot
102-
screenshot_path = os.path.join(screenshot_directory, f"{time.time()}_screenshot.png")
103-
screenshot = ImageGrab.grab(bbox=active_window.box)
104-
screenshot.save(screenshot_path)
105-
106-
# save title and screenshot in markdown log
107-
window_title = active_window.title if active_window.title else "Unknown Window"
108-
self.markdown_log += f"\n## Window Selected: {window_title}\n\n![Screenshot]({screenshot_path})\n"
109-
previous_window = active_window
110-
except Exception as e:
111-
print(f"Error getting active window: {e}")
165+
window_title = get_active_window_title()
166+
if previous_window != window_title:
167+
self.markdown_log += f"\n## Window Selected: {window_title}\n\n![Screenshot]({screenshot_directory}/{time.time()}_screenshot.png)\n"
168+
previous_window = window_title
112169

113170
# Capture typed keys (improved version)
114171
key_events = keyboard.record(until='enter')
@@ -117,8 +174,37 @@ def monitor_activities(self):
117174
self.markdown_log += f"\n- **Typed Text**: {typed_text.replace('space', ' ')}\n"
118175
self.text_buffer = ""
119176

177+
# Archive URLs
178+
self.archive_url(archive_directory)
179+
120180
time.sleep(1) # Reduce CPU usage
121181

182+
def archive_url(self, directory):
183+
"""
184+
Archive URLs.
185+
186+
Args:
187+
directory (str): The directory to save the archived URLs.
188+
"""
189+
window_title = get_active_window_title()
190+
# Extend or adjust the list of browser window titles as needed
191+
browsers = ["Brave", "Firefox", "Chrome", "Chromium", "Edge", "Opera"]
192+
if any(browser in window_title for browser in browsers):
193+
# Set focus to the address bar and copy URL
194+
keyboard.send('ctrl+l') # Focus on address bar
195+
time.sleep(0.1) # Briefly wait for focus to be set
196+
keyboard.send('ctrl+c') # Copy URL
197+
time.sleep(0.1) # Wait for clipboard to update
198+
keyboard.send('esc') # Escape
199+
200+
# Read URL from clipboard
201+
url = clipboard.paste()
202+
if url.startswith("http") or url.startswith("https"):
203+
with open(os.path.join(directory, f"{time.time()}_url.txt"), "w", encoding='utf-8') as file:
204+
file.write(url)
205+
else:
206+
print("The active window is not a recognized browser window.")
207+
122208
def write_markdown_file(self):
123209
with open("activity_log.md", "w", encoding='utf-8') as md_file:
124210
md_file.write(self.markdown_log)

0 commit comments

Comments
 (0)