Revert and Fix up

-finally fix stuck at 100% download for torrent file and metadata
-fix other bugs

Signed-off-by: anas <e.anastayyar@gmail.com>
This commit is contained in:
anas 2021-07-14 06:13:54 +03:00
parent e703e01a9e
commit c68e4d17a7
6 changed files with 44 additions and 64 deletions

View File

@ -15,38 +15,34 @@ class AriaDownloadHelper(DownloadHelper):
super().__init__() super().__init__()
@new_thread @new_thread
def __onDownloadStarted(self, api: API, gid): def __onDownloadStarted(self, api, gid):
if STOP_DUPLICATE_MIRROR or TORRENT_DIRECT_LIMIT is not None or TAR_UNZIP_LIMIT is not None: if STOP_DUPLICATE_MIRROR or TORRENT_DIRECT_LIMIT is not None or TAR_UNZIP_LIMIT is not None:
sleep(1) sleep(1)
dl = getDownloadByGid(gid) dl = getDownloadByGid(gid)
download = api.get_download(gid) download = aria2.get_download(gid)
if STOP_DUPLICATE_MIRROR: if STOP_DUPLICATE_MIRROR:
LOGGER.info(f"Checking File/Folder if already in Drive...") LOGGER.info(f"Checking File/Folder if already in Drive...")
sleep(1) sleep(1)
self.name = aria2.get_download(gid).name sname = aria2.get_download(gid).name
sname = self.name
if self.listener.isTar: if self.listener.isTar:
sname = sname + ".tar" sname = sname + ".tar"
if self.listener.extract: elif self.listener.extract:
smsg = None smsg = None
else: else:
gdrive = GoogleDriveHelper(None) gdrive = GoogleDriveHelper(None)
smsg, button = gdrive.drive_list(sname) smsg, button = gdrive.drive_list(sname)
if smsg: if smsg:
dl.getListener().onDownloadError(f'File/Folder already available in Drive.\n\n') dl.getListener().onDownloadError(f'File/Folder already available in Drive.\n\n')
aria2.remove([download], force = True) aria2.remove([download], force=True)
aria2.purge()
sendMarkup("Here are the search results:", dl.getListener().bot, dl.getListener().update, button) sendMarkup("Here are the search results:", dl.getListener().bot, dl.getListener().update, button)
return return
if TORRENT_DIRECT_LIMIT is not None or TAR_UNZIP_LIMIT is not None: if TORRENT_DIRECT_LIMIT is not None or TAR_UNZIP_LIMIT is not None:
limit = None limit = None
if TAR_UNZIP_LIMIT is not None and (self.listener.isTar or self.listener.extract): if TAR_UNZIP_LIMIT is not None and (self.listener.isTar or self.listener.extract):
LOGGER.info(f"Checking File/Folder Size...") LOGGER.info(f"Checking File/Folder Size...")
limit = TAR_UNZIP_LIMIT limit = TAR_UNZIP_LIMIT
mssg = f'Tar/Unzip limit is {TAR_UNZIP_LIMIT}' mssg = f'Tar/Unzip limit is {TAR_UNZIP_LIMIT}'
if TORRENT_DIRECT_LIMIT is not None and limit is None: elif TORRENT_DIRECT_LIMIT is not None and limit is None:
LOGGER.info(f"Checking File/Folder Size...") LOGGER.info(f"Checking File/Folder Size...")
limit = TORRENT_DIRECT_LIMIT limit = TORRENT_DIRECT_LIMIT
mssg = f'Torrent/Direct limit is {TORRENT_DIRECT_LIMIT}' mssg = f'Torrent/Direct limit is {TORRENT_DIRECT_LIMIT}'
@ -58,24 +54,23 @@ class AriaDownloadHelper(DownloadHelper):
if 'G' in limit[1] or 'g' in limit[1]: if 'G' in limit[1] or 'g' in limit[1]:
if size > limitint * 1024**3: if size > limitint * 1024**3:
dl.getListener().onDownloadError(f'{mssg}.\nYour File/Folder size is {get_readable_file_size(size)}') dl.getListener().onDownloadError(f'{mssg}.\nYour File/Folder size is {get_readable_file_size(size)}')
aria2.remove([download], force = True) aria2.remove([download], force=True)
aria2.purge()
return return
elif 'T' in limit[1] or 't' in limit[1]: elif 'T' in limit[1] or 't' in limit[1]:
if size > limitint * 1024**4: if size > limitint * 1024**4:
dl.getListener().onDownloadError(f'{mssg}.\nYour File/Folder size is {get_readable_file_size(size)}') dl.getListener().onDownloadError(f'{mssg}.\nYour File/Folder size is {get_readable_file_size(size)}')
aria2.remove([download], force = True) aria2.remove([download], force=True)
aria2.purge()
return return
update_all_messages() update_all_messages()
def __onDownloadComplete(self, api: API, gid): def __onDownloadComplete(self, api: API, gid):
LOGGER.info(f"onDownloadComplete: {gid}")
dl = getDownloadByGid(gid) dl = getDownloadByGid(gid)
download = api.get_download(gid) download = aria2.get_download(gid)
if download.followed_by_ids: if download.followed_by_ids:
new_gid = download.followed_by_ids[0] new_gid = download.followed_by_ids[0]
new_download = api.get_download(new_gid) new_download = aria2.get_download(new_gid)
if dl is None:
dl = getDownloadByGid(new_gid)
with download_dict_lock: with download_dict_lock:
download_dict[dl.uid()] = AriaDownloadStatus(new_gid, dl.getListener()) download_dict[dl.uid()] = AriaDownloadStatus(new_gid, dl.getListener())
if new_download.is_torrent: if new_download.is_torrent:
@ -88,22 +83,20 @@ class AriaDownloadHelper(DownloadHelper):
@new_thread @new_thread
def __onDownloadStopped(self, api, gid): def __onDownloadStopped(self, api, gid):
sleep(5) sleep(0.5)
dl = getDownloadByGid(gid) dl = getDownloadByGid(gid)
if dl: if dl:
dl.getListener().onDownloadError('Dead torrent!') dl.getListener().onDownloadError('Dead torrent!')
aria2.purge()
@new_thread @new_thread
def __onDownloadError(self, api, gid): def __onDownloadError(self, api, gid):
sleep(0.5) # sleep for split second to ensure proper dl gid update from onDownloadComplete sleep(0.5) # sleep for split second to ensure proper dl gid update from onDownloadComplete
dl = getDownloadByGid(gid) dl = getDownloadByGid(gid)
download = api.get_download(gid) download = aria2.get_download(gid)
error = download.error_message error = download.error_message
LOGGER.info(f"Download Error: {error}") LOGGER.info(f"Download Error: {error}")
if dl: if dl:
dl.getListener().onDownloadError(error) dl.getListener().onDownloadError(error)
aria2.purge()
def start_listener(self): def start_listener(self):
aria2.listen_to_notifications(threaded=True, on_download_start=self.__onDownloadStarted, aria2.listen_to_notifications(threaded=True, on_download_start=self.__onDownloadStarted,

View File

@ -169,7 +169,7 @@ class MegaDownloadHelper:
mname = node.getName() mname = node.getName()
if listener.isTar: if listener.isTar:
mname = mname + ".tar" mname = mname + ".tar"
if listener.extract: elif listener.extract:
smsg = None smsg = None
else: else:
gd = GoogleDriveHelper() gd = GoogleDriveHelper()
@ -184,7 +184,7 @@ class MegaDownloadHelper:
if TAR_UNZIP_LIMIT is not None and (listener.isTar or listener.extract): if TAR_UNZIP_LIMIT is not None and (listener.isTar or listener.extract):
limit = TAR_UNZIP_LIMIT limit = TAR_UNZIP_LIMIT
msg3 = f'Failed, Tar/Unzip limit is {TAR_UNZIP_LIMIT}.\nYour File/Folder size is {get_readable_file_size(api.getSize(node))}.' msg3 = f'Failed, Tar/Unzip limit is {TAR_UNZIP_LIMIT}.\nYour File/Folder size is {get_readable_file_size(api.getSize(node))}.'
if MEGA_LIMIT is not None and limit is None: elif MEGA_LIMIT is not None and limit is None:
limit = MEGA_LIMIT limit = MEGA_LIMIT
msg3 = f'Failed, Mega limit is {MEGA_LIMIT}.\nYour File/Folder size is {get_readable_file_size(api.getSize(node))}.' msg3 = f'Failed, Mega limit is {MEGA_LIMIT}.\nYour File/Folder size is {get_readable_file_size(api.getSize(node))}.'
if limit is not None: if limit is not None:

View File

@ -104,7 +104,7 @@ class TelegramDownloadHelper(DownloadHelper):
LOGGER.info(f"Checking File/Folder if already in Drive...") LOGGER.info(f"Checking File/Folder if already in Drive...")
if self.__listener.isTar: if self.__listener.isTar:
name = name + ".tar" name = name + ".tar"
if self.__listener.extract: elif self.__listener.extract:
smsg = None smsg = None
else: else:
gd = GoogleDriveHelper() gd = GoogleDriveHelper()

View File

@ -2,7 +2,6 @@ from bot import aria2, DOWNLOAD_DIR, LOGGER
from bot.helper.ext_utils.bot_utils import MirrorStatus from bot.helper.ext_utils.bot_utils import MirrorStatus
from .status import Status from .status import Status
def get_download(gid): def get_download(gid):
return aria2.get_download(gid) return aria2.get_download(gid)
@ -24,6 +23,9 @@ class AriaDownloadStatus(Status):
def __update(self): def __update(self):
self.__download = get_download(self.__gid) self.__download = get_download(self.__gid)
download = self.__download
if download.followed_by_ids:
self.__gid = download.followed_by_ids[0]
def progress(self): def progress(self):
""" """
@ -95,13 +97,11 @@ class AriaDownloadStatus(Status):
LOGGER.info(f"Cancelling Download: {self.name()}") LOGGER.info(f"Cancelling Download: {self.name()}")
download = self.aria_download() download = self.aria_download()
if download.is_waiting: if download.is_waiting:
aria2.remove([download], force = True)
aria2.purge()
self.__listener.onDownloadError("Cancelled by user") self.__listener.onDownloadError("Cancelled by user")
aria2.remove([download], force=True)
return return
if len(download.followed_by_ids) != 0: if len(download.followed_by_ids) != 0:
downloads = aria2.get_downloads(download.followed_by_ids) downloads = aria2.get_downloads(download.followed_by_ids)
aria2.remove(downloads, force = True) aria2.remove(downloads, force=True)
self.__listener.onDownloadError("Download stopped by user!") self.__listener.onDownloadError('Download stopped by user!')
aria2.remove([download], force = True) aria2.remove([download], force=True)
aria2.purge()

View File

@ -1,11 +1,10 @@
from telegram import InlineKeyboardButton, InlineKeyboardMarkup from telegram import InlineKeyboardMarkup
from telegram.ext import CallbackContext, CallbackQueryHandler
from telegram.message import Message from telegram.message import Message
from telegram.update import Update from telegram.update import Update
import psutil, shutil import psutil, shutil
import time import time
from bot import AUTO_DELETE_MESSAGE_DURATION, LOGGER, bot, \ from bot import AUTO_DELETE_MESSAGE_DURATION, LOGGER, bot, \
status_reply_dict, status_reply_dict_lock, download_dict, download_dict_lock, botStartTime, dispatcher status_reply_dict, status_reply_dict_lock, download_dict, download_dict_lock, botStartTime
from bot.helper.ext_utils.bot_utils import get_readable_message, get_readable_file_size, get_readable_time, MirrorStatus from bot.helper.ext_utils.bot_utils import get_readable_message, get_readable_file_size, get_readable_time, MirrorStatus
from telegram.error import TimedOut, BadRequest from telegram.error import TimedOut, BadRequest
@ -14,7 +13,7 @@ def sendMessage(text: str, bot, update: Update):
try: try:
return bot.send_message(update.message.chat_id, return bot.send_message(update.message.chat_id,
reply_to_message_id=update.message.message_id, reply_to_message_id=update.message.message_id,
text=text, allow_sending_without_reply=True, parse_mode='HTMl') text=text, allow_sending_without_reply=True, parse_mode='HTMl')
except Exception as e: except Exception as e:
LOGGER.error(str(e)) LOGGER.error(str(e))
def sendMarkup(text: str, bot, update: Update, reply_markup: InlineKeyboardMarkup): def sendMarkup(text: str, bot, update: Update, reply_markup: InlineKeyboardMarkup):
@ -68,7 +67,13 @@ def delete_all_messages():
def update_all_messages(): def update_all_messages():
total, used, free = shutil.disk_usage('.')
free = get_readable_file_size(free)
currentTime = get_readable_time(time.time() - botStartTime)
msg = get_readable_message() msg = get_readable_message()
msg += f"<b>CPU:</b> {psutil.cpu_percent()}%" \
f" <b>RAM:</b> {psutil.virtual_memory().percent}%" \
f" <b>DISK:</b> {psutil.disk_usage('/').percent}%"
with download_dict_lock: with download_dict_lock:
dlspeed_bytes = 0 dlspeed_bytes = 0
uldl_bytes = 0 uldl_bytes = 0
@ -86,22 +91,27 @@ def update_all_messages():
uldl_bytes += float(speedy.split('M')[0]) * 1048576 uldl_bytes += float(speedy.split('M')[0]) * 1048576
dlspeed = get_readable_file_size(dlspeed_bytes) dlspeed = get_readable_file_size(dlspeed_bytes)
ulspeed = get_readable_file_size(uldl_bytes) ulspeed = get_readable_file_size(uldl_bytes)
msg += f"<b>DL:</b> {dlspeed}ps 🔻 | <b>UL:</b> {ulspeed}ps 🔺" msg += f"\n<b>FREE:</b> {free} | <b>UPTIME:</b> {currentTime}\n<b>DL:</b> {dlspeed}ps 🔻 | <b>UL:</b> {ulspeed}ps 🔺\n"
with status_reply_dict_lock: with status_reply_dict_lock:
for chat_id in list(status_reply_dict.keys()): for chat_id in list(status_reply_dict.keys()):
if status_reply_dict[chat_id] and msg != status_reply_dict[chat_id].text: if status_reply_dict[chat_id] and msg != status_reply_dict[chat_id].text:
if len(msg) == 0: if len(msg) == 0:
msg = "Starting DL" msg = "Starting DL"
try: try:
keyboard = [[InlineKeyboardButton("♻️ Stats", callback_data="stats_")]] editMessage(msg, status_reply_dict[chat_id])
editMessage(msg, status_reply_dict[chat_id], reply_markup=InlineKeyboardMarkup(keyboard))
except Exception as e: except Exception as e:
LOGGER.error(str(e)) LOGGER.error(str(e))
status_reply_dict[chat_id].text = msg status_reply_dict[chat_id].text = msg
def sendStatusMessage(msg, bot): def sendStatusMessage(msg, bot):
total, used, free = shutil.disk_usage('.')
free = get_readable_file_size(free)
currentTime = get_readable_time(time.time() - botStartTime)
progress = get_readable_message() progress = get_readable_message()
progress += f"<b>CPU:</b> {psutil.cpu_percent()}%" \
f" <b>RAM:</b> {psutil.virtual_memory().percent}%" \
f" <b>DISK:</b> {psutil.disk_usage('/').percent}%"
with download_dict_lock: with download_dict_lock:
dlspeed_bytes = 0 dlspeed_bytes = 0
uldl_bytes = 0 uldl_bytes = 0
@ -119,7 +129,7 @@ def sendStatusMessage(msg, bot):
uldl_bytes += float(speedy.split('M')[0]) * 1048576 uldl_bytes += float(speedy.split('M')[0]) * 1048576
dlspeed = get_readable_file_size(dlspeed_bytes) dlspeed = get_readable_file_size(dlspeed_bytes)
ulspeed = get_readable_file_size(uldl_bytes) ulspeed = get_readable_file_size(uldl_bytes)
progress += f"<b>DL:</b> {dlspeed}ps 🔻 | <b>UL:</b> {ulspeed}ps 🔺" progress += f"\n<b>FREE:</b> {free} | <b>UPTIME:</b> {currentTime}\n<b>DL:</b> {dlspeed}ps 🔻 | <b>UL:</b> {ulspeed}ps 🔺\n"
with status_reply_dict_lock: with status_reply_dict_lock:
if msg.message.chat.id in list(status_reply_dict.keys()): if msg.message.chat.id in list(status_reply_dict.keys()):
try: try:
@ -134,27 +144,3 @@ def sendStatusMessage(msg, bot):
progress = "Starting DL" progress = "Starting DL"
message = sendMessage(progress, bot, msg) message = sendMessage(progress, bot, msg)
status_reply_dict[msg.message.chat.id] = message status_reply_dict[msg.message.chat.id] = message
def pop_up_stats(update, context):
query = update.callback_query
stats = bot_sys_stats()
query.answer(text=stats, show_alert=True)
def bot_sys_stats():
currentTime = get_readable_time(time.time() - botStartTime)
cpu = psutil.cpu_percent()
mem = psutil.virtual_memory().percent
disk = psutil.disk_usage("/").percent
total, used, free = shutil.disk_usage('.')
free = get_readable_file_size(free)
stats = f"""
Bot Uptime: {currentTime}
Free Disk: {free}
CPU: {cpu}%
RAM: {mem}%
DISK: {disk}%
"""
return stats
dispatcher.add_handler(CallbackQueryHandler(pop_up_stats, pattern="stats_"))

View File

@ -2,7 +2,7 @@ import requests
from telegram.ext import CommandHandler from telegram.ext import CommandHandler
from telegram import InlineKeyboardMarkup from telegram import InlineKeyboardMarkup
from bot import Interval, INDEX_URL, BUTTON_FOUR_NAME, BUTTON_FOUR_URL, BUTTON_FIVE_NAME, BUTTON_FIVE_URL, BUTTON_SIX_NAME, BUTTON_SIX_URL, BLOCK_MEGA_FOLDER, BLOCK_MEGA_LINKS, VIEW_LINK from bot import Interval, INDEX_URL, BUTTON_FOUR_NAME, BUTTON_FOUR_URL, BUTTON_FIVE_NAME, BUTTON_FIVE_URL, BUTTON_SIX_NAME, BUTTON_SIX_URL, BLOCK_MEGA_FOLDER, BLOCK_MEGA_LINKS, VIEW_LINK, aria2
from bot import dispatcher, DOWNLOAD_DIR, DOWNLOAD_STATUS_UPDATE_INTERVAL, download_dict, download_dict_lock, SHORTENER, SHORTENER_API, TAR_UNZIP_LIMIT from bot import dispatcher, DOWNLOAD_DIR, DOWNLOAD_STATUS_UPDATE_INTERVAL, download_dict, download_dict_lock, SHORTENER, SHORTENER_API, TAR_UNZIP_LIMIT
from bot.helper.ext_utils import fs_utils, bot_utils from bot.helper.ext_utils import fs_utils, bot_utils
from bot.helper.ext_utils.bot_utils import setInterval, get_mega_link_type from bot.helper.ext_utils.bot_utils import setInterval, get_mega_link_type
@ -50,6 +50,7 @@ class MirrorListener(listeners.MirrorListeners):
def clean(self): def clean(self):
try: try:
aria2.purge()
Interval[0].cancel() Interval[0].cancel()
del Interval[0] del Interval[0]
delete_all_messages() delete_all_messages()