Skip to content

Commit

Permalink
Add support for switching to random SA on each task
Browse files Browse the repository at this point in the history
- Improve code quality
- Fix bugs
- Tidy up
  • Loading branch information
l3v11 authored Aug 28, 2022
1 parent 4a08b66 commit de02d6f
Show file tree
Hide file tree
Showing 19 changed files with 608 additions and 1,073 deletions.
16 changes: 8 additions & 8 deletions bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,6 @@ def get_config(name: str):
# Value: telegram.Message
status_reply_dict = {}

try:
users = get_config('AUTHORIZED_CHATS')
users = users.split()
for user in users:
AUTHORIZED_CHATS.add(int(user.strip()))
except:
pass

try:
BOT_TOKEN = get_config('BOT_TOKEN')
except:
Expand All @@ -101,6 +93,14 @@ def get_config(name: str):
LOGGER.error("DOWNLOAD_DIR env variable is missing")
exit(1)

try:
users = get_config('AUTHORIZED_CHATS')
users = users.split()
for user in users:
AUTHORIZED_CHATS.add(int(user.strip()))
except:
pass

try:
DATABASE_URL = get_config('DATABASE_URL')
if len(DATABASE_URL) == 0:
Expand Down
37 changes: 15 additions & 22 deletions bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from psutil import cpu_percent, cpu_count, disk_usage, virtual_memory, net_io_counters
from sys import executable
from telegram import InlineKeyboardMarkup
from telegram.ext import CommandHandler

from bot import bot, LOGGER, botStartTime, AUTHORIZED_CHATS, DEST_DRIVES, TELEGRAPH, Interval, dispatcher, updater
Expand Down Expand Up @@ -38,26 +37,16 @@ def ping(update, context):
editMessage(f'<code>{end_time - start_time}ms</code>', reply)

def stats(update, context):
uptime = get_readable_time(time.time() - botStartTime)
total, used, free, disk = disk_usage('/')
total = get_readable_file_size(total)
used = get_readable_file_size(used)
free = get_readable_file_size(free)
sent = get_readable_file_size(net_io_counters().bytes_sent)
recv = get_readable_file_size(net_io_counters().bytes_recv)
cpu = cpu_percent(interval=0.5)
ram = virtual_memory().percent
p_core = cpu_count(logical=False)
l_core = cpu_count(logical=True)
stats = '⚙️ <u><b>SYSTEM STATISTICS</b></u>' \
f'\n\n<b>Total Disk Space:</b> {total}' \
f'\n<b>Used:</b> {used} | <b>Free:</b> {free}' \
f'\n\n<b>Upload:</b> {sent}' \
f'\n<b>Download:</b> {recv}' \
f'\n\n<b>Physical Cores:</b> {p_core}' \
f'\n<b>Logical Cores:</b> {l_core}' \
f'\n\n<b>CPU:</b> {cpu}% | <b>RAM:</b> {ram}%' \
f'\n<b>DISK:</b> {disk}% | <b>Uptime:</b> {uptime}'
f'\n\n<b>Total Disk Space:</b> {get_readable_file_size(total)}' \
f'\n<b>Used:</b> {get_readable_file_size(used)} | <b>Free:</b> {get_readable_file_size(free)}' \
f'\n\n<b>Upload:</b> {get_readable_file_size(net_io_counters().bytes_sent)}' \
f'\n<b>Download:</b> {get_readable_file_size(net_io_counters().bytes_recv)}' \
f'\n\n<b>Physical Cores:</b> {cpu_count(logical=False)}' \
f'\n<b>Logical Cores:</b> {cpu_count(logical=True)}' \
f'\n\n<b>CPU:</b> {cpu_percent(interval=0.5)}% | <b>RAM:</b> {virtual_memory().percent}%' \
f'\n<b>DISK:</b> {disk}% | <b>Uptime:</b> {get_readable_time(time.time() - botStartTime)}'
sendMessage(stats, context.bot, update.message)

def log(update, context):
Expand Down Expand Up @@ -129,7 +118,11 @@ def restart(update, context):
<br><br>
• <b>/{BotCommands.ShellCommand}</b> &lt;cmd&gt;: Run commands in terminal
<br><br>
• <b>/{BotCommands.ExecHelpCommand}</b>: Get help about executor
• <b>/{BotCommands.EvalCommand}</b>: Evaluate Python expressions using eval() function
<br><br>
• <b>/{BotCommands.ExecCommand}</b>: Execute Python code using exec() function
<br><br>
• <b>/{BotCommands.ClearLocalsCommand}</b>: Clear locals of eval() and exec() functions
<br><br>
• <b>/{BotCommands.LogCommand}</b>: Get the log file
<br><br>
Expand All @@ -146,14 +139,14 @@ def bot_help(update, context):
button = ButtonMaker()
button.build_button("User", f"https://graph.org/{help_user}")
button.build_button("Admin", f"https://graph.org/{help_admin}")
sendMarkup(help_string, context.bot, update.message, InlineKeyboardMarkup(button.build_menu(2)))
sendMarkup(help_string, context.bot, update.message, button.build_menu(2))

def main():
start_cleanup()
if os.path.isfile(".restartmsg"):
with open(".restartmsg") as f:
chat_id, msg_id = map(int, f)
bot.editMessageText("<b>Restarted successfully</b>", chat_id, msg_id, parse_mode='HTMl')
bot.editMessageText("<b>Restarted successfully</b>", chat_id, msg_id, parse_mode='HTML')
os.remove(".restartmsg")

start_handler = CommandHandler(BotCommands.StartCommand, start, run_async=True)
Expand Down
4 changes: 2 additions & 2 deletions bot/helper/download_utils/gd_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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, listener, is_appdrive, appdict, is_gdtot):
def add_gd_download(link, path, listener, 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()
Expand All @@ -23,7 +23,7 @@ def add_gd_download(link, listener, is_appdrive, appdict, is_gdtot):
msg2 += "\n\n<b>⚠️ Task failed</b>"
return sendMessage(msg2, listener.bot, listener.message)
LOGGER.info(f"Downloading: {name}")
drive = GoogleDriveHelper(name, listener)
drive = GoogleDriveHelper(name, path, size, listener)
gid = ''.join(random.SystemRandom().choices(string.ascii_letters + string.digits, k=12))
download_status = DownloadStatus(drive, size, listener, gid)
with download_dict_lock:
Expand Down
Loading

0 comments on commit de02d6f

Please sign in to comment.