From b2f2ee4335e198e5605d095ae6c3f6acf0190158 Mon Sep 17 00:00:00 2001 From: lzzy12 Date: Mon, 6 Apr 2020 12:31:20 +0530 Subject: [PATCH] Fix aria downloads cancellation after b58eb87b111ddcbd8e607000b1f13e738495247d Signed-off-by: lzzy12 --- README.md | 24 +++--- add_to_google_group.py | 84 ------------------- bot/helper/ext_utils/bot_utils.py | 12 +-- .../download_utils/aria2_download.py | 6 +- .../status_utils/aria_download_status.py | 26 +++--- bot/modules/cancel_mirror.py | 6 +- 6 files changed, 35 insertions(+), 123 deletions(-) delete mode 100644 add_to_google_group.py diff --git a/README.md b/README.md index 0e8d5b6..8e14f78 100644 --- a/README.md +++ b/README.md @@ -45,18 +45,18 @@ cp config_sample.env config.env _____REMOVE_THIS_LINE_____=True ``` Fill up rest of the fields. Meaning of each fields are discussed below: -- BOT_TOKEN : The telegram bot token that you get from @BotFather -- GDRIVE_FOLDER_ID : This is the folder ID of the Google Drive Folder to which you want to upload all the mirrors. -- DOWNLOAD_DIR : The path to the local folder where the downloads should be downloaded to -- DOWNLOAD_STATUS_UPDATE_INTERVAL : A short interval of time in seconds after which the Mirror progress message is updated. (I recommend to keep it 5 seconds at least) -- OWNER_ID : The Telegram user ID (not username) of the owner of the bot -- AUTO_DELETE_MESSAGE_DURATION : Interval of time (in seconds), after which the bot deletes it's message (and command message) which is expected to be viewed instantly. Note: Set to -1 to never automatically delete messages -- IS_TEAM_DRIVE : (Optional field) Set to "True" if GDRIVE_FOLDER_ID is from a Team Drive else False or Leave it empty. -- USE_SERVICE_ACCOUNTS: (Optional field) (Leave empty if unsure) Whether to use service accounts or not. For this to work see "Using service accounts" section below. -- INDEX_URL : (Optional field) Refer to https://github.com/maple3142/GDIndex/ The URL should not have any trailing '/' -- API_KEY : This is to authenticate to your telegram account for downloading Telegram files. You can get this from https://my.telegram.org DO NOT put this in quotes. -- API_HASH : This is to authenticate to your telegram account for downloading Telegram files. You can get this from https://my.telegram.org -- USER_SESSION_STRING : Session string generated by running: +- **BOT_TOKEN** : The telegram bot token that you get from @BotFather +- **GDRIVE_FOLDER_ID** : This is the folder ID of the Google Drive Folder to which you want to upload all the mirrors. +- **DOWNLOAD_DIR** : The path to the local folder where the downloads should be downloaded to +- **DOWNLOAD_STATUS_UPDATE_INTERVAL** : A short interval of time in seconds after which the Mirror progress message is updated. (I recommend to keep it 5 seconds at least) +- **OWNER_ID** : The Telegram user ID (not username) of the owner of the bot +- **AUTO_DELETE_MESSAGE_DURATION** : Interval of time (in seconds), after which the bot deletes it's message (and command message) which is expected to be viewed instantly. Note: Set to -1 to never automatically delete messages +- **IS_TEAM_DRIVE** : (Optional field) Set to "True" if GDRIVE_FOLDER_ID is from a Team Drive else False or Leave it empty. +- **USE_SERVICE_ACCOUNTS**: (Optional field) (Leave empty if unsure) Whether to use service accounts or not. For this to work see "Using service accounts" section below. +- **INDEX_URL** : (Optional field) Refer to https://github.com/maple3142/GDIndex/ The URL should not have any trailing '/' +- **API_KEY** : This is to authenticate to your telegram account for downloading Telegram files. You can get this from https://my.telegram.org DO NOT put this in quotes. +- **API_HASH** : This is to authenticate to your telegram account for downloading Telegram files. You can get this from https://my.telegram.org +- **USER_SESSION_STRING** : Session string generated by running: ``` python3 generate_string_session.py ``` diff --git a/add_to_google_group.py b/add_to_google_group.py deleted file mode 100644 index aaed28b..0000000 --- a/add_to_google_group.py +++ /dev/null @@ -1,84 +0,0 @@ -# auto rclone -# Add service accounts to groups for your organization -# -# Author Telegram https://t.me/CodyDoby -# Inbox codyd@qq.com - -from __future__ import print_function - -import os -import pickle - -import argparse -import glob -import googleapiclient.discovery -import json -import progress.bar -import time -from google.auth.transport.requests import Request -from google_auth_oauthlib.flow import InstalledAppFlow - -stt = time.time() - -parse = argparse.ArgumentParser( - description='A tool to add service accounts to groups for your organization from a folder containing credential ' - 'files.') -parse.add_argument('--path', '-p', default='accounts', - help='Specify an alternative path to the service accounts folder.') -parse.add_argument('--credentials', '-c', default='credentials/credentials.json', - help='Specify the relative path for the controller file.') -parsereq = parse.add_argument_group('required arguments') -# service-account@googlegroups.com -parsereq.add_argument('--groupaddr', '-g', help='The address of groups for your organization.', required=True) - -args = parse.parse_args() -acc_dir = args.path -gaddr = args.groupaddr -credentials = glob.glob(args.credentials) - -creds = None -if os.path.exists('credentials/token.pickle'): - with open('credentials/token.pickle', 'rb') as token: - creds = pickle.load(token) -# If there are no (valid) credentials available, let the user log in. -if not creds or not creds.valid: - if creds and creds.expired and creds.refresh_token: - creds.refresh(Request()) - else: - flow = InstalledAppFlow.from_client_secrets_file(credentials[0], scopes=[ - 'https://www.googleapis.com/auth/admin.directory.group', - 'https://www.googleapis.com/auth/admin.directory.group.member' - ]) - # creds = flow.run_local_server(port=0) - creds = flow.run_console() - # Save the credentials for the next run - with open('credentials/token.pickle', 'wb') as token: - pickle.dump(creds, token) - -group = googleapiclient.discovery.build("admin", "directory_v1", credentials=creds) - -print(group.members()) - -batch = group.new_batch_http_request() - -sa = glob.glob('%s/*.json' % acc_dir) - -# sa = sa[0:5] - -pbar = progress.bar.Bar("Readying accounts", max=len(sa)) -for i in sa: - ce = json.loads(open(i, 'r').read())['client_email'] - - body = {"email": ce, "role": "MEMBER"} - batch.add(group.members().insert(groupKey=gaddr, body=body)) - # group.members().insert(groupKey=gaddr, body=body).execute() - - pbar.next() -pbar.finish() -print('Adding...') -batch.execute() - -print('Complete.') -hours, rem = divmod((time.time() - stt), 3600) -minutes, sec = divmod(rem, 60) -print("Elapsed Time:\n{:0>2}:{:0>2}:{:05.2f}".format(int(hours), int(minutes), sec)) diff --git a/bot/helper/ext_utils/bot_utils.py b/bot/helper/ext_utils/bot_utils.py index 4d396b9..8d0e6ce 100644 --- a/bot/helper/ext_utils/bot_utils.py +++ b/bot/helper/ext_utils/bot_utils.py @@ -85,14 +85,6 @@ def get_progress_bar_string(status): return p_str -def get_download_index(_list, gid): - index = 0 - for i in _list: - if i.download().gid == gid: - return index - index += 1 - - def get_readable_message(): with download_dict_lock: msg = "" @@ -105,8 +97,8 @@ def get_readable_message(): f" at {download.speed()}, ETA: {download.eta()} " if download.status() == MirrorStatus.STATUS_DOWNLOADING: if hasattr(download, 'is_torrent'): - msg += f"| P: {download.download().connections} " \ - f"| S: {download.download().num_seeders}" + msg += f"| P: {download.aria_download().connections} " \ + f"| S: {download.aria_download().num_seeders}" msg += f"\nGID: {download.gid()}" msg += "\n\n" return msg diff --git a/bot/helper/mirror_utils/download_utils/aria2_download.py b/bot/helper/mirror_utils/download_utils/aria2_download.py index c62cc0b..aaa299f 100644 --- a/bot/helper/mirror_utils/download_utils/aria2_download.py +++ b/bot/helper/mirror_utils/download_utils/aria2_download.py @@ -28,7 +28,7 @@ class AriaDownloadHelper(DownloadHelper): if api.get_download(gid).followed_by_ids: self.gid = api.get_download(gid).followed_by_ids[0] with download_dict_lock: - download_dict[self.__listener.uid] = AriaDownloadStatus(self.gid, self.__listener) + download_dict[self.__listener.uid] = AriaDownloadStatus(self, self.__listener) download_dict[self.__listener.uid].is_torrent = True update_all_messages() LOGGER.info(f'Changed gid from {gid} to {self.gid}') @@ -60,7 +60,7 @@ class AriaDownloadHelper(DownloadHelper): download = aria2.add_uris([link], {'dir': path}) self.gid = download.gid with download_dict_lock: - download_dict[self.__listener.uid] = AriaDownloadStatus(self.gid, self.__listener) + download_dict[self.__listener.uid] = AriaDownloadStatus(self, self.__listener) if download.error_message: self.__listener.onDownloadError(download.error_message) return @@ -73,7 +73,7 @@ class AriaDownloadHelper(DownloadHelper): def cancel_download(self): download = aria2.get_download(self.gid) - if download.is_queued: + if download.is_waiting: aria2.remove([download]) self.__listener.onDownloadError("Cancelled by user") return diff --git a/bot/helper/mirror_utils/status_utils/aria_download_status.py b/bot/helper/mirror_utils/status_utils/aria_download_status.py index 8449c83..7425e08 100644 --- a/bot/helper/mirror_utils/status_utils/aria_download_status.py +++ b/bot/helper/mirror_utils/status_utils/aria_download_status.py @@ -9,12 +9,13 @@ def get_download(gid): class AriaDownloadStatus(Status): - def __init__(self, gid, listener): + def __init__(self, obj, listener): super().__init__() self.upload_name = None self.is_archiving = False - self.__gid = gid - self.__download = get_download(gid) + self.obj = obj + self.__gid = obj.gid + self.__download = get_download(obj.gid) self.__uid = listener.uid self.__listener = listener self.message = listener.message @@ -37,28 +38,28 @@ class AriaDownloadStatus(Status): Gets total size of the mirror file/folder :return: total size of mirror """ - return self.download().total_length + return self.aria_download().total_length def processed_bytes(self): - return self.download().completed_length + return self.aria_download().completed_length def speed(self): - return self.download().download_speed_string() + return self.aria_download().download_speed_string() def name(self): - return self.download().name + return self.aria_download().name def path(self): return f"{DOWNLOAD_DIR}{self.__uid}" def size(self): - return self.download().total_length_string() + return self.aria_download().total_length_string() def eta(self): - return self.download().eta_string() + return self.aria_download().eta_string() def status(self): - download = self.download() + download = self.aria_download() if download.is_waiting: status = MirrorStatus.STATUS_WAITING elif download.is_paused: @@ -70,10 +71,13 @@ class AriaDownloadStatus(Status): status = MirrorStatus.STATUS_DOWNLOADING return status - def download(self): + def aria_download(self): self.__update() return self.__download + def download(self): + return self.obj + def uid(self): return self.__uid diff --git a/bot/modules/cancel_mirror.py b/bot/modules/cancel_mirror.py index c93988d..53ccdfc 100644 --- a/bot/modules/cancel_mirror.py +++ b/bot/modules/cancel_mirror.py @@ -57,9 +57,9 @@ def cancel_all(update, bot): with download_dict_lock: count = 0 for dlDetails in list(download_dict.values()): - if not dlDetails.status() == MirrorStatus.STATUS_UPLOADING\ - or not dlDetails.status() == MirrorStatus.STATUS_ARCHIVING: - dlDetails.cancel_download() + if dlDetails.status() == MirrorStatus.STATUS_DOWNLOADING\ + or dlDetails.status() == MirrorStatus.STATUS_WAITING: + dlDetails.download().cancel_download() count += 1 delete_all_messages() sendMessage(f'Cancelled {count} downloads!', update, bot)