From 3a3eca241179eea27979462e31c0edd526944e6c Mon Sep 17 00:00:00 2001 From: jaskaranSM Date: Fri, 1 Nov 2019 21:18:46 +0530 Subject: [PATCH] Refactored disk to stats and added bot uptime - Also helps tracking auto reboots on Heroku. --- bot/__init__.py | 3 ++- bot/__main__.py | 22 ++++++++++++---------- bot/helper/telegram_helper/bot_commands.py | 2 +- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/bot/__init__.py b/bot/__init__.py index 219a565..4507171 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -4,8 +4,9 @@ import aria2p import threading from telegram.ext import Updater import os +import time - +botStartTime = time.time() if os.path.exists('log.txt'): with open('log.txt', 'r+') as f: f.truncate(0) diff --git a/bot/__main__.py b/bot/__main__.py index 11afa83..eb534f5 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -1,7 +1,7 @@ from telegram.ext import CommandHandler, run_async -from bot import dispatcher, LOGGER, updater +from bot import dispatcher, LOGGER, updater, botStartTime from bot.helper.ext_utils import fs_utils -from .helper.ext_utils.bot_utils import get_readable_file_size +from .helper.ext_utils.bot_utils import get_readable_file_size, get_readable_time import signal import time from bot.helper.telegram_helper.message_utils import * @@ -11,16 +11,18 @@ from bot.helper.telegram_helper.bot_commands import BotCommands from .modules import authorize, list, cancel_mirror, mirror_status, mirror -@run_async -def disk_usage(update, context): +def stats(update, context): + currentTime = get_readable_time((time.time() - botStartTime)) total, used, free = shutil.disk_usage('.') total = get_readable_file_size(total) used = get_readable_file_size(used) free = get_readable_file_size(free) - disk_usage_string = f'Total disk space: {total}\n' \ + stats = f'Bot Uptime: {currentTime}\n' \ + f'Total disk space: {total}\n' \ f'Used: {used}\n' \ f'Free: {free}' - sendMessage(disk_usage_string, context, update) + sendMessage(stats, context, update) + @run_async @@ -57,7 +59,7 @@ def bot_help(update, context): /{BotCommands.ListCommand} [search term]: Searches the search term in the Google drive, if found replies with the link -/{BotCommands.DiskCommand}: Show a status of the disk usage of the machine the bot is hosted on +/{BotCommands.StatsCommand}: Show Stats of the machine the bot is hosted on /{BotCommands.AuthorizeCommand}: Authorize a chat or a user to use the bot (Can only be invoked by owner of the bot) @@ -75,13 +77,13 @@ def main(): filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) help_handler = CommandHandler(BotCommands.HelpCommand, bot_help, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) - disk_handler = CommandHandler(BotCommands.DiskCommand, - disk_usage, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) + stats_handler = CommandHandler(BotCommands.StatsCommand, + stats, filters=CustomFilters.authorized_chat | CustomFilters.authorized_user) log_handler = CommandHandler(BotCommands.LogCommand, log, filters=CustomFilters.owner_filter) dispatcher.add_handler(start_handler) dispatcher.add_handler(ping_handler) dispatcher.add_handler(help_handler) - dispatcher.add_handler(disk_handler) + dispatcher.add_handler(stats_handler) dispatcher.add_handler(log_handler) updater.start_polling() LOGGER.info("Bot Started!") diff --git a/bot/helper/telegram_helper/bot_commands.py b/bot/helper/telegram_helper/bot_commands.py index be1833c..918500d 100644 --- a/bot/helper/telegram_helper/bot_commands.py +++ b/bot/helper/telegram_helper/bot_commands.py @@ -10,7 +10,7 @@ class _BotCommands: self.AuthorizeCommand = 'authorize' self.UnAuthorizeCommand = 'unauthorize' self.PingCommand = 'ping' - self.DiskCommand = 'disk' + self.StatsCommand = 'stats' self.HelpCommand = 'help' self.LogCommand = 'log'