Files
OqeeAdWatch/main.py
√(noham)² 4d4c470410 Refactor core logic and add visualization tools
Moved database and scraping logic to utils/scrap.py for modularity. Added utils/visualizer.py for channel-level ad break analysis and plotting. Introduced .env.example for webhook configuration and updated main.py to support webhook heartbeats and improved logging. Updated README with new usage and visualization instructions. Added matplotlib and python-dotenv as dependencies.
2025-11-30 14:45:43 +01:00

85 lines
2.4 KiB
Python

"""Background ad-break collector that stores anti-adskipping periods in SQLite."""
from datetime import datetime
import logging
import os
import time
import requests
from dotenv import load_dotenv
from utils.scrap import get_connection, init_db, run_collection_cycle, POLL_INTERVAL_SECONDS
load_dotenv()
logger = logging.getLogger(__name__)
HEARTBEAT_INTERVAL_SECONDS = 24 * 60 * 60 # 24 hours
def notify_webhook(message: str) -> bool:
"""Send a notification to the GitHub webhook if configured."""
webhook_url = os.getenv("WEBHOOK_URL")
if not webhook_url:
logger.debug("No WEBHOOK_URL configured, skipping notification")
return False
try:
payload = {"content": message}
response = requests.post(webhook_url, json=payload, timeout=10)
response.raise_for_status()
logger.info("Webhook notification sent successfully")
return True
except requests.RequestException as exc:
logger.warning("Failed to send webhook notification: %s", exc)
return False
def main() -> None:
"""Entrypoint for CLI execution."""
env_level = os.getenv("LOGLEVEL", "INFO").upper()
log_level = getattr(logging, env_level, logging.INFO)
logging.basicConfig(
level=log_level,
format="%(asctime)s [%(levelname)s] %(message)s",
)
conn = get_connection()
init_db(conn)
last_heartbeat = 0.0
try:
while True:
cycle_started = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
logger.info("=== New collection started at %s ===", cycle_started)
try:
run_collection_cycle(conn)
except Exception: # pylint: disable=broad-exception-caught
logger.exception("Critical error during collection")
# Send heartbeat notification every 24 hours
now = time.time()
if now - last_heartbeat >= HEARTBEAT_INTERVAL_SECONDS:
notify_webhook(
f"🟢 OqeeAdWatch is running - heartbeat at {cycle_started}"
)
last_heartbeat = now
logger.info(
"Pausing for %s minutes before the next collection...",
POLL_INTERVAL_SECONDS // 60,
)
time.sleep(POLL_INTERVAL_SECONDS)
except KeyboardInterrupt:
logger.info("Shutdown requested by user. Closing...")
finally:
conn.close()
if __name__ == "__main__":
main()