Skip to content

Commit

Permalink
Add custom name support in compress module
Browse files Browse the repository at this point in the history
  • Loading branch information
l3v11 committed Sep 4, 2022
1 parent 7ed265c commit 1846978
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
6 changes: 3 additions & 3 deletions bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ def restart(update, context):
<br><br>
• <b>/{BotCommands.CancelCommand}</b> &lt;gid&gt;: Cancel a task
<br><br>
• <b>/{BotCommands.StatusCommand}</b>: Get a status of all tasks
• <b>/{BotCommands.StatusCommand}</b>: Get status of all tasks
<br><br>
• <b>/{BotCommands.ListKeysCommand}</b>: Get a list of keys for the destination drives
• <b>/{BotCommands.ListKeysCommand}</b>: Get the list of destination drives keys
<br><br>
• <b>/{BotCommands.PingCommand}</b>: Ping the bot
<br><br>
Expand All @@ -114,7 +114,7 @@ def restart(update, context):
<br><br>
• <b>/{BotCommands.UnauthorizeCommand}</b>: Unauthorize an user or a chat for using the bot
<br><br>
• <b>/{BotCommands.UsersCommand}</b>: Get a list of authorized chats
• <b>/{BotCommands.UsersCommand}</b>: Get the list of authorized users
<br><br>
• <b>/{BotCommands.ShellCommand}</b> &lt;cmd&gt;: Run commands in terminal
<br><br>
Expand Down
4 changes: 3 additions & 1 deletion bot/helper/download_utils/gd_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
from bot.helper.telegram_helper.message_utils import sendMessage, deleteMessage, sendStatusMessage
from bot.helper.ext_utils.bot_utils import get_readable_file_size

def add_gd_download(link, path, listener, is_appdrive, appdict, is_gdtot):
def add_gd_download(link, path, listener, customname, is_appdrive, appdict, is_gdtot):
msg = sendMessage(f"<b>Checking:</b> <code>{link}</code>", listener.bot, listener.message)
LOGGER.info(f"Checking: {link}")
gd = GoogleDriveHelper()
res, size, name, files = gd.helper(link)
deleteMessage(listener.bot, msg)
if res != "":
return sendMessage(res, listener.bot, listener.message)
if customname:
name = customname
if COMPRESS_LIMIT is not None:
if size > COMPRESS_LIMIT * 1024**3:
msg2 = f"<b>Name:</b> <code>{name}</code>"
Expand Down
26 changes: 13 additions & 13 deletions bot/helper/drive_utils/gdriveTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def helper(self, link):
return self.helper(link)
msg = "File not found"
else:
msg = str(err)
msg = err
return msg, "", "", ""
return "", size, name, files

Expand All @@ -239,16 +239,17 @@ def deleteFile(self, link: str):
supportsAllDrives=IS_TEAM_DRIVE).execute()
msg = "Permanently deleted"
except HttpError as err:
if "File not found" in str(err):
err = str(err).replace('>', '').replace('<', '')
if "File not found" in err:
msg = "File not found"
elif "insufficientFilePermissions" in str(err):
elif "insufficientFilePermissions" in err:
token_service = self.__alt_authorize()
if token_service is not None:
self.__service = token_service
return self.deleteFile(link)
msg = "Insufficient file permissions"
else:
msg = str(err)
msg = err
LOGGER.error(msg)
return msg

Expand Down Expand Up @@ -291,17 +292,16 @@ def setPermission(self, link, access):
msg = "Set permission to <code>Anyone with the link</code>"
except HttpError as err:
err = str(err).replace('>', '').replace('<', '')
LOGGER.error(err)
if "File not found" in str(err):
if "File not found" in err:
msg = "File not found"
elif "insufficientFilePermissions" in str(err):
elif "insufficientFilePermissions" in err:
token_service = self.__alt_authorize()
if token_service is not None:
self.__service = token_service
return self.setPermission(link, access)
msg = "Insufficient file permissions"
else:
msg = str(err)
msg = err
return msg

