diff --git a/README.md b/README.md index ab4023f781d..5478f25724e 100644 --- a/README.md +++ b/README.md @@ -100,8 +100,9 @@ Thanks to: - [Dank-del](https://github.com/Dank-del/) for base repo - [magneto261290](https://github.com/magneto261290/) for some features - [SVR666](https://github.com/SVR666/) for some fixes -- [iamLiquidX](https://github.com/iamLiquidX/) for Speedtest module - [4amparaboy](https://github.com/4amparaboy/) for some help - [WinTenDev](https://github.com/WinTenDev/) for Uptobox support +- [iamLiquidX](https://github.com/iamLiquidX/) for Speedtest module +- [ydner](https://github.com/ydner/) for Usage module and many more people who aren't mentioned here, but may be found in [Contributors](https://github.com/breakdowns/slam-mirrorbot/graphs/contributors). diff --git a/app.json b/app.json index 19e9496ae3c..480fcf9974f 100644 --- a/app.json +++ b/app.json @@ -66,6 +66,16 @@ "description": "This is to authenticate to your telegram account for downloading Telegram files. You can get this from https://my.telegram.org.", "required": true }, + "HEROKU_API_KEY": { + "description": "Your Heroku API key, get it from https://dashboard.heroku.com/account.", + "value": "", + "required": true + }, + "HEROKU_APP_NAME": { + "description": "Add the Heroku app name here. It helps with userbot updates.", + "value": "", + "required": true + }, "UPTOBOX_TOKEN": { "description": "Uptobox token to mirror uptobox links. Get it from [Uptobox Premium Account](https://uptobox.com/my_account).", "required": false diff --git a/bot/__init__.py b/bot/__init__.py index 308776ee111..4ef68c5a8db 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -86,6 +86,8 @@ def getConfig(name: str): AUTO_DELETE_MESSAGE_DURATION = int(getConfig('AUTO_DELETE_MESSAGE_DURATION')) TELEGRAM_API = getConfig('TELEGRAM_API') TELEGRAM_HASH = getConfig('TELEGRAM_HASH') + HEROKU_API_KEY = getConfig('HEROKU_API_KEY') + HEROKU_APP_NAME = getConfig('HEROKU_APP_NAME') except KeyError as e: LOGGER.error("One or more env variables missing! Exiting now") exit(1) diff --git a/bot/__main__.py b/bot/__main__.py index 00ee964bc5d..165bad0c99a 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -14,7 +14,7 @@ from bot.helper.telegram_helper.message_utils import * from .helper.ext_utils.bot_utils import get_readable_file_size, get_readable_time from .helper.telegram_helper.filters import CustomFilters -from .modules import authorize, list, cancel_mirror, mirror_status, mirror, clone, watch, shell, eval, anime, stickers, search, delete, speedtest +from .modules import authorize, list, cancel_mirror, mirror_status, mirror, clone, watch, shell, eval, anime, stickers, search, delete, speedtest, usage @run_async @@ -104,6 +104,9 @@ def bot_help(update, context): /{BotCommands.SpeedCommand}: Check Internet Speed of the Host + +/{BotCommands.UsageCommand}: To see Heroku Dyno Stats (Owner and Sudo only). + /tshelp: Get help for torrent search module. /weebhelp: Get help for anime, manga and character module. diff --git a/bot/helper/telegram_helper/bot_commands.py b/bot/helper/telegram_helper/bot_commands.py index 3803b9f36ca..433d9c907ab 100644 --- a/bot/helper/telegram_helper/bot_commands.py +++ b/bot/helper/telegram_helper/bot_commands.py @@ -20,5 +20,6 @@ def __init__(self): self.WatchCommand = 'watch' self.TarWatchCommand = 'tarwatch' self.DeleteCommand = 'del' + self.UsageCommand = 'usage' BotCommands = _BotCommands() diff --git a/bot/modules/usage.py b/bot/modules/usage.py new file mode 100644 index 00000000000..b5790f7e65c --- /dev/null +++ b/bot/modules/usage.py @@ -0,0 +1,79 @@ +import math + +import requests +import heroku3 + +from bot import dispatcher, HEROKU_APP_NAME, HEROKU_API_KEY +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 sendMessage +from telegram import update +from telegram.ext import run_async, CommandHandler + + +@run_async +def dyno_usage(update, context): + heroku_api = "https://api.heroku.com" + if HEROKU_API_KEY is not None and HEROKU_APP_NAME is not None: + Heroku = heroku3.from_key(HEROKU_API_KEY) + app = Heroku.app(HEROKU_APP_NAME) + else: + sendMessage( + "Please insert your Heroku App Name and API Key in Vars", + context.bot, + update + ) + useragent = ( + "Mozilla/5.0 (Linux; Android 10; SM-G975F) " + "AppleWebKit/537.36 (KHTML, like Gecko) " + "Chrome/81.0.4044.117 Mobile Safari/537.36" + ) + user_id = Heroku.account().id + headers = { + "User-Agent": useragent, + "Authorization": f"Bearer {HEROKU_API_KEY}", + "Accept": "application/vnd.heroku+json; version=3.account-quotas", + } + path = "/accounts/" + user_id + "/actions/get-quota" + session = requests.Session() + with session as ses: + with ses.get(heroku_api + path, headers=headers) as r: + result = r.json() + """Account Quota.""" + quota = result["account_quota"] + quota_used = result["quota_used"] + quota_remain = quota - quota_used + quota_percent = math.floor(quota_remain / quota * 100) + minutes_remain = quota_remain / 60 + hours = math.floor(minutes_remain / 60) + minutes = math.floor(minutes_remain % 60) + + """App Quota.""" + Apps = result["apps"] + for apps in Apps: + if apps.get("app_uuid") == app.id: + AppQuotaUsed = apps.get("quota_used") / 60 + AppPercent = math.floor(apps.get("quota_used") * 100 / quota) + break + else: + AppQuotaUsed = 0 + AppPercent = 0 + + AppHours = math.floor(AppQuotaUsed / 60) + AppMinutes = math.floor(AppQuotaUsed % 60) + + sendMessage( + f"Dyno Usage for {app.name} :\n" + f"• {AppHours} Hours and {AppMinutes} Minutes - {AppPercent}%\n\n" + "Dyno Remaining this month :\n" + f"• {hours} Hours and {minutes} Minutes - {quota_percent}%", + context.bot, + update + ) + return True + + +dyno_usage_handler = CommandHandler(command=BotCommands.UsageCommand, callback=dyno_usage, + filters=CustomFilters.owner_filter) + +dispatcher.add_handler(dyno_usage_handler) diff --git a/requirements.txt b/requirements.txt index a36385a34a5..267a98232f9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,6 +16,7 @@ TgCrypto youtube_dl feedparser natsort +heroku3 aiohttp speedtest-cli>=2.1.2 messages