Skip to content

Commit

Permalink
BREAKING CHANGES
Browse files Browse the repository at this point in the history
- Implement Batch HTTP Request in the drive_query function
- Remove index link support from drive_list function
- Add multi-page telegraph support
- Remove regex from drive_query function for better results
- Add ping module
- Set retry attempt to 3
- Improve GDToT function
- Remove unnecessary functions
- Add multi-token telegraph support
- Tidying up
  • Loading branch information
l3v11 authored Mar 16, 2022
1 parent 0b8e194 commit 63a4eb8
Show file tree
Hide file tree
Showing 16 changed files with 244 additions and 223 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

## SearchX

> A simple Telegram Bot for searching data on Google Drive. Able to clone data from Drive or GDToT links. Supports MongoDB for storing authorized users record.
> A simple Telegram Bot for searching data on Google Drive. Able to clone data from Drive / AppDrive / DriveApp / GDToT links. Supports MongoDB for storing authorized users record.
</p>

Expand Down
30 changes: 17 additions & 13 deletions bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import requests
import subprocess
import socket
import time

import telegram.ext as tg

Expand Down Expand Up @@ -64,6 +65,11 @@ def get_config(name: str):
BOT_TOKEN = get_config('BOT_TOKEN')
OWNER_ID = int(get_config('OWNER_ID'))
parent_id = get_config('DRIVE_FOLDER_ID')
TELEGRAPH_ACCS = None
try:
TELEGRAPH_ACCS = int(get_config('TELEGRAPH_ACCS'))
except ValueError:
TELEGRAPH_ACCS = 2
except KeyError:
LOGGER.error("One or more env variables are missing")
exit(1)
Expand Down Expand Up @@ -140,6 +146,7 @@ def get_config(name: str):
LOGGER.error(f"Failed to load accounts.zip file [{res.status_code}]")
raise KeyError
subprocess.run(["unzip", "-q", "-o", "accounts.zip"])
subprocess.run(["chmod", "-R", "777", "accounts"])
os.remove("accounts.zip")
except KeyError:
pass
Expand All @@ -162,7 +169,6 @@ def get_config(name: str):

DRIVE_NAME = []
DRIVE_ID = []
INDEX_URL = []

if os.path.exists('drive_list'):
with open('drive_list', 'r+') as f:
Expand All @@ -171,24 +177,22 @@ def get_config(name: str):
temp = line.strip().split()
DRIVE_NAME.append(temp[0].replace("_", " "))
DRIVE_ID.append(temp[1])
try:
INDEX_URL.append(temp[2])
except IndexError:
INDEX_URL.append(None)

if DRIVE_ID:
pass
else:
LOGGER.error("drive_list file is missing")
exit(1)

# Generate Telegraph Token
sname = ''.join(random.SystemRandom().choices(string.ascii_letters, k=8))
LOGGER.info("Generating TELEGRAPH_TOKEN using '" + sname + "' name")
telegraph = Telegraph()
telegraph.create_account(short_name=sname)
telegraph_token = telegraph.get_access_token()
telegra_ph = Telegraph(access_token=telegraph_token)
telegraph = []

for i in range(TELEGRAPH_ACCS):
sname = ''.join(random.SystemRandom().choices(string.ascii_letters, k=8))
telegra_ph = Telegraph()
telegra_ph.create_account(short_name=sname)
telegraph_token = telegra_ph.get_access_token()
telegraph.append(Telegraph(access_token=telegraph_token))
time.sleep(0.5)
LOGGER.info(f"Generated {TELEGRAPH_ACCS} telegraph tokens")

updater = tg.Updater(token=BOT_TOKEN, use_context=True)
bot = updater.bot
Expand Down
25 changes: 18 additions & 7 deletions bot/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import time

from telegram.ext import CommandHandler

from bot import AUTHORIZED_CHATS, dispatcher, updater
Expand All @@ -9,14 +11,20 @@
def start(update, context):
if CustomFilters.authorized_user(update) or CustomFilters.authorized_chat(update):
if update.message.chat.type == "private":
sendMessage(f"Access granted", context.bot, update)
sendMessage("<b>Access granted</b>", context.bot, update)
else:
sendMessage(f"I'm alive :)", context.bot, update)
sendMessage("<b>I'm alive :)</b>", context.bot, update)
LOGGER.info('Granted: {} [{}]'.format(update.message.from_user.first_name, update.message.from_user.id))
else:
sendMessage(f"Access denied", context.bot, update)
sendMessage("<b>Access denied</b>", context.bot, update)
LOGGER.info('Denied: {} [{}]'.format(update.message.from_user.first_name, update.message.from_user.id))

def ping(update, context):
start_time = int(round(time.time() * 1000))
reply = sendMessage("<b>Pong!</b>", context.bot, update)
end_time = int(round(time.time() * 1000))
editMessage(f'<code>{end_time - start_time}ms</code>', reply)

def bot_help(update, context):
help_string = f'''
<u><i><b>Usage:</b></i></u>
Expand All @@ -33,7 +41,7 @@ def bot_help(update, context):
/{BotCommands.ListCommand} [query]: Search data on Drives
/{BotCommands.CloneCommand} [url]: Copy data from drive/appdrive/gdtot to Drive
/{BotCommands.CloneCommand} [url]: Copy data from Drive / AppDrive / DriveApp / GDToT to Drive
/{BotCommands.CountCommand} [drive_url]: Count data of Drive
Expand All @@ -49,26 +57,29 @@ def bot_help(update, context):
/{BotCommands.ShellCommand} [cmd]: Execute bash commands (Only owner)
/{BotCommands.PingCommand}: Ping the bot
/{BotCommands.LogCommand}: Get the log file (Only owner)
/{BotCommands.HelpCommand}: Get this message
'''
sendMessage(help_string, context.bot, update)

def log(update, context):
send_log_file(context.bot, update)
sendLogFile(context.bot, update)

def main():
start_handler = CommandHandler(BotCommands.StartCommand, start, run_async=True)
ping_handler = CommandHandler(BotCommands.PingCommand, ping,
filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True)
help_handler = CommandHandler(BotCommands.HelpCommand, bot_help,
filters=CustomFilters.authorized_chat | CustomFilters.authorized_user, run_async=True)
log_handler = CommandHandler(BotCommands.LogCommand, log,
filters=CustomFilters.owner_filter, run_async=True)

dispatcher.add_handler(start_handler)
dispatcher.add_handler(ping_handler)
dispatcher.add_handler(help_handler)
dispatcher.add_handler(log_handler)

updater.start_polling()
LOGGER.info("Bot started")
updater.idle()
Expand Down
Loading

0 comments on commit 63a4eb8

Please sign in to comment.