@retry(wait=wait_exponential(multiplier=2, min=3, max=6),
Expand Down Expand Up @@ -438,7 +438,7 @@ def clone(self, link, key):
return self.clone(link, key)
msg = "File not found"
else:
msg = str(err)
msg = err
return msg

def count(self, link):
Expand Down Expand Up @@ -479,7 +479,7 @@ def count(self, link):
return self.count(link)
msg = "File not found"
else:
msg = str(err)
msg = err
return msg

def _progress(self):
Expand Down Expand Up @@ -677,10 +677,10 @@ def download(self, link):
try:
meta = self.__getFileMetadata(file_id)
if meta.get("mimeType") == self.__G_DRIVE_DIR_MIME_TYPE:
self.__download_folder(file_id, self.__path, meta.get('name'))
self.__download_folder(file_id, self.__path, self.name)
else:
os.makedirs(self.__path)
self.__download_file(file_id, self.__path, meta.get('name'), meta.get('mimeType'))
self.__download_file(file_id, self.__path, self.name, meta.get('mimeType'))
except Exception as err:
if isinstance(err, RetryError):
LOGGER.info(f"Total attempts: {err.last_attempt.attempt_number}")
Expand Down Expand Up @@ -749,7 +749,7 @@ def __receive_callback(self, request_id, response, exception):
# request_id = order number of request = shared drive index (1 based)
if exception is not None:
exception = str(exception).replace('>', '').replace('<', '')
LOGGER.error(str(exception))
LOGGER.error(exception)
else:
if response['files']:
self.response[request_id] = response
Expand Down
12 changes: 10 additions & 2 deletions bot/modules/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def onUploadError(self, error):
def _compress(bot, message, is_archive=False, is_extract=False, pswd=None):
mesg = message.text.split('\n')
message_args = mesg[0].split(maxsplit=1)
reply_to = message.reply_to_message
name_arg = mesg[0].split('|', maxsplit=1)
is_appdrive = False
is_gdtot = False
appdict = ''
Expand All @@ -195,13 +195,20 @@ def _compress(bot, message, is_archive=False, is_extract=False, pswd=None):
link = ''
else:
link = ''
if len(name_arg) > 1:
name = name_arg[1]
name = name.split(' pswd:')[0]
name = name.strip()
else:
name = ''
link = re.split(r"pswd:|\|", link)[0]
link = link.strip()
pswd_arg = mesg[0].split(' pswd: ')
if len(pswd_arg) > 1:
pswd = pswd_arg[1]
else:
pswd = None
reply_to = message.reply_to_message
if reply_to is not None:
link = reply_to.text.split(maxsplit=1)[0].strip()
is_appdrive = is_appdrive_link(link)
Expand All @@ -222,10 +229,11 @@ def _compress(bot, message, is_archive=False, is_extract=False, pswd=None):
return sendMessage(str(e), bot, message)
listener = CompressListener(bot, message, is_archive, is_extract, pswd)
if is_gdrive_link(link):
threading.Thread(target=add_gd_download, args=(link, f'{DOWNLOAD_DIR}{listener.uid}', listener, is_appdrive, appdict, is_gdtot)).start()
threading.Thread(target=add_gd_download, args=(link, f'{DOWNLOAD_DIR}{listener.uid}', listener, name, is_appdrive, appdict, is_gdtot)).start()
else:
help_msg = '<b><u>Instructions</u></b>\nSend a link along with command'
help_msg += '\n\n<b><u>Supported Sites</u></b>\n• Google Drive\n• AppDrive\n• GDToT'
help_msg += '\n\n<b><u>Set Custom Name</u></b>\nAdd "<code>|customname</code>" after the link'
help_msg += '\n\n<b><u>Set Password</u></b>\nAdd "<code>pswd: xxx</code>" after the link'
sendMessage(help_msg, bot, message)

Expand Down

0 comments on commit 1846978

Please sign in to comment.