From ed8bb1e69d2db0d63f5193c7f0cb72d1bdadb1f1 Mon Sep 17 00:00:00 2001
From: Levi <57452819+l3v11@users.noreply.github.com>
Date: Wed, 5 Oct 2022 05:58:28 +0600
Subject: [PATCH] Fix Index Link appearance issue in clone module
---
bot/__init__.py | 28 +++-----
bot/helper/drive_utils/gdriveTools.py | 67 ++++++++++----------
bot/helper/ext_utils/fs_utils.py | 2 +-
bot/helper/telegram_helper/button_builder.py | 30 ++++-----
bot/modules/auth.py | 4 +-
bot/modules/eval.py | 4 +-
bot/modules/shell.py | 4 +-
7 files changed, 63 insertions(+), 76 deletions(-)
diff --git a/bot/__init__.py b/bot/__init__.py
index 26693283..ec546d30 100644
--- a/bot/__init__.py
+++ b/bot/__init__.py
@@ -80,9 +80,7 @@
else:
AUTHORIZED_USERS = set()
-DATABASE_URL = os.environ.get('DATABASE_URL', '')
-if len(DATABASE_URL) == 0:
- DATABASE_URL = None
+DATABASE_URL = os.environ.get('DATABASE_URL', None)
IS_TEAM_DRIVE = os.environ.get('IS_TEAM_DRIVE', '')
IS_TEAM_DRIVE = IS_TEAM_DRIVE.lower() == 'true'
@@ -90,12 +88,9 @@
USE_SERVICE_ACCOUNTS = os.environ.get('USE_SERVICE_ACCOUNTS', '')
USE_SERVICE_ACCOUNTS = USE_SERVICE_ACCOUNTS.lower() == 'true'
-DOWNLOAD_DIR = os.environ.get('DOWNLOAD_DIR', '')
-if len(DOWNLOAD_DIR) == 0:
- DOWNLOAD_DIR = '/usr/src/app/downloads/'
-else:
- if not DOWNLOAD_DIR.endswith('/'):
- DOWNLOAD_DIR = DOWNLOAD_DIR + '/'
+DOWNLOAD_DIR = os.environ.get('DOWNLOAD_DIR', '/usr/src/app/downloads/')
+if not DOWNLOAD_DIR.endswith('/'):
+ DOWNLOAD_DIR = DOWNLOAD_DIR + '/'
STATUS_UPDATE_INTERVAL = os.environ.get('STATUS_UPDATE_INTERVAL', '')
STATUS_UPDATE_INTERVAL = 10 if len(STATUS_UPDATE_INTERVAL) == 0 else int(STATUS_UPDATE_INTERVAL)
@@ -103,9 +98,7 @@
TELEGRAPH_ACCS = os.environ.get('TELEGRAPH_ACCS', '')
TELEGRAPH_ACCS = 1 if len(TELEGRAPH_ACCS) == 0 else int(TELEGRAPH_ACCS)
-INDEX_URL = os.environ.get('INDEX_URL', '').rstrip("/")
-if len(INDEX_URL) == 0:
- INDEX_URL = None
+INDEX_URL = os.environ.get('INDEX_URL', None).rstrip("/")
ARCHIVE_LIMIT = os.environ.get('ARCHIVE_LIMIT', '')
ARCHIVE_LIMIT = None if len(ARCHIVE_LIMIT) == 0 else float(ARCHIVE_LIMIT)
@@ -152,15 +145,10 @@
except Exception as err:
LOGGER.error(f"DRIVE_LIST_URL: {err}")
-APPDRIVE_EMAIL = os.environ.get('APPDRIVE_EMAIL', '')
-APPDRIVE_PASS = os.environ.get('APPDRIVE_PASS', '')
-if len(APPDRIVE_EMAIL) == 0 or len(APPDRIVE_PASS) == 0:
- APPDRIVE_EMAIL = None
- APPDRIVE_PASS = None
+APPDRIVE_EMAIL = os.environ.get('APPDRIVE_EMAIL', None)
+APPDRIVE_PASS = os.environ.get('APPDRIVE_PASS', None)
-GDTOT_CRYPT = os.environ.get('GDTOT_CRYPT', '')
-if len(GDTOT_CRYPT) == 0:
- GDTOT_CRYPT = None
+GDTOT_CRYPT = os.environ.get('GDTOT_CRYPT', None)
if os.path.exists('drive_list'):
with open('drive_list', 'r+') as f:
diff --git a/bot/helper/drive_utils/gdriveTools.py b/bot/helper/drive_utils/gdriveTools.py
index bf92c92e..50ffb51e 100644
--- a/bot/helper/drive_utils/gdriveTools.py
+++ b/bot/helper/drive_utils/gdriveTools.py
@@ -16,7 +16,7 @@
from google.oauth2 import service_account
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
-from googleapiclient.errors import Error as GCError, HttpError
+from googleapiclient.errors import HttpError
from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
from bot import LOGGER, DRIVE_NAMES, DRIVE_IDS, INDEX_URLS, DRIVE_FOLDER_ID, \
@@ -138,16 +138,16 @@ def __getIdFromUrl(link: str):
@retry(wait=wait_exponential(multiplier=2, min=3, max=6),
stop=stop_after_attempt(3),
- retry=retry_if_exception_type(GCError))
+ retry=retry_if_exception_type(Exception))
def __getFileMetadata(self, file_id):
return self.__service.files().get(
- supportsAllDrives=True,
fileId=file_id,
+ supportsAllDrives=True,
fields='name, id, mimeType, size').execute()
@retry(wait=wait_exponential(multiplier=2, min=3, max=6),
stop=stop_after_attempt(3),
- retry=retry_if_exception_type(GCError))
+ retry=retry_if_exception_type(Exception))
def __getFilesByFolderId(self, folder_id):
page_token = None
query = f"'{folder_id}' in parents and trashed = false"
@@ -236,7 +236,7 @@ def deleteFile(self, link: str):
try:
self.__service.files().delete(
fileId=file_id,
- supportsAllDrives=IS_TEAM_DRIVE).execute()
+ supportsAllDrives=True).execute()
msg = "Permanently deleted"
except HttpError as err:
err = str(err).replace('>', '').replace('<', '')
@@ -259,9 +259,9 @@ def __set_permission_public(self, file_id):
'role': 'reader'
}
return self.__service.permissions().create(
- supportsAllDrives=True,
fileId=file_id,
- body=permissions).execute()
+ body=permissions,
+ supportsAllDrives=True).execute()
def __set_permission_email(self, file_id, email):
permissions = {
@@ -270,9 +270,9 @@ def __set_permission_email(self, file_id, email):
'emailAddress': email
}
return self.__service.permissions().create(
- supportsAllDrives=True,
fileId=file_id,
body=permissions,
+ supportsAllDrives=True,
sendNotificationEmail=False).execute()
def setPermission(self, link, access):
@@ -307,7 +307,7 @@ def setPermission(self, link, access):
@retry(wait=wait_exponential(multiplier=2, min=3, max=6),
stop=stop_after_attempt(3),
- retry=retry_if_exception_type(GCError))
+ retry=retry_if_exception_type(Exception))
def __create_directory(self, directory_name, dest_id):
file_metadata = {
"name": directory_name,
@@ -316,8 +316,8 @@ def __create_directory(self, directory_name, dest_id):
if dest_id is not None:
file_metadata["parents"] = [dest_id]
file = self.__service.files().create(
- supportsAllDrives=True,
- body=file_metadata).execute()
+ body=file_metadata,
+ supportsAllDrives=True).execute()
file_id = file.get("id")
if not IS_TEAM_DRIVE:
self.__set_permission_public(file_id)
@@ -325,16 +325,16 @@ def __create_directory(self, directory_name, dest_id):
@retry(wait=wait_exponential(multiplier=2, min=3, max=6),
stop=stop_after_attempt(3),
- retry=retry_if_exception_type(GCError))
+ retry=retry_if_exception_type(Exception))
def __copyFile(self, file_id, dest_id):
body = {
'parents': [dest_id]
}
try:
res = self.__service.files().copy(
- supportsAllDrives=True,
fileId=file_id,
- body=body).execute()
+ body=body,
+ supportsAllDrives=True).execute()
return res
except HttpError as err:
if err.resp.get('content-type', '').startswith('application/json'):
@@ -371,7 +371,7 @@ def __cloneFolder(self, name, local_path, folder_id, dest_id):
if self.__is_cancelled:
break
- def clone(self, link, dest_id):
+ def clone(self, link, __dest_id):
self.__is_cloning = True
self.__start_time = time.time()
self.__total_files = 0
@@ -382,7 +382,8 @@ def clone(self, link, dest_id):
msg = "Drive ID not found"
LOGGER.error(msg)
return msg
- if dest_id != "":
+ if __dest_id != "":
+ dest_id = __dest_id
index_url = None
else:
dest_id = DRIVE_FOLDER_ID
@@ -433,7 +434,7 @@ def clone(self, link, dest_id):
token_service = self.__alt_authorize()
if token_service is not None:
self.__service = token_service
- return self.clone(link, dest_id)
+ return self.clone(link, __dest_id)
msg = "File not found"
else:
msg = err
@@ -491,7 +492,7 @@ def _progress(self):
@retry(wait=wait_exponential(multiplier=2, min=3, max=6),
stop=stop_after_attempt(3),
- retry=(retry_if_exception_type(GCError) | retry_if_exception_type(IOError)))
+ retry=(retry_if_exception_type(Exception)))
def __upload_file(self, file_path, file_name, mime_type, dest_id):
file_metadata = {
'name': file_name,
@@ -502,26 +503,24 @@ def __upload_file(self, file_path, file_name, mime_type, dest_id):
if os.path.getsize(file_path) == 0:
media_body = MediaFileUpload(file_path, mimetype=mime_type, resumable=False)
response = self.__service.files().create(
- supportsAllDrives=True,
body=file_metadata,
- media_body=media_body).execute()
+ media_body=media_body,
+ supportsAllDrives=True).execute()
if not IS_TEAM_DRIVE:
self.__set_permission_public(response['id'])
drive_file = self.__service.files().get(
- supportsAllDrives=True,
- fileId=response['id']).execute()
+ fileId=response['id'],
+ supportsAllDrives=True).execute()
download_url = self.__G_DRIVE_BASE_DOWNLOAD_URL.format(drive_file.get('id'))
return download_url
media_body = MediaFileUpload(file_path, mimetype=mime_type, resumable=True,
chunksize=50 * 1024 * 1024)
drive_file = self.__service.files().create(
- supportsAllDrives=True,
body=file_metadata,
- media_body=media_body)
+ media_body=media_body,
+ supportsAllDrives=True)
response = None
- while response is None:
- if self.__is_cancelled:
- break
+ while response is None and not self.__is_cancelled:
try:
self.__status, response = drive_file.next_chunk()
except HttpError as err:
@@ -541,8 +540,8 @@ def __upload_file(self, file_path, file_name, mime_type, dest_id):
if not IS_TEAM_DRIVE:
self.__set_permission_public(response['id'])
drive_file = self.__service.files().get(
- supportsAllDrives=True,
- fileId=response['id']).execute()
+ fileId=response['id'],
+ supportsAllDrives=True).execute()
download_url = self.__G_DRIVE_BASE_DOWNLOAD_URL.format(drive_file.get('id'))
return download_url
@@ -610,9 +609,11 @@ def upload(self, file_name: str):
@retry(wait=wait_exponential(multiplier=2, min=3, max=6),
stop=stop_after_attempt(3),
- retry=(retry_if_exception_type(GCError) | retry_if_exception_type(IOError)))
+ retry=(retry_if_exception_type(Exception)))
def __download_file(self, file_id, path, filename, mime_type):
- request = self.__service.files().get_media(fileId=file_id)
+ request = self.__service.files().get_media(
+ fileId=file_id,
+ supportsAllDrives=True)
filename = filename.replace('/', '')
if len(filename.encode()) > 255:
ext = os.path.splitext(filename)[1]
@@ -753,7 +754,7 @@ def __receive_callback(self, request_id, response, exception):
if response['files']:
self.response[request_id] = response
- def __drive_query(self, DRIVE_IDS, search_type, file_name):
+ def __drive_query(self, drive_ids, search_type, file_name):
batch = self.__service.new_batch_http_request(self.__receive_callback)
query = f"name contains '{file_name}' and "
if search_type is not None:
@@ -762,7 +763,7 @@ def __drive_query(self, DRIVE_IDS, search_type, file_name):
elif search_type == '-f':
query += "mimeType != 'application/vnd.google-apps.folder' and "
query += "trashed=false"
- for drive_id in DRIVE_IDS:
+ for drive_id in drive_ids:
if drive_id == "root":
batch.add(
self.__service.files().list(
diff --git a/bot/helper/ext_utils/fs_utils.py b/bot/helper/ext_utils/fs_utils.py
index ea3bff4f..e7aef7fb 100644
--- a/bot/helper/ext_utils/fs_utils.py
+++ b/bot/helper/ext_utils/fs_utils.py
@@ -67,7 +67,7 @@ def get_path_size(path: str):
def get_base_name(orig_path: str):
ext = [ext for ext in ARCH_EXT if orig_path.lower().endswith(ext)]
- if len(ext) > 0:
+ if ext:
ext = ext[0]
return re.split(ext + '$', orig_path, maxsplit=1, flags=re.I)[0]
else:
diff --git a/bot/helper/telegram_helper/button_builder.py b/bot/helper/telegram_helper/button_builder.py
index e31d0d24..863c821f 100644
--- a/bot/helper/telegram_helper/button_builder.py
+++ b/bot/helper/telegram_helper/button_builder.py
@@ -3,22 +3,22 @@
class ButtonMaker:
def __init__(self):
- self.button = []
- self.header_button = []
- self.footer_button = []
+ self.__button = []
+ self.__header_button = []
+ self.__footer_button = []
- def build_button(self, key, link, footer=False, header=False):
- if not footer and not header:
- self.button.append(InlineKeyboardButton(text=key, url=link))
- elif header:
- self.header_button.append(InlineKeyboardButton(text=key, url=link))
- elif footer:
- self.footer_button.append(InlineKeyboardButton(text=key, url=link))
+ def build_button(self, key, link, position=None):
+ if not position:
+ self.__button.append(InlineKeyboardButton(text=key, url=link))
+ elif position == 'header':
+ self.__header_button.append(InlineKeyboardButton(text=key, url=link))
+ elif position == 'footer':
+ self.__footer_button.append(InlineKeyboardButton(text=key, url=link))
def build_menu(self, n_cols):
- menu = [self.button[i:i + n_cols] for i in range(0, len(self.button), n_cols)]
- if self.header_button:
- menu.insert(0, self.header_button)
- if self.footer_button:
- menu.append(self.footer_button)
+ menu = [self.__button[i:i + n_cols] for i in range(0, len(self.__button), n_cols)]
+ if self.__header_button:
+ menu.insert(0, self.__header_button)
+ if self.__footer_button:
+ menu.append(self.__footer_button)
return InlineKeyboardMarkup(menu)
diff --git a/bot/modules/auth.py b/bot/modules/auth.py
index a4755154..450a8246 100644
--- a/bot/modules/auth.py
+++ b/bot/modules/auth.py
@@ -10,7 +10,7 @@ def authorize(update, context):
user_id = ''
reply_message = update.message.reply_to_message
if len(context.args) == 1:
- user_id = int(context.args[0])
+ user_id = context.args[0]
elif reply_message:
user_id = reply_message.from_user.id
if user_id:
@@ -38,7 +38,7 @@ def unauthorize(update, context):
user_id = ''
reply_message = update.message.reply_to_message
if len(context.args) == 1:
- user_id = int(context.args[0])
+ user_id = context.args[0]
elif reply_message:
user_id = reply_message.from_user.id
if user_id:
diff --git a/bot/modules/eval.py b/bot/modules/eval.py
index f5d481e9..9456f787 100644
--- a/bot/modules/eval.py
+++ b/bot/modules/eval.py
@@ -4,13 +4,11 @@
from contextlib import redirect_stdout
from io import StringIO, BytesIO
-from telegram import ParseMode
from telegram.ext import CommandHandler
from bot import dispatcher
from bot.helper.telegram_helper.bot_commands import BotCommands
from bot.helper.telegram_helper.filters import CustomFilters
-from bot.helper.telegram_helper.message_utils import sendMessage
namespaces = {}
@@ -36,7 +34,7 @@ def send(msg, bot, update):
bot.send_message(
chat_id=update.effective_chat.id,
text=f"`{msg}`",
- parse_mode=ParseMode.MARKDOWN)
+ parse_mode='MARKDOWN')
def evaluate(update, context):
bot = context.bot
diff --git a/bot/modules/shell.py b/bot/modules/shell.py
index c6428ddf..4fc91ca5 100644
--- a/bot/modules/shell.py
+++ b/bot/modules/shell.py
@@ -31,9 +31,9 @@ def shell(update, context):
reply_to_message_id=message.message_id,
chat_id=message.chat_id)
elif len(reply) != 0:
- message.reply_text(reply, parse_mode='HTML')
+ message.reply_text(reply, parse_mode='MARKDOWN')
else:
- message.reply_text('Command executed', parse_mode='HTML')
+ message.reply_text('Command executed', parse_mode='MARKDOWN')
shell_handler = CommandHandler(BotCommands.ShellCommand, shell,
filters=CustomFilters.owner_filter, run_async=True)