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