diff --git a/README.md b/README.md index 152593b2..627e3b8a 100644 --- a/README.md +++ b/README.md @@ -674,4 +674,4 @@ ETH Address: 0xf798a8a1c72d593e16d8f3bb619ebd1a093c7309 ``` ------ +----- \ No newline at end of file diff --git a/bot/__main__.py b/bot/__main__.py index 32fe8c71..836711fb 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -234,13 +234,13 @@ async def restart_notification(): async def main(): + jdownloader.initiate() await gather( sync_to_async(clean_all), torrent_search.initiate_search_tools(), restart_notification(), telegraph.create_account(), rclone_serve_booter(), - jdownloader.intiate(), sync_to_async(start_aria2_listener, wait=False), ) create_help_buttons() diff --git a/bot/helper/common.py b/bot/helper/common.py index ffb6efd9..2ac62422 100644 --- a/bot/helper/common.py +++ b/bot/helper/common.py @@ -388,6 +388,7 @@ class TaskConfig: nextmsg, self.isQbit, self.isLeech, + self.isJd, self.sameDir, self.bulk, self.multiTag, diff --git a/bot/helper/ext_utils/files_utils.py b/bot/helper/ext_utils/files_utils.py index f2ac9c6c..98e20a4c 100644 --- a/bot/helper/ext_utils/files_utils.py +++ b/bot/helper/ext_utils/files_utils.py @@ -2,7 +2,7 @@ from os import walk, path as ospath, makedirs from aiofiles.os import remove, path as aiopath, listdir, rmdir from aioshutil import rmtree as aiormtree from magic import Magic -from re import split as re_split, I, search as re_search +from re import split as re_split, I, search as re_search, escape from subprocess import run as srun from sys import exit as sexit from shutil import rmtree @@ -191,5 +191,5 @@ async def join_files(path): LOGGER.info("Join Completed!") for res in results: for file_ in files: - if re_search(rf"{res}\.0[0-9]+$", file_): + if re_search(rf"{escape(res)}\.0[0-9]+$", file_): await remove(f"{path}/{file_}") diff --git a/bot/helper/ext_utils/help_messages.py b/bot/helper/ext_utils/help_messages.py index 5de12374..e8ebdb4c 100644 --- a/bot/helper/ext_utils/help_messages.py +++ b/bot/helper/ext_utils/help_messages.py @@ -36,7 +36,7 @@ thumb = """Thumbnail for current task: -t /cmd link -t tg-message-link(doc or photo)""" -split_size = """Split size for current task: -t +split_size = """Split size for current task: -sp /cmd link -sp (500mb or 2gb or 4000000000) Note: Only mb and gb are supported or write in bytes without unit!""" diff --git a/bot/helper/ext_utils/jdownloader_booter.py b/bot/helper/ext_utils/jdownloader_booter.py index a94fb981..40faacbe 100644 --- a/bot/helper/ext_utils/jdownloader_booter.py +++ b/bot/helper/ext_utils/jdownloader_booter.py @@ -1,4 +1,9 @@ from myjdapi import Myjdapi +from json import dump +from time import sleep +from asyncio import sleep as aiosleep +from random import randint +from aiofiles.os import listdir from myjdapi.exception import ( MYJDException, MYJDAuthFailedException, @@ -6,13 +11,13 @@ from myjdapi.exception import ( MYJDEmailInvalidException, MYJDErrorEmailNotConfirmedException, ) -from json import dump -from time import sleep -from random import randint -from aiofiles.os import listdir -from bot import config_dict, LOGGER -from bot.helper.ext_utils.bot_utils import cmd_exec, new_task +from bot import config_dict, LOGGER, jd_lock, Intervals +from bot.helper.ext_utils.bot_utils import ( + cmd_exec, + new_task, + sync_to_async, +) class JDownloader: @@ -21,17 +26,28 @@ class JDownloader: self._username = "" self._password = "" self._device_name = "" + self.error = "JDownloader Credentials not provided!" self.device = None self.myjd.set_app_key("mltb") - async def intiate(self): + @new_task + async def initiate(self): self.boot() + await aiosleep(10) + await self.start() + + async def start(self): + async with jd_lock: + await sync_to_async(self.connect) + if self.device is not None: + await self.keepJdAlive() @new_task async def boot(self): await cmd_exec(["pkill", "-9", "java"]) self.device = None - if config_dict["JD_EMAIL"]: + if config_dict["JD_EMAIL"] and config_dict["JD_PASS"]: + self.error = "Connecting... Try agin after couple of seconds" self._device_name = f"{randint(0, 1000)}" logs = await listdir("/JDownloader/logs") if len(logs) > 2: @@ -55,31 +71,57 @@ class JDownloader: cmd = "java -Dsun.jnu.encoding=UTF-8 -Dfile.encoding=UTF-8 -Djava.awt.headless=true -jar /JDownloader/JDownloader.jar" _, __, code = await cmd_exec(cmd, shell=True) if code != -9: - await self.intiate() + self.boot() def connect(self): LOGGER.info(f"Connecting to JDownloader...") try: self.myjd.connect(config_dict["JD_EMAIL"], config_dict["JD_PASS"]) - sleep(0.5) + while True: + self.myjd.update_devices() + devices = self.myjd.list_devices() + if devices: + for device in devices: + if self._device_name == device["name"]: + break + else: + continue + else: + continue + break self.device = self.myjd.get_device(f"{self._device_name}") self.device.enable_direct_connection() - self.device.linkgrabber.query_packages() + self.device.linkgrabber.clear_list() + LOGGER.info(f"JDownloader is Connected!") except ( MYJDAuthFailedException, MYJDEmailForbiddenException, MYJDEmailInvalidException, MYJDErrorEmailNotConfirmedException, ) as err: - LOGGER.info(f"Failed to connect with jdownloader!ERROR: {err}".strip()) + self.error = f"{err}".strip() + LOGGER.info(f"Failed to connect with jdownloader! ERROR: {self.error}") self.device = None - raise err + return except MYJDException as e: + self.error = f"{e}".strip() LOGGER.info( - f"Failed to connect with jdownloader! Retrying... ERROR: {e}".strip() + f"Failed to connect with jdownloader! Retrying... ERROR: {self.error}" ) sleep(10) return self.connect() + @new_task + async def keepJdAlive(self): + while True: + await aiosleep(30) + if self.device is not None: + break + if not Intervals["jd"]: + try: + await sync_to_async(self.device.action, "/device/ping") + except: + pass + jdownloader = JDownloader() diff --git a/bot/helper/ext_utils/media_utils.py b/bot/helper/ext_utils/media_utils.py index a76119e7..6f96b20b 100644 --- a/bot/helper/ext_utils/media_utils.py +++ b/bot/helper/ext_utils/media_utils.py @@ -17,10 +17,10 @@ def getSplitSizeBytes(size): size = size.lower() if size.endswith("mb"): size = size.split("mb")[0] - size = float(size) * 1048576 + size = int(float(size) * 1048576) elif size.endswith("gb"): size = size.split("gb")[0] - size = float(size) * 1073741824 + size = int(float(size) * 1073741824) else: size = 0 return size diff --git a/bot/helper/mirror_utils/download_utils/jd_download.py b/bot/helper/mirror_utils/download_utils/jd_download.py index a25f2087..47b01d53 100644 --- a/bot/helper/mirror_utils/download_utils/jd_download.py +++ b/bot/helper/mirror_utils/download_utils/jd_download.py @@ -24,7 +24,6 @@ from bot import ( queue_dict_lock, jd_lock, jd_downloads, - config_dict, ) @@ -85,15 +84,8 @@ class JDownloaderHelper: async def add_jd_download(listener, path): async with jd_lock: if jdownloader.device is None: - if config_dict["JD_EMAIL"] and config_dict["JD_PASS"]: - await sync_to_async(jdownloader.connect) - else: - await listener.onDownloadError("NO JDownloader credentials!") - return - await retry_function( - sync_to_async, - jdownloader.device.linkgrabber.clear_list, - ) + await listener.onDownloadError(jdownloader.error) + return if not jd_downloads and ( odl := await retry_function( @@ -207,16 +199,16 @@ async def add_jd_download(listener, path): exists = True break + if not exists: + await listener.onDownloadError("This Download have been removed manually!") + return + await retry_function( sync_to_async, jdownloader.device.downloads.force_download, package_ids=[gid], ) - if not exists: - await listener.onDownloadError("This Download have been removed manually!") - return - async with task_dict_lock: task_dict[listener.mid] = JDownloaderStatus(listener, f"{gid}") diff --git a/bot/helper/mirror_utils/rclone_utils/transfer.py b/bot/helper/mirror_utils/rclone_utils/transfer.py index 49121f54..3c5e8880 100644 --- a/bot/helper/mirror_utils/rclone_utils/transfer.py +++ b/bot/helper/mirror_utils/rclone_utils/transfer.py @@ -405,7 +405,7 @@ class RcloneTransferHelper: f"/{self._listener.name}" if dst_path else self._listener.name ) - cmd = f'rclone link --config {config_path} "{destination}"' + cmd = f"rclone link --config {config_path} '{destination}'" res, err, code = await cmd_exec(cmd, shell=True) if self._is_cancelled: diff --git a/bot/modules/bot_settings.py b/bot/modules/bot_settings.py index 205fbd05..c69af8f1 100644 --- a/bot/modules/bot_settings.py +++ b/bot/modules/bot_settings.py @@ -9,7 +9,6 @@ from dotenv import load_dotenv from time import time from io import BytesIO from aioshutil import rmtree -from myjdapi.exception import MYJDException from bot.helper.telegram_helper.filters import CustomFilters from bot.helper.telegram_helper.bot_commands import BotCommands @@ -311,23 +310,15 @@ async def edit_qbit(_, message, pre_message, key): async def sync_jdownloader(): if DATABASE_URL: - if jdownloader.device is None: - try: - await sync_to_async(jdownloader.connect) - except (Exception, MYJDException) as e: - await sendMessage(f"{e}".strip()) - return + if jdownloader.device is not None: await sync_to_async(jdownloader.device.system.exit_jd) - if await aiopath.exists("cfg.zip"): - await remove("cfg.zip") - await ( - await create_subprocess_exec("7z", "a", "cfg.zip", "/JDownloader/cfg") - ).wait() - await DbManger().update_private_file("cfg.zip") - try: - await sync_to_async(jdownloader.connect) - except (Exception, MYJDException) as e: - await sendMessage(f"{e}".strip()) + if await aiopath.exists("cfg.zip"): + await remove("cfg.zip") + await ( + await create_subprocess_exec("7z", "a", "cfg.zip", "/JDownloader/cfg") + ).wait() + await DbManger().update_private_file("cfg.zip") + await jdownloader.start() async def update_private_file(_, message, pre_message): @@ -516,7 +507,8 @@ async def edit_bot_settings(client, query): elif data[2] == "INCOMPLETE_TASK_NOTIFIER" and DATABASE_URL: await DbManger().trunc_table("tasks") elif data[2] in ["JD_EMAIL", "JD_PASS"]: - jdownloader.device = None + await sleep(3) + await jdownloader.start() config_dict[data[2]] = value await update_buttons(message, "var") if DATABASE_URL: