From ee746d0abe7a0463d51c29f44dab42908874ead9 Mon Sep 17 00:00:00 2001 From: Cailean Finn Date: Sun, 10 May 2026 13:26:11 +0100 Subject: [PATCH] removed depreceated code, and logs --- README.md | 2 +- __pycache__/agent.cpython-312.pyc | Bin 2130 -> 2069 bytes agent.py | 3 +-- bot.py | 39 +++++++++++++++++++++++------- main.py | 9 +++++++ 5 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 main.py diff --git a/README.md b/README.md index a7e9129..b70a061 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ POCKETBASE_ADMIN_PASSWORD=secret Start the bot with the project's entrypoint (example): ```bash -python bot.py +python main.py ``` The bot listens for commands: diff --git a/__pycache__/agent.cpython-312.pyc b/__pycache__/agent.cpython-312.pyc index 2df0bf8803fda22d606bfbc27a5d0d41c39db667..9eda62ac349dbb1a96ba4fc251e7aa409212c505 100644 GIT binary patch delta 176 zcmca4Fjau}G%qg~0}!;7Gi3R11^Hn`p37wxa@tZnd^ti|TWF3G^B_VbI(0ojLygq^II52*?`nKBTl{VaOH8+%XgMSNvasrPUeyM-8~mdEwVkyM z9v}r8E&h`~u)48JGw`YX{318mmQCBr9B5{fBZIRr<3T1yCpDIX%#PeZ&LMs=Ck=)} zs!SlZhOn~$^C4>iXKv=h+^j(MVF6}m;mz~e5*Wqg83kq}e_#MoD~i8>s3H@fi2xr) BH&Flp diff --git a/agent.py b/agent.py index 301b6c3..e7b1480 100644 --- a/agent.py +++ b/agent.py @@ -47,10 +47,9 @@ async def parse_page(content: str, entry_type: str = "opportunity"): # 1. Run the agent (which returns a string) print(f"Parsing {entry_type}...") - print(content) + # print(content) result = await agent.run(content) raw_text = result.output - print(raw_text) # 2. Clean the string # We remove the markdown decorators so json.loads doesn't crash diff --git a/bot.py b/bot.py index 4799ddf..721ea48 100644 --- a/bot.py +++ b/bot.py @@ -1,6 +1,7 @@ import os import asyncio import logging +from contextlib import suppress from dotenv import load_dotenv from functools import wraps from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup @@ -12,6 +13,7 @@ from database import upload_entry from scraper import get_clean_content load_dotenv() +logging.getLogger("httpx").setLevel(logging.WARNING) # Configuration TOKEN = os.getenv("TG_TOKEN") @@ -260,20 +262,39 @@ async def button_handler(update: Update, context: ContextTypes.DEFAULT_TYPE): else: await query.edit_message_text("🗑️ Discarded.") -# --- Main Entry --- -if __name__ == '__main__': +# Main Entry Point +async def _main(): application = ApplicationBuilder().token(TOKEN).build() - - # Add Handlers + application.add_handler(CommandHandler("start", start)) application.add_handler(CommandHandler("op", handle_opportunity)) application.add_handler(CommandHandler("ev", handle_event)) application.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), handle_followup_text)) application.add_handler(CallbackQueryHandler(button_handler)) - # Start the worker thread - loop = asyncio.get_event_loop() - loop.create_task(worker()) + async with application: + await application.initialize() + await application.start() - print("🤖 Bot is running...") - application.run_polling() \ No newline at end of file + worker_task = asyncio.create_task(worker()) + print("🤖 Bot is running...") + + await application.updater.start_polling() + + # Keep running until interrupted + await asyncio.Event().wait() + + # Graceful shutdown + await application.updater.stop() + worker_task.cancel() + with suppress(asyncio.CancelledError): + await worker_task + + await application.stop() + + +if __name__ == '__main__': + import sys + if sys.platform == 'win32': + asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) + asyncio.run(_main()) \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..a6110a0 --- /dev/null +++ b/main.py @@ -0,0 +1,9 @@ +"""Entrypoint to run the bot. + +This simply runs `bot.py` as a script so you can start the bot with +`python main.py`. +""" +import runpy + +if __name__ == '__main__': + runpy.run_path('bot.py', run_name='__main__')