diff --git a/bot/__init__.py b/bot/__init__.py index 8b26f2d..dbf8905 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -73,7 +73,7 @@ def get_client() -> qba.TorrentsAPIMixIn: qb_client = qba.Client(host="localhost", port=8090, username="admin", password="adminadmin") try: qb_client.auth_log_in() - qb_client.application.set_preferences({"disk_cache":64, "incomplete_files_ext":True, "max_connec":10000, "max_connec_per_torrent":1000, "async_io_threads":32, "preallocate_all":True, "upnp":True, "dl_limit":-1, "up_limit":-1, "dht":True, "pex":True, "lsd":True, "encryption":0, "queueing_enabled":True, "max_active_downloads":15, "max_active_torrents":50, "dont_count_slow_torrents":True, "bittorrent_protocol":0, "recheck_completed_torrents":True, "auto_delete_mode":True, "enable_multi_connections_from_same_ip":True, "slow_torrent_dl_rate_threshold":100,"slow_torrent_inactive_timer":600}) + qb_client.application.set_preferences({"disk_cache":64, "incomplete_files_ext":True, "max_connec":10000, "max_connec_per_torrent":1000, "async_io_threads":32, "preallocate_all":True, "upnp":True, "dl_limit":-1, "up_limit":-1, "dht":True, "pex":True, "lsd":True, "encryption":0, "queueing_enabled":True, "max_active_downloads":15, "max_active_torrents":50, "dont_count_slow_torrents":True, "bittorrent_protocol":0, "recheck_completed_torrents":True, "enable_multi_connections_from_same_ip":True, "slow_torrent_dl_rate_threshold":100,"slow_torrent_inactive_timer":600}) return qb_client except qba.LoginFailed as e: LOGGER.error(str(e)) diff --git a/bot/__main__.py b/bot/__main__.py index 1bba6f2..3437c7e 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -92,6 +92,8 @@ def bot_help(update, context): /{BotCommands.TarMirrorCommand} [download_url][magnet_link]: Start mirroring and upload the archived (.tar) version of the download +/{BotCommands.ZipMirrorCommand} [download_url][magnet_link]: Start mirroring and upload the archived (.zip) version of the download + /{BotCommands.UnzipMirrorCommand} [download_url][magnet_link]: Starts mirroring and if downloaded file is any archive, extracts it to Google Drive /{BotCommands.CloneCommand} [drive_url]: Copy file/folder to Google Drive @@ -150,6 +152,8 @@ def bot_help(update, context): /{BotCommands.TarMirrorCommand} [download_url][magnet_link]: Start mirroring and upload the archived (.tar) version of the download +/{BotCommands.ZipMirrorCommand} [download_url][magnet_link]: Start mirroring and upload the archived (.zip) version of the download + /{BotCommands.UnzipMirrorCommand} [download_url][magnet_link]: Starts mirroring and if downloaded file is any archive, extracts it to Google Drive /{BotCommands.CloneCommand} [drive_url]: Copy file/folder to Google Drive @@ -184,6 +188,7 @@ botcmds = [ (f'{BotCommands.MirrorCommand}', 'Start Mirroring'), (f'{BotCommands.TarMirrorCommand}','Start mirroring and upload as .tar'), (f'{BotCommands.UnzipMirrorCommand}','Extract files'), + (f'{BotCommands.ZipMirrorCommand}','Start mirroring and upload as .zip'), (f'{BotCommands.CloneCommand}','Copy file/folder to Drive'), (f'{BotCommands.CountCommand}','Count file/folder of Drive link'), (f'{BotCommands.DeleteCommand}','Delete file from Drive'), diff --git a/bot/helper/ext_utils/fs_utils.py b/bot/helper/ext_utils/fs_utils.py index 32b7849..a7e1a4b 100644 --- a/bot/helper/ext_utils/fs_utils.py +++ b/bot/helper/ext_utils/fs_utils.py @@ -53,7 +53,7 @@ def get_path_size(path): def tar(org_path): tar_path = org_path + ".tar" - path = pathlib.PurePath(org_path) + #path = pathlib.PurePath(org_path) LOGGER.info(f'Tar: orig_path: {org_path}, tar_path: {tar_path}') tar = tarfile.open(tar_path, "w") tar.add(org_path, arcname=os.path.basename(org_path)) @@ -61,6 +61,15 @@ def tar(org_path): return tar_path +def zip(name, path): + root_dir = os.path.dirname(path) + base_dir = os.path.basename(path.strip(os.sep)) + zip_file = shutil.make_archive(name, "zip", root_dir, base_dir) + zip_path = shutil.move(zip_file, root_dir) + LOGGER.info(f"Zip: {zip_path}") + return zip_path + + def get_base_name(orig_path: str): if orig_path.endswith(".tar.bz2"): return orig_path.replace(".tar.bz2", "") diff --git a/bot/helper/telegram_helper/bot_commands.py b/bot/helper/telegram_helper/bot_commands.py index 761a152..e7e6812 100644 --- a/bot/helper/telegram_helper/bot_commands.py +++ b/bot/helper/telegram_helper/bot_commands.py @@ -4,6 +4,7 @@ class _BotCommands: self.MirrorCommand = 'mirror' self.UnzipMirrorCommand = 'unzipmirror' self.TarMirrorCommand = 'tarmirror' + self.ZipMirrorCommand = 'zipmirror' self.CancelMirror = 'cancel' self.CancelAllCommand = 'cancelall' self.ListCommand = 'list' diff --git a/bot/modules/mirror.py b/bot/modules/mirror.py index 5be7dfc..b4a98cd 100644 --- a/bot/modules/mirror.py +++ b/bot/modules/mirror.py @@ -35,11 +35,12 @@ ariaDlManager = AriaDownloadHelper() ariaDlManager.start_listener() class MirrorListener(listeners.MirrorListeners): - def __init__(self, bot, update, pswd, isTar=False, extract=False, qbit=False): + def __init__(self, bot, update, pswd, isTar=False, extract=False, isZip=False, isQbit=False): super().__init__(bot, update) self.isTar = isTar self.extract = extract - self.qbit = qbit + self.isZip = isZip + self.isQbit = isQbit self.pswd = pswd def onDownloadStarted(self): @@ -66,14 +67,17 @@ class MirrorListener(listeners.MirrorListeners): name = download.name() gid = download.gid() size = download.size_raw() - if name is None or self.qbit: # when pyrogram's media.file_name is of NoneType + if name is None or self.isQbit: # when pyrogram's media.file_name is of NoneType name = os.listdir(f'{DOWNLOAD_DIR}{self.uid}')[0] m_path = f'{DOWNLOAD_DIR}{self.uid}/{name}' if self.isTar: try: with download_dict_lock: download_dict[self.uid] = TarStatus(name, m_path, size) - path = fs_utils.tar(m_path) + if self.isZip: + path = fs_utils.zip(name, m_path) + else: + path = fs_utils.tar(m_path) except FileNotFoundError: LOGGER.info('File to archive not found!') self.onUploadError('Internal error occurred!!') @@ -225,20 +229,28 @@ class MirrorListener(listeners.MirrorListeners): else: update_all_messages() -def _mirror(bot, update, isTar=False, extract=False): +def _mirror(bot, update, isTar=False, extract=False, isZip=False, isQbit=False): mesg = update.message.text.split('\n') message_args = mesg[0].split(' ') name_args = mesg[0].split('|') - qbit = False qbitsel = False try: link = message_args[1] if link == "qb" or link == "qbs": - qbit = True + isQbit = True if link == "qbs": qbitsel = True link = message_args[2] - print(link) + if bot_utils.is_url(link) and link.endswith(".torrent"): + resp = requests.get(link) + if resp.status_code == 200: + file_name = link.split('/')[-1].replace(" ", "_") + with open(file_name, "wb") as f: + f.write(resp.content) + link = f"/usr/src/app/{file_name}" + else: + sendMessage("ERROR: link got HTTP response:" + resp.status_code, bot, update) + return if link.startswith("|") or link.startswith("pswd: "): link = '' except IndexError: @@ -263,7 +275,8 @@ def _mirror(bot, update, isTar=False, extract=False): if pswd is not None: pswd = pswd.groups() pswd = " ".join(pswd) - LOGGER.info(link) + if link != '': + LOGGER.info(link) link = link.strip() reply_to = update.message.reply_to_message if reply_to is not None: @@ -277,13 +290,13 @@ def _mirror(bot, update, isTar=False, extract=False): if not bot_utils.is_url(link) and not bot_utils.is_magnet(link) or len(link) == 0: if file is not None: if file.mime_type != "application/x-bittorrent": - listener = MirrorListener(bot, update, pswd, isTar, extract) + listener = MirrorListener(bot, update, pswd, isTar, extract, isZip) tg_downloader = TelegramDownloadHelper(listener) ms = update.message tg_downloader.add_download(ms, f'{DOWNLOAD_DIR}{listener.uid}/', name) return else: - if qbit: + if isQbit: file.get_file().download(custom_path=f"/usr/src/app/{file.file_name}") link = f"/usr/src/app/{file.file_name}" else: @@ -292,19 +305,19 @@ def _mirror(bot, update, isTar=False, extract=False): if not bot_utils.is_url(link) and not bot_utils.is_magnet(link): sendMessage('No download source provided', bot, update) return + if not os.path.exists(link) and not bot_utils.is_mega_link(link) and not bot_utils.is_gdrive_link(link) and not bot_utils.is_magnet(link): + try: + link = direct_link_generator(link) + except DirectDownloadLinkException as e: + LOGGER.info(e) + if "ERROR:" in str(e): + sendMessage(f"{e}", bot, update) + return + if "Youtube" in str(e): + sendMessage(f"{e}", bot, update) + return - try: - link = direct_link_generator(link) - except DirectDownloadLinkException as e: - LOGGER.info(e) - if "ERROR:" in str(e): - sendMessage(f"{e}", bot, update) - return - if "Youtube" in str(e): - sendMessage(f"{e}", bot, update) - return - - listener = MirrorListener(bot, update, pswd, isTar, extract, qbit) + listener = MirrorListener(bot, update, pswd, isTar, extract, isZip, isQbit) if bot_utils.is_gdrive_link(link): if not isTar and not extract: @@ -339,7 +352,7 @@ def _mirror(bot, update, isTar=False, extract=False): mega_dl = MegaDownloadHelper() mega_dl.add_download(link, f'{DOWNLOAD_DIR}{listener.uid}/', listener) - elif qbit and (bot_utils.is_magnet(link) or os.path.exists(link)): + elif isQbit and (bot_utils.is_magnet(link) or os.path.exists(link)): qbit = qbittorrent() qbit.add_torrent(link, f'{DOWNLOAD_DIR}{listener.uid}/', listener, qbitsel) @@ -359,6 +372,8 @@ def tar_mirror(update, context): def unzip_mirror(update, context): _mirror(context.bot, update, extract=True) +def zip_mirror(update, context): + _mirror(context.bot, update, True, isZip=True) mirror_handler = CommandHandler(BotCommands.MirrorCommand, mirror, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) @@ -366,6 +381,9 @@ tar_mirror_handler = CommandHandler(BotCommands.TarMirrorCommand, tar_mirror, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) unzip_mirror_handler = CommandHandler(BotCommands.UnzipMirrorCommand, unzip_mirror, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) +zip_mirror_handler = CommandHandler(BotCommands.ZipMirrorCommand, zip_mirror, + filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True) dispatcher.add_handler(mirror_handler) dispatcher.add_handler(tar_mirror_handler) dispatcher.add_handler(unzip_mirror_handler) +dispatcher.add_handler(zip_mirror_handler) diff --git a/wserver.py b/wserver.py index 842462a..49c856e 100644 --- a/wserver.py +++ b/wserver.py @@ -137,11 +137,11 @@ async def list_torrent_contents(request): gets = request.query if not "pin_code" in gets.keys(): - rend_page = code_page.replace("{form_url}",f"/slam/files/{torr}") - return web.Response(text=rend_page,content_type='text/html') + rend_page = code_page.replace("{form_url}", f"/slam/files/{torr}") + return web.Response(text=rend_page, content_type='text/html') - client = qba.Client(host="localhost",port="8090",username="admin",password="adminadmin") + client = qba.Client(host="localhost", port="8090", username="admin", password="adminadmin") client.auth_log_in() try: res = client.torrents_files(torrent_hash=torr) @@ -165,12 +165,12 @@ async def list_torrent_contents(request): par = nodes.make_tree(res) cont = ["",0] - nodes.create_list(par,cont) + nodes.create_list(par, cont) - rend_page = page.replace("{My_content}",cont[0]) - rend_page = rend_page.replace("{form_url}",f"/slam/files/{torr}?pin_code={pincode}") + rend_page = page.replace("{My_content}", cont[0]) + rend_page = rend_page.replace("{form_url}", f"/slam/files/{torr}?pin_code={pincode}") client.auth_log_out() - return web.Response(text=rend_page,content_type='text/html') + return web.Response(text=rend_page, content_type='text/html') async def re_verfiy(paused, resumed, client, torr): @@ -204,25 +204,23 @@ async def re_verfiy(paused, resumed, client, torr): if not verify: - LOGGER.info("Reverification Failed :- correcting stuff") - # reconnect and issue the request again + LOGGER.error("Reverification Failed, correcting stuff...") client.auth_log_out() - client = qba.Client(host="localhost",port="8090",username="admin",password="adminadmin") + client = qba.Client(host="localhost", port="8090", username="admin", password="adminadmin") client.auth_log_in() try: - client.torrents_file_priority(torrent_hash=torr,file_ids=paused,priority=0) + client.torrents_file_priority(torrent_hash=torr, file_ids=paused, priority=0) except: LOGGER.error("Errored in reverification paused") try: - client.torrents_file_priority(torrent_hash=torr,file_ids=resumed,priority=1) + client.torrents_file_priority(torrent_hash=torr, file_ids=resumed, priority=1) except: LOGGER.error("Errored in reverification resumed") client.auth_log_out() else: break k += 1 - if k >= 2: - # avoid an infite loop here + if k >= 4: return False return True @@ -232,7 +230,7 @@ async def re_verfiy(paused, resumed, client, torr): async def set_priority(request): torr = request.match_info["hash_id"] - client = qba.Client(host="localhost",port="8090",username="admin",password="adminadmin") + client = qba.Client(host="localhost", port="8090", username="admin", password="adminadmin") client.auth_log_in() data = await request.post() @@ -251,25 +249,23 @@ async def set_priority(request): pause = pause.strip("|") resume = resume.strip("|") - LOGGER.info(f"Paused {pause} of {torr}") - LOGGER.info(f"Resumed {resume} of {torr}") try: - client.torrents_file_priority(torrent_hash=torr,file_ids=pause,priority=0) + client.torrents_file_priority(torrent_hash=torr, file_ids=pause, priority=0) except qba.NotFound404Error: raise web.HTTPNotFound() except: - LOGGER.info("Errored in paused") + LOGGER.error("Errored in paused") try: - client.torrents_file_priority(torrent_hash=torr,file_ids=resume,priority=1) + client.torrents_file_priority(torrent_hash=torr, file_ids=resume, priority=1) except qba.NotFound404Error: raise web.HTTPNotFound() except: - LOGGER.info("Errored in resumed") + LOGGER.error("Errored in resumed") await asyncio.sleep(2) - if not await re_verfiy(pause,resume,client,torr): + if not await re_verfiy(pause, resume, client, torr): LOGGER.error("The torrent choose errored reverification failed") client.auth_log_out() return await list_torrent_contents(request)