Skip to content

Commit

Permalink
v4.0 (breakdowns#23)
Browse files Browse the repository at this point in the history
- Added /usage command to see heroku dynos
  • Loading branch information
breakdowns authored Mar 11, 2021
1 parent db7e8b5 commit 44bfe96
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
10 changes: 10 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions bot/helper/telegram_helper/bot_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ def __init__(self):
self.WatchCommand = 'watch'
self.TarWatchCommand = 'tarwatch'
self.DeleteCommand = 'del'
self.UsageCommand = 'usage'

BotCommands = _BotCommands()
79 changes: 79 additions & 0 deletions bot/modules/usage.py
Original file line number Diff line number Diff line change
@@ -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"<b>Dyno Usage for</b> <code>{app.name}</code> :\n"
f"• <code>{AppHours}</code> <b>Hours and</b> <code>{AppMinutes}</code> <b>Minutes - {AppPercent}%</b>\n\n"
"<b>Dyno Remaining this month :</b>\n"
f"• <code>{hours}</code> <b>Hours and</b> <code>{minutes}</code> <b>Minutes - {quota_percent}%</b>",
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)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ TgCrypto
youtube_dl
feedparser
natsort
heroku3
aiohttp
speedtest-cli>=2.1.2
messages
Expand Down

0 comments on commit 44bfe96

Please sign in to comment.