Skip to content

Commit

Permalink
Implement cancellation of downloads by gid
Browse files Browse the repository at this point in the history
  • Loading branch information
JaskaranSM authored and lzzy12 committed Mar 22, 2020
1 parent 6b60ca4 commit a44d0da
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
9 changes: 9 additions & 0 deletions bot/helper/ext_utils/bot_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
44 changes: 28 additions & 16 deletions bot/modules/cancel_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,36 @@
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
Expand All @@ -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}/')


Expand Down

0 comments on commit a44d0da

Please sign in to comment.