74 lines
2.8 KiB
Python
74 lines
2.8 KiB
Python
|
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
|
||
|
from pyrogram import Client, filters
|
||
|
from defs.msg import gen_update_msg
|
||
|
from defs.source import update_data, compare, download
|
||
|
|
||
|
|
||
|
async def send_track_msg(file, track_msg):
|
||
|
if file:
|
||
|
await app.send_document(channel_id, file,
|
||
|
caption=track_msg.text,
|
||
|
file_name=track_msg.name,
|
||
|
force_document=True,
|
||
|
parse_mode="html",
|
||
|
reply_markup=track_msg.button)
|
||
|
else:
|
||
|
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)
|
||
|
if track_msg.url:
|
||
|
file = await download(track_msg.url, track_msg.name)
|
||
|
try:
|
||
|
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))
|
||
|
await send_track_msg(file, track_msg)
|
||
|
except ButtonUrlInvalid:
|
||
|
print(f"Send button error")
|
||
|
await app.send_document(channel_id, file,
|
||
|
caption=track_msg.text,
|
||
|
file_name=track_msg.name,
|
||
|
force_document=True,
|
||
|
parse_mode="html",)
|
||
|
except Exception as e:
|
||
|
traceback.print_exc()
|
||
|
try:
|
||
|
remove(file)
|
||
|
except FileNotFoundError:
|
||
|
pass
|
||
|
else:
|
||
|
try:
|
||
|
await send_track_msg(None, track_msg)
|
||
|
except FloodWait as e:
|
||
|
print(f"Send document flood - Sleep for {e.x} second(s)")
|
||
|
await sleep(uniform(0.5, 1.0))
|
||
|
await send_track_msg(None, track_msg)
|
||
|
except ButtonUrlInvalid:
|
||
|
print(f"Send button error")
|
||
|
await app.send_message(channel_id, track_msg.text, parse_mode="html",)
|
||
|
except Exception as e:
|
||
|
traceback.print_exc()
|
||
|
|
||
|
|
||
|
@Client.on_message(filters.incoming & filters.private & filters.chat(admin_id) &
|
||
|
filters.command(["force_update", ]))
|
||
|
async def force_update(bot: Client, message: Message):
|
||
|
await run_every_30_minute()
|