import asyncio import logging import os from datetime import datetime, timedelta, timezone from db import init_db from collector import collect_messages from summarizer import summarize logging.basicConfig(level=logging.INFO, format="%(asctime)s %(name)s %(levelname)s %(message)s") log = logging.getLogger(__name__) CST = timezone(timedelta(hours=8)) SUMMARY_HOUR = int(os.environ.get("SUMMARY_HOUR", 23)) COLLECT_INTERVAL = 300 # 5 minutes async def main(): await init_db() log.info("Chat summarizer started (summary hour=%d CST)", SUMMARY_HOUR) last_summary_date = None while True: try: await collect_messages() except Exception as e: log.error("Collection error: %s", e) now = datetime.now(CST) today = now.strftime("%Y-%m-%d") if now.hour >= SUMMARY_HOUR and last_summary_date != today: try: await summarize(today) last_summary_date = today except Exception as e: log.error("Summary error: %s", e) await asyncio.sleep(COLLECT_INTERVAL) if __name__ == "__main__": asyncio.run(main())