diff --git a/tuhmayto/tracker.py b/tuhmayto/tracker.py index 0792673..e834481 100644 --- a/tuhmayto/tracker.py +++ b/tuhmayto/tracker.py @@ -30,11 +30,24 @@ def log_activity(timestamp: str) -> None: conn.close() +def fetch_latest_log() -> datetime | None: + conn = sqlite3.connect("activity_log.db") + cursor = conn.cursor() + cursor.execute("SELECT timestamp FROM activity_log ORDER BY timestamp DESC LIMIT 1") + result = cursor.fetchone() + conn.close() + return datetime.strptime(result[0], "%Y-%m-%d %H:%M:%S") if result else None + + def parse_last_seen(text: str) -> datetime | None: now = datetime.now() if "Visto por última vez" in text: + days_match = re.search(r"(\d+) día", text) hours_match = re.search(r"(\d+) horas", text) minutes_match = re.search(r"(\d+) minutos", text) + if days_match: + days_ago = int(days_match.group(1)) + return now - timedelta(days=days_ago) if hours_match: hours_ago = int(hours_match.group(1)) return now - timedelta(hours=hours_ago) @@ -65,8 +78,18 @@ def scrape_and_log(url: str) -> None: if not last_seen_time: return + latest_log = fetch_latest_log() + if latest_log and last_seen_time.date() <= latest_log.date(): + print(f"A log already exists for {latest_log} or later. Skipping new log.") + return + + if last_seen_time.hour == latest_log.hour: + print(f"An entry for this hour already exists. Skipping new log.") + return + timestamp = last_seen_time.strftime("%Y-%m-%d %H:%M:%S") log_activity(timestamp) + print(f"Logged activity: {timestamp}") if __name__ == "__main__":