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