Implement cancellation of downloads by gid

This commit is contained in:
jaskaranSM 2020-03-21 14:28:38 +05:30 committed by lzzy12
parent 6b60ca4491
commit a44d0da9f6
2 changed files with 37 additions and 16 deletions

View File

@ -54,6 +54,14 @@ def get_readable_file_size(size_in_bytes) -> str:
except IndexError:
return 'File too large'
def getDownloadByGid(gid):
with download_dict_lock:
for dl in download_dict.values():
if dl.status() == MirrorStatus.STATUS_DOWNLOADING:
if dl.download().gid == gid:
return dl
return None
def get_progress_bar_string(status):
completed = status.processed_bytes() / 8
@ -95,6 +103,7 @@ def get_readable_message():
if hasattr(download, 'is_torrent'):
msg += f"| P: {download.download().connections} " \
f"| S: {download.download().num_seeders}"
msg += f"\nGID: <code>{download.download().gid}</code>"
msg += "\n\n"
return msg

View File

@ -5,20 +5,36 @@ from bot.helper.telegram_helper.filters import CustomFilters
from bot.helper.ext_utils.fs_utils import clean_download
from bot.helper.telegram_helper.bot_commands import BotCommands
from time import sleep
from bot.helper.ext_utils.bot_utils import getDownloadByGid
@run_async
def cancel_mirror(bot,update):
mirror_message = update.message.reply_to_message
with download_dict_lock:
keys = download_dict.keys()
dl = download_dict[mirror_message.message_id]
if mirror_message is None or mirror_message.message_id not in keys:
if '/mirror' in mirror_message.text or '/tarmirror' in mirror_message.text:
msg = 'Message has already been cancelled'
else:
msg = 'Please reply to the /mirror message which was used to start the download to cancel it!'
sendMessage(msg, bot, update)
return
args = update.message.text.split(" ",maxsplit=1)
mirror_message = None
if len(args) > 1:
gid = args[1]
dl = getDownloadByGid(gid)
if not dl:
sendMessage(f"GID: <code>{gid}</code> not found.",bot,update)
return
with download_dict_lock:
keys = list(download_dict.keys())
mirror_message = dl._listener.message
elif update.message.reply_to_message:
mirror_message = update.message.reply_to_message
with download_dict_lock:
keys = list(download_dict.keys())
dl = download_dict[mirror_message.message_id]
if len(args) == 1:
if mirror_message is None or mirror_message.message_id not in keys:
if BotCommands.MirrorCommand in mirror_message.text or BotCommands.TarMirrorCommand in mirror_message.text:
msg = "Mirror already have been cancelled"
sendMessage(msg,bot,update)
return
else:
msg = "Please reply to the /mirror message which was used to start the download or /cancel gid to cancel it!"
sendMessage(msg,bot,update)
return
if dl.status() == "Uploading":
sendMessage("Upload in Progress, Don't Cancel it.", bot, update)
return
@ -31,13 +47,9 @@ def cancel_mirror(bot,update):
downloads = aria2.get_downloads(download.followed_by_ids)
aria2.pause(downloads)
aria2.pause([download])
elif dl.status() == "Uploading":
sendMessage("Upload in Progress, Dont Cancel it.",bot,update)
return
else:
dl._listener.onDownloadError("Download stopped by user!")
sleep(1) #Wait a Second For Aria2 To free Resources.
sleep(1) # Wait a Second For Aria2 To free Resources.
clean_download(f'{DOWNLOAD_DIR}{mirror_message.message_id}/')