Trying to fix bug

Signed-off-by: lzzy12 <jhashivam2020@gmail.com>
This commit is contained in:
lzzy12 2019-10-05 22:48:37 +05:30
parent 99efa2dacb
commit b58cb47584
6 changed files with 33 additions and 18 deletions

View File

@ -17,7 +17,7 @@ def getConfig(name: str):
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
try: try:
if bool(config['DEFAULT']['_____REMOVE_THIS_LINE_____']): if bool(getConfig('_____REMOVE_THIS_LINE_____')):
logging.ERROR('The README.md file there to be read! Exiting now!') logging.ERROR('The README.md file there to be read! Exiting now!')
exit() exit()
except KeyError: except KeyError:

View File

@ -1,8 +1,10 @@
from bot import download_dict from bot import download_dict
import logging
LOGGER = logging.getLogger(__name__)
class MirrorStatus: class MirrorStatus:
STATUS_UPLOADING = "Uploading" STATUS_UPLOADING = "Uploading"
STATUS_DOWNLOADING = "Downloading" STATUS_DOWNLOADING = "Downloading"
STATUS_WAITING = "Queued" STATUS_WAITING = "Queued"
@ -37,10 +39,10 @@ def get_download_status_list():
def get_progress_bar_string(status): def get_progress_bar_string(status):
if status.status() == MirrorStatus.STATUS_UPLOADING: if status.status() == MirrorStatus.STATUS_UPLOADING:
completed = status.uploaded_bytes/8 completed = status.uploaded_bytes / 8
else: else:
completed = status.download().completed_length/8 completed = status.download().completed_length / 8
total = status.download().total_length/8 total = status.download().total_length / 8
if total == 0: if total == 0:
p = 0 p = 0
else: else:
@ -48,10 +50,10 @@ def get_progress_bar_string(status):
p = min(max(p, 0), 100) p = min(max(p, 0), 100)
cFull = p // 8 cFull = p // 8
cPart = p % 8 - 1 cPart = p % 8 - 1
p_str = ''*cFull p_str = '' * cFull
if cPart >= 0: if cPart >= 0:
p_str += PROGRESS_INCOMPLETE[cPart] p_str += PROGRESS_INCOMPLETE[cPart]
p_str += ' '*(PROGRESS_MAX_SIZE - cFull) p_str += ' ' * (PROGRESS_MAX_SIZE - cFull)
p_str = f"[{p_str}]" p_str = f"[{p_str}]"
return p_str return p_str
@ -77,6 +79,7 @@ def get_readable_message(progress_list: list = download_dict.values()):
msg += f'<b>Name:</b> {status.name()}\n' \ msg += f'<b>Name:</b> {status.name()}\n' \
f'<b>status:</b> {status.status()}\n' \ f'<b>status:</b> {status.status()}\n' \
f'<code>{get_progress_bar_string(status)}</code> {status.progress()} of {status.size()}\n' \ f'<code>{get_progress_bar_string(status)}</code> {status.progress()} of {status.size()}\n' \
f'<b>Speed:</b> {status.speed()}\n' \ f'<b>Speed:</b> {status.speed()}\n' \
f'<b>ETA:</b> {status.eta()}\n' f'<b>ETA:</b> {status.eta()}\n'
return msg LOGGER.info(msg)
return msg

View File

@ -2,6 +2,7 @@ from time import sleep
from bot import DOWNLOAD_DIR, DOWNLOAD_STATUS_UPDATE_INTERVAL, aria2 from bot import DOWNLOAD_DIR, DOWNLOAD_STATUS_UPDATE_INTERVAL, aria2
from .download_status import DownloadStatus from .download_status import DownloadStatus
from .bot_utils import * from .bot_utils import *
from .exceptions import KillThreadException
class DownloadHelper: class DownloadHelper:

View File

@ -1,8 +1,9 @@
class DriveAuthError(Exception): class DriveAuthError(Exception):
pass pass
# Custom Exception class for killing thread as soon as they aren't needed
class KillThreadException(Exception): class KillThreadException(Exception):
""" Custom Exception class for killing thread as soon as they aren't needed"""
def __init__(self, message, error=None): def __init__(self, message, error=None):
super().__init__(message) super().__init__(message)
self.error = error self.error = error

View File

@ -4,11 +4,12 @@ from google.auth.transport.requests import Request
from googleapiclient.http import MediaFileUpload from googleapiclient.http import MediaFileUpload
import pickle import pickle
import os import os
import time
import logging
from bot import LOGGER, parent_id, DOWNLOAD_DIR from bot import LOGGER, parent_id, DOWNLOAD_DIR
from .fs_utils import get_mime_type from .fs_utils import get_mime_type
from .bot_utils import * from .bot_utils import *
import time from .exceptions import KillThreadException
import logging
logging.getLogger('googleapiclient.discovery').setLevel(logging.ERROR) logging.getLogger('googleapiclient.discovery').setLevel(logging.ERROR)
@ -32,7 +33,8 @@ class GoogleDriveHelper:
# File body description # File body description
media_body = MediaFileUpload(file_path, media_body = MediaFileUpload(file_path,
mimetype=mime_type, mimetype=mime_type,
resumable=True) resumable=True,
chunksize=1024*1024)
file_metadata = { file_metadata = {
'name': file_name, 'name': file_name,
'description': 'mirror', 'description': 'mirror',
@ -54,18 +56,23 @@ class GoogleDriveHelper:
_list = get_download_status_list() _list = get_download_status_list()
index = get_download_index(_list, get_download(self.__listener.message.message_id).gid) index = get_download_index(_list, get_download(self.__listener.message.message_id).gid)
uploaded_bytes = 0 uploaded_bytes = 0
should_update = True
while response is None: while response is None:
status, response = drive_file.next_chunk() status, response = drive_file.next_chunk()
time_lapsed = time.time() - self.start_time time_lapsed = time.time() - self.start_time
if status: if status:
# The iconic formula of speed = distance / time :) # The iconic formula of speed = distance / time :)
LOGGER.info(status.progress() * 100) LOGGER.info(f'{file_name}: {status.progress() * 100}')
chunk_size = status.total_size*status.progress() - uploaded_bytes chunk_size = status.total_size*status.progress() - uploaded_bytes
uploaded_bytes = status.total_size*status.progress() uploaded_bytes = status.total_size*status.progress()
download_dict[self.__listener.uid].uploaded_bytes += chunk_size download_dict[self.__listener.uid].uploaded_bytes += chunk_size
download_dict[self.__listener.uid].upload_time = time_lapsed download_dict[self.__listener.uid].upload_time = time_lapsed
self.__listener.onUploadProgress(_list, index) if should_update:
try:
self.__listener.onUploadProgress(_list, index)
except KillThreadException:
should_update = False
# Insert new permissions # Insert new permissions
self.__service.permissions().create(fileId=response['id'], body=permissions).execute() self.__service.permissions().create(fileId=response['id'], body=permissions).execute()
# Define file instance and get url for download # Define file instance and get url for download

View File

@ -67,7 +67,10 @@ class MirrorListener(listeners.MirrorListeners):
def onUploadProgress(self, progress: list, index: int): def onUploadProgress(self, progress: list, index: int):
msg = get_readable_message(progress) msg = get_readable_message(progress)
editMessage(msg, self.context, self.reply_message) try:
editMessage(msg, self.context, self.reply_message)
except BadRequest:
raise KillThreadException('Message deleted. Do not call this method from the thread')
@run_async @run_async