Skip to content

Commit

Permalink
Fix Index Link appearance issue in clone module
Browse files Browse the repository at this point in the history
  • Loading branch information
l3v11 authored Oct 4, 2022
1 parent 4381651 commit ed8bb1e
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 76 deletions.
28 changes: 8 additions & 20 deletions bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,32 +80,25 @@
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'

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)

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)
Expand Down Expand Up @@ -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:
Expand Down
67 changes: 34 additions & 33 deletions bot/helper/drive_utils/gdriveTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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('<', '')
Expand All @@ -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 = {
Expand All @@ -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):
Expand Down Expand Up @@ -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,
Expand All @@ -316,25 +316,25 @@ 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)
return file_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'):
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand All @@ -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

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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:
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion bot/helper/ext_utils/fs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 15 additions & 15 deletions bot/helper/telegram_helper/button_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions bot/modules/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 1 addition & 3 deletions bot/modules/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions bot/modules/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('<b>Command executed</b>', parse_mode='HTML')
message.reply_text('<b>Command executed</b>', parse_mode='MARKDOWN')

shell_handler = CommandHandler(BotCommands.ShellCommand, shell,
filters=CustomFilters.owner_filter, run_async=True)
Expand Down

0 comments on commit ed8bb1e

Please sign in to comment.