Use logging module instead of print

This commit is contained in:
2025-06-14 17:05:59 -04:00
parent 51f987c814
commit 38821b682e
2 changed files with 22 additions and 16 deletions

View File

@@ -2,8 +2,8 @@
Type=Application Type=Application
Name=ActivityWatch Amazing Marvin Watcher Name=ActivityWatch Amazing Marvin Watcher
Comment=Tracks Amazing Marvin tasks in ActivityWatch Comment=Tracks Amazing Marvin tasks in ActivityWatch
Exec=poetry run awam Exec=poetry run aw-watcher-marvin
Path=/home/felixm/dev/awam Path=/home/felixm/dev/aw-watcher-marvin
Icon=activitywatch Icon=activitywatch
StartupNotify=false StartupNotify=false
NoDisplay=true NoDisplay=true

View File

@@ -1,3 +1,4 @@
import logging
import os import os
import sys import sys
from datetime import datetime, timezone from datetime import datetime, timezone
@@ -22,11 +23,11 @@ def get_tracked_task(api_token: str) -> Optional[Dict[str, Any]]:
if response.status_code == 200: if response.status_code == 200:
return response.json() return response.json()
else: else:
print(f"No tracked task found (status: {response.status_code})") logging.debug(f"No tracked task found (status: {response.status_code})")
return None return None
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
print(f"Error fetching tracked task: {e}") logging.error(f"Error fetching tracked task: {e}")
return None return None
@@ -38,10 +39,10 @@ def setup_activitywatch_client() -> Tuple[ActivityWatchClient, str]:
try: try:
client.create_bucket(bucket_id, event_type=event_type) client.create_bucket(bucket_id, event_type=event_type)
print(f"ActivityWatch bucket '{bucket_id}' ready") logging.info(f"ActivityWatch bucket '{bucket_id}' ready")
return client, bucket_id return client, bucket_id
except Exception as e: except Exception as e:
print(f"Error setting up ActivityWatch: {e}") logging.error(f"Error setting up ActivityWatch: {e}")
sys.exit(1) sys.exit(1)
@@ -59,22 +60,27 @@ def create_task_event(tracked_task: Dict[str, Any]) -> Event:
def main() -> None: def main() -> None:
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%H:%M:%S",
handlers=[logging.StreamHandler()],
)
username = os.getlogin() username = os.getlogin()
token = keyring.get_password("amazing-marvin-api-token", username) token = keyring.get_password("amazing-marvin-api-token", username)
if not token: if not token:
print( logging.error(
"No API token found in keyring. Please store your Amazing Marvin API token:" f"No API token found in keyring. Please store your Amazing Marvin API token:"
)
print(
f"Run: python -c \"import keyring; keyring.set_password('amazing-marvin-api-token', '{username}', 'YOUR_TOKEN_HERE')\"" f"Run: python -c \"import keyring; keyring.set_password('amazing-marvin-api-token', '{username}', 'YOUR_TOKEN_HERE')\""
) )
sys.exit(1) sys.exit(1)
client, bucket_id = setup_activitywatch_client() client, bucket_id = setup_activitywatch_client()
print("Starting Amazing Marvin task tracking...") logging.info("Starting Amazing Marvin task tracking...")
print("Press Ctrl+C to stop") logging.info("Press Ctrl+C to stop")
current_task_id = None current_task_id = None
heartbeat_interval = 60 heartbeat_interval = 60
@@ -90,7 +96,7 @@ def main() -> None:
if task_id != current_task_id: if task_id != current_task_id:
current_task_id = task_id current_task_id = task_id
print(f"Now tracking: {task_title}") logging.info(f"Now tracking: {task_title}")
event = create_task_event(tracked_task) event = create_task_event(tracked_task)
client.heartbeat( client.heartbeat(
@@ -102,12 +108,12 @@ def main() -> None:
else: else:
if current_task_id is not None: if current_task_id is not None:
current_task_id = None current_task_id = None
print("No task currently being tracked") logging.info("No task currently being tracked")
sleep(heartbeat_interval) sleep(heartbeat_interval)
except KeyboardInterrupt: except KeyboardInterrupt:
print("\nStopping Amazing Marvin task tracking...") logging.info("\nStopping Amazing Marvin task tracking...")
sleep(1) # Give time for queued events to be sent sleep(1) # Give time for queued events to be sent