tracker now considers days and prevents duplicated

This commit is contained in:
Danilo Reyes 2024-12-06 12:09:14 -06:00
parent 9bb0301aa2
commit f13f566650

View File

@ -30,11 +30,24 @@ def log_activity(timestamp: str) -> None:
conn.close() 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: def parse_last_seen(text: str) -> datetime | None:
now = datetime.now() now = datetime.now()
if "Visto por última vez" in text: if "Visto por última vez" in text:
days_match = re.search(r"(\d+) día", text)
hours_match = re.search(r"(\d+) horas", text) hours_match = re.search(r"(\d+) horas", text)
minutes_match = re.search(r"(\d+) minutos", 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: if hours_match:
hours_ago = int(hours_match.group(1)) hours_ago = int(hours_match.group(1))
return now - timedelta(hours=hours_ago) return now - timedelta(hours=hours_ago)
@ -65,8 +78,18 @@ def scrape_and_log(url: str) -> None:
if not last_seen_time: if not last_seen_time:
return 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") timestamp = last_seen_time.strftime("%Y-%m-%d %H:%M:%S")
log_activity(timestamp) log_activity(timestamp)
print(f"Logged activity: {timestamp}")
if __name__ == "__main__": if __name__ == "__main__":