Small optimisations all around

Signed-off-by: lzzy12 <jhashivam2020@gmail.com>
This commit is contained in:
lzzy12 2019-12-25 16:17:02 +05:30
parent 8e886a3030
commit 4e1f7a8a77
6 changed files with 33 additions and 42 deletions

View File

@ -55,21 +55,8 @@ def get_readable_file_size(size_in_bytes) -> str:
return 'File too large'
def get_download(message_id):
with download_dict_lock:
return download_dict[message_id].download()
def get_download_status_list():
with download_dict_lock:
return list(download_dict.values())
def get_progress_bar_string(status):
if status.status() == MirrorStatus.STATUS_UPLOADING:
completed = status.obj.uploaded_bytes / 8
else:
completed = status.download().completed_length / 8
completed = status.processed_bytes() / 8
total = status.size_raw() / 8
if total == 0:
p = 0
@ -94,14 +81,6 @@ def get_download_index(_list, gid):
index += 1
def get_download_str():
result = ""
with download_dict_lock:
for status in list(download_dict.values()):
result += (status.progress() + status.speed() + status.status())
return result
def get_readable_message():
with download_dict_lock:
msg = ""

View File

@ -1,13 +1,13 @@
from bot import aria2, DOWNLOAD_DIR
from bot.helper.ext_utils.bot_utils import get_readable_file_size, MirrorStatus, get_readable_time
from .download_status import DownloadStatus
from bot.helper.ext_utils.bot_utils import MirrorStatus
from .status import Status
def get_download(gid):
return aria2.get_download(gid)
class AriaDownloadStatus(DownloadStatus):
class AriaDownloadStatus(Status):
def __init__(self, gid, listener):
super().__init__()
@ -38,31 +38,31 @@ class AriaDownloadStatus(DownloadStatus):
"""
return self.download().total_length
def processed_bytes(self):
return self.download().completed_length
def speed(self):
self.__update()
return self.__download.download_speed_string()
return self.download().download_speed_string()
def name(self):
self.__update()
return self.__download.name
return self.download().name
def path(self):
return f"{DOWNLOAD_DIR}{self.__uid}"
def size(self):
return self.__download.total_length_string()
return self.download().total_length_string()
def eta(self):
self.__update()
return self.__download.eta_string()
return self.download().eta_string()
def status(self):
self.__update()
if self.download().is_waiting:
download = self.download()
if download.is_waiting:
status = MirrorStatus.STATUS_WAITING
elif self.download().is_paused:
elif download.is_paused:
status = MirrorStatus.STATUS_CANCELLED
elif self.__download.has_failed:
elif download.has_failed:
status = MirrorStatus.STATUS_FAILED
else:
status = MirrorStatus.STATUS_DOWNLOADING

View File

@ -1,7 +1,7 @@
# Generic status class. All other status classes must inherit this class
class DownloadStatus:
class Status:
def progress(self):
"""
@ -34,3 +34,7 @@ class DownloadStatus:
""":return String describing what is the object of this class will be tracking (upload/download/something
else) """
raise NotImplementedError
def processed_bytes(self):
""":return The size of file that has been processed (downloaded/uploaded/archived)"""
raise NotImplementedError

View File

@ -1,13 +1,16 @@
from .download_status import DownloadStatus
from .status import Status
from bot.helper.ext_utils.bot_utils import get_readable_file_size, MirrorStatus
class TarStatus(DownloadStatus):
class TarStatus(Status):
def __init__(self, name, path, size):
self.__name = name
self.__path = path
self.__size = size
# The progress of Tar function cannot be tracked. So we just return dummy values.
# If this is possible in future,we should implement it
def progress(self):
return '0'
@ -29,3 +32,5 @@ class TarStatus(DownloadStatus):
def status(self):
return MirrorStatus.STATUS_ARCHIVING
def processed_bytes(self):
return 0

View File

@ -1,9 +1,9 @@
from .download_status import DownloadStatus
from .status import Status
from bot.helper.ext_utils.bot_utils import MirrorStatus, get_readable_file_size, get_readable_time
from bot import DOWNLOAD_DIR
class UploadStatus(DownloadStatus):
class UploadStatus(Status):
def __init__(self, obj, size, uid):
self.obj = obj
self.__size = size
@ -12,6 +12,9 @@ class UploadStatus(DownloadStatus):
def path(self):
return f"{DOWNLOAD_DIR}{self.uid}"
def processed_bytes(self):
return self.obj.uploaded_bytes
def size_raw(self):
return self.__size

View File

@ -242,7 +242,7 @@ class GoogleDriveHelper:
f"</a> (folder)"
if INDEX_URL is not None:
url = f'{INDEX_URL}/{file.get("name")}/'
msg += f'| <a href="{url}"> Index URL</a>'
msg += f' | <a href="{url}"> Index URL</a>'
else:
msg += f"⁍ <a href='https://drive.google.com/uc?id={file.get('id')}" \
f"&export=download'>{file.get('name')}</a> ({get_readable_file_size(int(file.get('size')))})"