Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
anasty17
GitHub Repository: anasty17/mirror-leech-telegram-bot
Path: blob/master/bot/modules/file_selector.py
1623 views
1
from aiofiles.os import remove, path as aiopath
2
from asyncio import iscoroutinefunction
3
4
from .. import (
5
task_dict,
6
task_dict_lock,
7
user_data,
8
LOGGER,
9
sabnzbd_client,
10
)
11
from ..core.config_manager import Config
12
from ..core.torrent_manager import TorrentManager
13
from ..helper.ext_utils.bot_utils import (
14
bt_selection_buttons,
15
new_task,
16
)
17
from ..helper.ext_utils.status_utils import get_task_by_gid, MirrorStatus
18
from ..helper.telegram_helper.message_utils import (
19
send_message,
20
send_status_message,
21
delete_message,
22
)
23
24
25
@new_task
26
async def select(_, message):
27
if not Config.BASE_URL:
28
await send_message(message, "Base URL not defined!")
29
return
30
user_id = message.from_user.id
31
msg = message.text.split()
32
if len(msg) > 1:
33
gid = msg[1]
34
task = await get_task_by_gid(gid)
35
if task is None:
36
await send_message(message, f"GID: <code>{gid}</code> Not Found.")
37
return
38
elif reply_to_id := message.reply_to_message_id:
39
async with task_dict_lock:
40
task = task_dict.get(reply_to_id)
41
if task is None:
42
await send_message(message, "This is not an active task!")
43
return
44
elif len(msg) == 1:
45
msg = (
46
"Reply to an active /cmd which was used to start the download or add gid along with cmd\n\n"
47
+ "This command mainly for selection incase you decided to select files from already added torrent/nzb. "
48
+ "But you can always use /cmd with arg `s` to select files before download start."
49
)
50
await send_message(message, msg)
51
return
52
if (
53
Config.OWNER_ID != user_id
54
and task.listener.user_id != user_id
55
and (user_id not in user_data or not user_data[user_id].get("SUDO"))
56
):
57
await send_message(message, "This task is not for you!")
58
return
59
if not iscoroutinefunction(task.status):
60
await send_message(message, "The task have finished the download stage!")
61
return
62
if await task.status() not in [
63
MirrorStatus.STATUS_DOWNLOAD,
64
MirrorStatus.STATUS_PAUSED,
65
MirrorStatus.STATUS_QUEUEDL,
66
]:
67
await send_message(
68
message,
69
"Task should be in download or pause (incase message deleted by wrong) or queued status (incase you have used torrent or nzb file)!",
70
)
71
return
72
if task.name().startswith("[METADATA]") or task.name().startswith("Trying"):
73
await send_message(message, "Try after downloading metadata finished!")
74
return
75
76
try:
77
if not task.queued:
78
await task.update()
79
id_ = task.gid()
80
if task.listener.is_nzb:
81
await sabnzbd_client.pause_job(id_)
82
elif task.listener.is_qbit:
83
id_ = task.hash()
84
await TorrentManager.qbittorrent.torrents.stop([id_])
85
else:
86
try:
87
await TorrentManager.aria2.forcePause(id_)
88
except Exception as e:
89
LOGGER.error(
90
f"{e} Error in pause, this mostly happens after abuse aria2"
91
)
92
task.listener.select = True
93
except:
94
await send_message(message, "This is not a bittorrent or sabnzbd task!")
95
return
96
97
SBUTTONS = bt_selection_buttons(id_)
98
msg = "Your download paused. Choose files then press Done Selecting button to resume downloading."
99
await send_message(message, msg, SBUTTONS)
100
101
102
@new_task
103
async def confirm_selection(_, query):
104
user_id = query.from_user.id
105
data = query.data.split()
106
message = query.message
107
task = await get_task_by_gid(data[2])
108
if task is None:
109
await query.answer("This task has been cancelled!", show_alert=True)
110
await delete_message(message)
111
return
112
if user_id != task.listener.user_id:
113
await query.answer("This task is not for you!", show_alert=True)
114
elif data[1] == "pin":
115
await query.answer(data[3], show_alert=True)
116
elif data[1] == "done":
117
await query.answer()
118
id_ = data[3]
119
if hasattr(task, "seeding"):
120
if task.listener.is_qbit:
121
tor_info = (
122
await TorrentManager.qbittorrent.torrents.info(hashes=[id_])
123
)[0]
124
path = tor_info.content_path.rsplit("/", 1)[0]
125
res = await TorrentManager.qbittorrent.torrents.files(id_)
126
for f in res:
127
if f.priority == 0:
128
f_paths = [f"{path}/{f.name}", f"{path}/{f.name}.!qB"]
129
for f_path in f_paths:
130
if await aiopath.exists(f_path):
131
try:
132
await remove(f_path)
133
except:
134
pass
135
if not task.queued:
136
await TorrentManager.qbittorrent.torrents.start([id_])
137
else:
138
res = await TorrentManager.aria2.getFiles(id_)
139
for f in res:
140
if f["selected"] == "false" and await aiopath.exists(f["path"]):
141
try:
142
await remove(f["path"])
143
except:
144
pass
145
if not task.queued:
146
try:
147
await TorrentManager.aria2.unpause(id_)
148
except Exception as e:
149
LOGGER.error(
150
f"{e} Error in resume, this mostly happens after abuse aria2. Try to use select cmd again!"
151
)
152
elif task.listener.is_nzb:
153
await sabnzbd_client.resume_job(id_)
154
await send_status_message(message)
155
await delete_message(message)
156
else:
157
await delete_message(message)
158
await task.cancel_task()
159
160