Created
March 19, 2024 16:45
-
-
Save g8d3/8bdbbcd00f0c260279296f4b3d76daab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import psutil | |
import psycopg2 | |
from psycopg2 import sql | |
# import pygetwindow as gw | |
import subprocess | |
import platform | |
from datetime import datetime | |
# Function to get the title of the active window | |
def get_active_window_title(): | |
if platform.system() == "Windows": | |
import pygetwindow as gw | |
active_window = gw.getActiveWindow() | |
if active_window: | |
return active_window.title | |
else: | |
return None | |
elif platform.system() == "Linux": | |
try: | |
# Get the ID of the active window | |
active_window_id = subprocess.check_output(['xdotool', 'getactivewindow']).strip().decode('utf-8') | |
# Get the window title using xdotool | |
window_title = subprocess.check_output(['xdotool', 'getwindowname', active_window_id]).strip().decode('utf-8') | |
return window_title | |
except subprocess.CalledProcessError: | |
return None | |
else: | |
return None # Unsupported platform | |
# Function to connect to PostgreSQL and create necessary table if not exists | |
def create_database_connection(): | |
try: | |
conn = psycopg2.connect( | |
dbname="ttrack", | |
user="postgres", | |
password="ayayay", | |
host="localhost" | |
) | |
cur = conn.cursor() | |
# Creating a table if not exists | |
cur.execute(""" | |
CREATE TABLE IF NOT EXISTS time_tracking ( | |
id SERIAL PRIMARY KEY, | |
window_title TEXT, | |
time_spent INTERVAL, | |
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP | |
) | |
""") | |
conn.commit() | |
return conn, cur | |
except psycopg2.Error as e: | |
print("Error connecting to PostgreSQL:", e) | |
return None, None | |
# Function to insert data into the database | |
def insert_data(conn, cur, window_title, time_spent): | |
try: | |
cur.execute( | |
sql.SQL("INSERT INTO time_tracking (window_title, time_spent) VALUES (%s, %s)"), | |
(window_title, time_spent) | |
) | |
conn.commit() | |
except psycopg2.Error as e: | |
print("Error inserting data into PostgreSQL:", e) | |
# Main function to track time and insert into the database | |
def track_time(conn, cur): | |
try: | |
last_active_window = None | |
start_time = time.time() | |
while True: | |
active_window = get_active_window_title() | |
print(active_window) | |
if active_window != last_active_window: | |
if last_active_window is not None: | |
end_time = time.time() | |
time_spent = datetime.utcfromtimestamp(end_time - start_time).strftime('%H:%M:%S') | |
insert_data(conn, cur, last_active_window, time_spent) | |
start_time = time.time() | |
last_active_window = active_window | |
time.sleep(1) # Checking every 1 second | |
except KeyboardInterrupt: | |
print("Time tracking stopped.") | |
if __name__ == "__main__": | |
conn, cur = create_database_connection() | |
if conn and cur: | |
track_time(conn, cur) | |
cur.close() | |
conn.close() | |
# SELECT | |
# time_tracking.window_title, | |
# SUM(time_tracking.time_spent) | |
# FROM | |
# public.time_tracking | |
# GROUP BY | |
# time_tracking.window_title | |
# ORDER BY | |
# SUM(time_tracking.time_spent) desc; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment