Skip to content

Commit

Permalink
Added buttons in bot reply
Browse files Browse the repository at this point in the history
  • Loading branch information
SVR666 committed Jul 13, 2020
1 parent 69c53b0 commit 412b8ec
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
24 changes: 15 additions & 9 deletions bot/helper/mirror_utils/upload_utils/gdriveTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
from googleapiclient.http import MediaFileUpload
from tenacity import *

from telegram import InlineKeyboardMarkup
from bot.helper.telegram_helper import button_builder

from bot import parent_id, DOWNLOAD_DIR, IS_TEAM_DRIVE, INDEX_URL, \
USE_SERVICE_ACCOUNTS, download_dict
from bot.helper.ext_utils.bot_utils import *
Expand Down Expand Up @@ -303,37 +306,40 @@ def clone(self, link):
file_id = self.getIdFromUrl(link)
except (KeyError,IndexError):
msg = "Google drive ID could not be found in the provided link"
return msg
return msg, ""
msg = ""
LOGGER.info(f"File ID: {file_id}")
try:
meta = self.getFileMetadata(file_id)
if meta.get("mimeType") == self.__G_DRIVE_DIR_MIME_TYPE:
dir_id = self.create_directory(meta.get('name'), parent_id)
result = self.cloneFolder(meta.get('name'), meta.get('name'), meta.get('id'), dir_id)
msg += f'<a href="{self.__G_DRIVE_DIR_BASE_DOWNLOAD_URL.format(dir_id)}">{meta.get("name")}</a>' \
f' ({get_readable_file_size(self.transferred_size)})'
msg += f'<b>Filename : </b><code>{meta.get("name")}</code>\n<b>Size : </b>{get_readable_file_size(self.transferred_size)}'
buttons = button_builder.ButtonMaker()
buttons.buildbutton("⚡Drive Link⚡", self.__G_DRIVE_DIR_BASE_DOWNLOAD_URL.format(dir_id))
if INDEX_URL is not None:
url = requests.utils.requote_uri(f'{INDEX_URL}/{meta.get("name")}/')
msg += f' | <a href="{url}"> Index URL</a>'
buttons.buildbutton("💥Index Link💥", url)
else:
file = self.copyFile(meta.get('id'), parent_id)
msg += f'<a href="{self.__G_DRIVE_BASE_DOWNLOAD_URL.format(file.get("id"))}">{file.get("name")}</a>'
msg += f'<b>Filename : </b><code>{file.get("name")}</code>'
buttons = button_builder.ButtonMaker()
buttons.buildbutton("⚡Drive Link⚡", self.__G_DRIVE_BASE_DOWNLOAD_URL.format(file.get("id")))
try:
msg += f' ({get_readable_file_size(int(meta.get("size")))}) '
msg += f'\n<b>Size : </b><code>{get_readable_file_size(int(meta.get("size")))}</code>'
except TypeError:
pass
if INDEX_URL is not None:
url = requests.utils.requote_uri(f'{INDEX_URL}/{file.get("name")}')
msg += f' | <a href="{url}"> Index URL</a>'
buttons.buildbutton("💥Index Link💥", url)
except Exception as err:
if isinstance(err, RetryError):
LOGGER.info(f"Total Attempts: {err.last_attempt.attempt_number}")
err = err.last_attempt.exception()
err = str(err).replace('>', '').replace('<', '')
LOGGER.error(err)
return err
return msg
return err, ""
return msg, InlineKeyboardMarkup(buttons.build_menu(2))

def cloneFolder(self, name, local_path, folder_id, parent_id):
LOGGER.info(f"Syncing: {local_path}")
Expand Down
7 changes: 5 additions & 2 deletions bot/modules/clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ def cloneNode(update,context):
link = args[1]
msg = sendMessage(f"Cloning: <code>{link}</code>",context.bot,update)
gd = GoogleDriveHelper()
result = gd.clone(link)
result, button = gd.clone(link)
deleteMessage(context.bot,msg)
sendMessage(result,context.bot,update)
if button == "":
sendMessage(result,context.bot,update)
else:
sendMarkup(result,context.bot,update,button)
else:
sendMessage("Provide G-Drive Shareable Link to Clone.",context.bot,update)

Expand Down
12 changes: 8 additions & 4 deletions bot/modules/mirror.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import requests
from telegram.ext import CommandHandler, run_async
from telegram import InlineKeyboardMarkup

from bot import Interval, INDEX_URL
from bot import dispatcher, DOWNLOAD_DIR, DOWNLOAD_STATUS_UPDATE_INTERVAL, download_dict, download_dict_lock
Expand All @@ -18,6 +19,7 @@
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 *
from bot.helper.telegram_helper import button_builder
import pathlib
import os
import subprocess
Expand Down Expand Up @@ -136,22 +138,24 @@ def onUploadProgress(self):

def onUploadComplete(self, link: str):
with download_dict_lock:
msg = f'<a href="{link}">{download_dict[self.uid].name()}</a> ({download_dict[self.uid].size()})'
msg = f'<b>Filename : </b><code>{download_dict[self.uid].name()}</code>\n<b>Size : </b><code>{download_dict[self.uid].size()}</code>'
buttons = button_builder.ButtonMaker()
buttons.buildbutton("⚡Drive Link⚡", link)
LOGGER.info(f'Done Uploading {download_dict[self.uid].name()}')
if INDEX_URL is not None:
share_url = requests.utils.requote_uri(f'{INDEX_URL}/{download_dict[self.uid].name()}')
if os.path.isdir(f'{DOWNLOAD_DIR}/{self.uid}/{download_dict[self.uid].name()}'):
share_url += '/'
msg += f'\n\n Shareable link: <a href="{share_url}">here</a>'
buttons.buildbutton("💥Index Link💥", share_url)
if self.tag is not None:
msg += f'\ncc: @{self.tag}'
msg += f'\n\nHey @{self.tag}, your file is uploaded'
try:
fs_utils.clean_download(download_dict[self.uid].path())
except FileNotFoundError:
pass
del download_dict[self.uid]
count = len(download_dict)
sendMessage(msg, self.bot, self.update)
sendMarkup(msg, self.bot, self.update, InlineKeyboardMarkup(buttons.build_menu(2)))
if count == 0:
self.clean()
else:
Expand Down

0 comments on commit 412b8ec

Please sign in to comment.