Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
anasty17
GitHub Repository: anasty17/mirror-leech-telegram-bot
Path: blob/master/bot/modules/restart.py
1625 views
1
from sys import executable
2
from aiofiles import open as aiopen
3
from aiofiles.os import path as aiopath, remove
4
from asyncio import gather, create_subprocess_exec
5
from os import execl as osexecl
6
7
from .. import intervals, scheduler, sabnzbd_client, LOGGER
8
from ..helper.ext_utils.bot_utils import new_task
9
from ..helper.telegram_helper.message_utils import (
10
send_message,
11
delete_message,
12
)
13
from ..helper.ext_utils.db_handler import database
14
from ..helper.ext_utils.files_utils import clean_all
15
from ..helper.telegram_helper.button_build import ButtonMaker
16
from ..core.telegram_manager import TgClient
17
from ..core.config_manager import Config
18
from ..core.jdownloader_booter import jdownloader
19
from ..core.torrent_manager import TorrentManager
20
21
22
@new_task
23
async def restart_bot(_, message):
24
buttons = ButtonMaker()
25
buttons.data_button("Yes!", "botrestart confirm")
26
buttons.data_button("Cancel", "botrestart cancel")
27
button = buttons.build_menu(2)
28
await send_message(message, "Are you sure you want to restart the bot ?!", button)
29
30
31
async def send_incomplete_task_message(cid, msg_id, msg):
32
try:
33
if msg.startswith("Restarted Successfully!"):
34
await TgClient.bot.edit_message_text(
35
chat_id=cid,
36
message_id=msg_id,
37
text=msg,
38
)
39
await remove(".restartmsg")
40
else:
41
await TgClient.bot.send_message(
42
chat_id=cid,
43
text=msg,
44
disable_notification=True,
45
)
46
except Exception as e:
47
LOGGER.error(e)
48
49
50
async def restart_notification():
51
if await aiopath.isfile(".restartmsg"):
52
with open(".restartmsg") as f:
53
chat_id, msg_id = map(int, f)
54
else:
55
chat_id, msg_id = 0, 0
56
57
if Config.INCOMPLETE_TASK_NOTIFIER and Config.DATABASE_URL:
58
if notifier_dict := await database.get_incomplete_tasks():
59
for cid, data in notifier_dict.items():
60
msg = "Restarted Successfully!" if cid == chat_id else "Bot Restarted!"
61
for tag, links in data.items():
62
msg += f"\n\n{tag}: "
63
for index, link in enumerate(links, start=1):
64
msg += f" <a href='{link}'>{index}</a> |"
65
if len(msg.encode()) > 4000:
66
await send_incomplete_task_message(cid, msg_id, msg)
67
msg = ""
68
if msg:
69
await send_incomplete_task_message(cid, msg_id, msg)
70
71
if await aiopath.isfile(".restartmsg"):
72
try:
73
await TgClient.bot.edit_message_text(
74
chat_id=chat_id, message_id=msg_id, text="Restarted Successfully!"
75
)
76
except:
77
pass
78
await remove(".restartmsg")
79
80
81
@new_task
82
async def confirm_restart(_, query):
83
await query.answer()
84
data = query.data.split()
85
message = query.message
86
if data[1] == "confirm":
87
reply_to = message.reply_to_message
88
intervals["stopAll"] = True
89
restart_message = await send_message(reply_to, "Restarting...")
90
await delete_message(message)
91
await TgClient.stop()
92
if scheduler.running:
93
scheduler.shutdown(wait=False)
94
if qb := intervals["qb"]:
95
qb.cancel()
96
if jd := intervals["jd"]:
97
jd.cancel()
98
if nzb := intervals["nzb"]:
99
nzb.cancel()
100
if st := intervals["status"]:
101
for intvl in list(st.values()):
102
intvl.cancel()
103
await clean_all()
104
await TorrentManager.close_all()
105
if sabnzbd_client.LOGGED_IN:
106
await gather(
107
sabnzbd_client.pause_all(),
108
sabnzbd_client.delete_job("all", True),
109
sabnzbd_client.purge_all(True),
110
sabnzbd_client.delete_history("all", delete_files=True),
111
)
112
await sabnzbd_client.close()
113
if jdownloader.is_connected:
114
await gather(
115
jdownloader.device.downloadcontroller.stop_downloads(),
116
jdownloader.device.linkgrabber.clear_list(),
117
jdownloader.device.downloads.cleanup(
118
"DELETE_ALL",
119
"REMOVE_LINKS_AND_DELETE_FILES",
120
"ALL",
121
),
122
)
123
await jdownloader.close()
124
proc1 = await create_subprocess_exec(
125
"pkill",
126
"-9",
127
"-f",
128
"gunicorn|aria2c|qbittorrent-nox|ffmpeg|rclone|java|sabnzbdplus|7z|split",
129
)
130
proc2 = await create_subprocess_exec("python3", "update.py")
131
await gather(proc1.wait(), proc2.wait())
132
async with aiopen(".restartmsg", "w") as f:
133
await f.write(f"{restart_message.chat.id}\n{restart_message.id}\n")
134
osexecl(executable, executable, "-m", "bot")
135
else:
136
await delete_message(message)
137
138