import contextlib import traceback from asyncio import sleep from os import remove from random import uniform from pyrogram.errors import FloodWait, ButtonUrlInvalid from pyrogram.types import Message from ci import app, scheduler, channel_id, admin_id, sqlite from pyrogram import Client, filters from defs.msg import gen_update_msg from defs.source import update_data, compare, download from defs.subs import send_to_subscribes async def send_track_msg(file, track_msg) -> Message: if file: return await app.send_document(channel_id, file, caption=track_msg.text, force_document=True, parse_mode="html", reply_markup=track_msg.button) else: return await app.send_message(channel_id, track_msg.text, parse_mode="html", reply_markup=track_msg.button) @scheduler.scheduled_job("cron", minute="*/30", id="0") async def run_every_30_minute(): await update_data() need_update = compare() for i in need_update: track_msg = gen_update_msg(i) msg = None if track_msg.url: try: file, url = await download(track_msg.url, track_msg.name, i.name) track_msg.url = url try: msg = await send_track_msg(file, track_msg) except FloodWait as e: print(f"Send document flood - Sleep for {e.x} second(s)") await sleep(uniform(0.5, 1.0)) msg = await send_track_msg(file, track_msg) except ButtonUrlInvalid: print(f"Send button error") msg = await app.send_document(channel_id, file, caption=track_msg.text, force_document=True, parse_mode="html", ) except Exception as e: traceback.print_exc() with contextlib.suppress(FileNotFoundError): remove(file) except FileNotFoundError: track_msg.url = None if not track_msg.url: try: msg = await send_track_msg(None, track_msg) except FloodWait as e: print(f"Send document flood - Sleep for {e.value} second(s)") await sleep(e.value + uniform(0.5, 1.0)) msg = await send_track_msg(None, track_msg) except ButtonUrlInvalid: print(f"Send button error") msg = await app.send_message(channel_id, track_msg.text, parse_mode="html", ) except Exception as e: traceback.print_exc() await sleep(uniform(0.5, 2.0)) data_ = sqlite.get(i.name, {"msg_link": ""}) if msg: data_["msg_link"] = msg.link else: data_["msg_link"] = "https://t.me/lsposed_Modules_Updates_Tracker" sqlite[i.name] = data_ await send_to_subscribes(i) await sleep(uniform(0.5, 2.0)) @Client.on_message(filters.incoming & filters.private & filters.chat(admin_id) & filters.command(["force_update", ])) async def force_update(_: Client, message: Message): await run_every_30_minute() await message.reply("Force update complete OK!")