feat: add staleness detection and notification for monitored content with timestamp tracking

This commit is contained in:
Rodrigo Verdiani 2025-12-11 23:37:58 -03:00
parent d5239a9d78
commit 60a14e10a5

39
main.py
View File

@ -8,10 +8,12 @@ from bs4 import BeautifulSoup
URL_TO_MONITOR = "https://univesp.br/vestibular" URL_TO_MONITOR = "https://univesp.br/vestibular"
HASH_FILE = os.path.join("/data", "last_hash.txt")
NTFY_URL = os.getenv("NTFY_URL") NTFY_URL = os.getenv("NTFY_URL")
FLARESOLVERR_URL = os.getenv("FLARESOLVERR_URL") FLARESOLVERR_URL = os.getenv("FLARESOLVERR_URL")
MONITOR_SELECTOR = os.getenv("MONITOR_SELECTOR", "div.conteudo") MONITOR_SELECTOR = os.getenv("MONITOR_SELECTOR", "div.conteudo")
HASH_FILE = os.path.join("/data", "last_hash.txt")
LAST_CHANGE_FILE = os.path.join("/data", "last_change_timestamp.txt")
LAST_NOTIFY_FILE = os.path.join("/data", "last_staleness_notification.txt")
def send_notification(message): def send_notification(message):
@ -91,6 +93,10 @@ def check_for_updates():
with open(HASH_FILE, 'w') as f: with open(HASH_FILE, 'w') as f:
f.write(current_hash) f.write(current_hash)
# Update last change timestamp
with open(LAST_CHANGE_FILE, 'w') as f:
f.write(str(datetime.now().timestamp()))
else: else:
print(f"[{datetime.now()}] No changes found.") print(f"[{datetime.now()}] No changes found.")
else: else:
@ -98,8 +104,39 @@ def check_for_updates():
with open(HASH_FILE, 'w') as f: with open(HASH_FILE, 'w') as f:
f.write(current_hash) f.write(current_hash)
# Initialize last change timestamp
with open(LAST_CHANGE_FILE, 'w') as f:
f.write(str(datetime.now().timestamp()))
return return
# Check for staleness (no changes for > 7 days)
if os.path.exists(LAST_CHANGE_FILE):
try:
with open(LAST_CHANGE_FILE, 'r') as f:
last_change_ts = float(f.read().strip())
time_since_change = datetime.now().timestamp() - last_change_ts
if time_since_change > 7 * 24 * 60 * 60: # 7 days in seconds
# Check if we recently notified about this
should_notify = True
if os.path.exists(LAST_NOTIFY_FILE):
with open(LAST_NOTIFY_FILE, 'r') as f:
last_notify_ts = float(f.read().strip())
if (datetime.now().timestamp() - last_notify_ts) < 7 * 24 * 60 * 60:
should_notify = False
if should_notify:
print(f"[{datetime.now()}] ⚠️ Updates are stale (> 7 days). Sends notification.")
send_notification("⚠️ No changes detected for over 7 days. Please check manually.")
with open(LAST_NOTIFY_FILE, 'w') as f:
f.write(str(datetime.now().timestamp()))
except Exception as e:
print(f"[{datetime.now()}] Error checking staleness: {e}")
def main(): def main():