From 3bdbc73ea9b8ed17a70f962a91831afbce2f59ff Mon Sep 17 00:00:00 2001 From: anas Date: Fri, 10 Sep 2021 14:45:39 +0300 Subject: [PATCH] Minor Fix Signed-off-by: anas --- README.md | 5 +--- alive.py | 11 ++++---- bot/__init__.py | 25 +++---------------- bot/__main__.py | 8 +++--- bot/helper/__init__.py | 25 ------------------- .../download_utils/aria2_download.py | 4 ++- .../download_utils/mega_downloader.py | 4 ++- .../download_utils/telegram_downloader.py | 9 ++----- bot/modules/reboot.py | 18 ------------- config_sample.env | 5 +--- start.sh | 2 +- wserver.py | 11 +++++--- 12 files changed, 33 insertions(+), 94 deletions(-) delete mode 100644 bot/modules/reboot.py diff --git a/README.md b/README.md index f83884d..d6771af 100644 --- a/README.md +++ b/README.md @@ -163,13 +163,10 @@ Fill up rest of the fields. Meaning of each field is discussed below: - `TAR_UNZIP_LIMIT`: To limit the size of mirroring as Tar or unzipmirror. Available units are (gb or GB, tb or TB), Examples: `100 gb, 100 GB, 10 tb, 10 TB` - `VIEW_LINK`: View Link button to open file Index Link in browser instead of direct download link, you can figure out if it's compatible with your Index code or not, open any video from you Index and check if its URL ends with `?a=view`, if yes make it `True` it will work (Compatible with https://gitlab.com/ParveenBhadooOfficial/Google-Drive-Index Code) - `UPTOBOX_TOKEN`: Uptobox token to mirror uptobox links. Get it from [Uptobox Premium Account](https://uptobox.com/my_account). -- `HEROKU_EMAIL`: E-Mail of the Heroku account in which the above app will be deployed (**NOTE**: Only needed if you are deploying on Heroku with Github Workflow). -- `HEROKU_API_KEY`: (Only if you deploying on Heroku) Your Heroku API key, get it from https://dashboard.heroku.com/account. -- `HEROKU_APP_NAME`: (Only if you deploying on Heroku) Your Heroku app name. - `IGNORE_PENDING_REQUESTS`: If you want the bot to ignore pending requests after it restarts, set this to `True`. - `STATUS_LIMIT`: Limit the no. of tasks shown in status message with button. (**NOTE**: Recommended limit is `4` tasks at max). - `IS_VPS`: (Only for VPS) Don't set this to `True` even if you are using VPS, unless facing error with web server. Also go to start.sh and replace `$PORT` by `80` or any port you want to use. -- `SERVER_PORT`: (Only if IS_VPS is `True`) Base URL Port +- `SERVER_PORT`: Only For VPS even if `IS_VPS` is `False` --> Base URL Port - `BASE_URL_OF_BOT`: (Required for Heroku to avoid sleep/idling) Valid BASE URL of app where the bot is deployed. IP/Domain of your bot like `http://myip` or if you have chosen other port then `80` then `http://myip:port`, for Heroku fill `https://yourappname.herokuapp.com` (**NOTE**: Do not put slash at the end), still got idling? You can use http://cron-job.org to ping your Heroku app. - `SHORTENER_API`: Fill your Shortener API key if you are using Shortener. - `SHORTENER`: if you want to use Shortener in G-Drive and index link, fill Shortener URL here. Examples: diff --git a/alive.py b/alive.py index 60cb81b..279f713 100644 --- a/alive.py +++ b/alive.py @@ -7,12 +7,13 @@ import requests import os BASE_URL = os.environ.get('BASE_URL_OF_BOT', None) -if len(BASE_URL) == 0: +try: + if len(BASE_URL) == 0: + BASE_URL = None +except: BASE_URL = None - -IS_VPS = os.environ.get('IS_VPS', 'False') -IS_VPS = IS_VPS.lower() == 'true' -if not IS_VPS and BASE_URL is not None: +PORT = os.environ.get('PORT', None) +if PORT is not None and BASE_URL is not None: while True: time.sleep(600) status = requests.get(BASE_URL).status_code diff --git a/bot/__init__.py b/bot/__init__.py index d145cdc..70a202e 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -46,12 +46,13 @@ if CONFIG_FILE_URL is not None: load_dotenv('config.env') +PORT = os.environ.get('PORT', 'SERVER_PORT') +web = subprocess.Popen([f"gunicorn wserver:start_server --bind 0.0.0.0:{PORT} --worker-class aiohttp.GunicornWebWorker"], shell=True) alive = subprocess.Popen(["python3", "alive.py"]) - subprocess.run(["mkdir", "-p", "qBittorrent/config"]) subprocess.run(["cp", "qBittorrent.conf", "qBittorrent/config/qBittorrent.conf"]) subprocess.run(["qbittorrent-nox", "-d", "--profile=."]) - +time.sleep(0.5) Interval = [] DRIVES_NAMES = [] DRIVES_IDS = [] @@ -156,7 +157,6 @@ try: if len(DB_URI) == 0: raise KeyError except KeyError: - logging.warning('Database not provided!') DB_URI = None if DB_URI is not None: try: @@ -180,7 +180,7 @@ if DB_URI is not None: conn.close() LOGGER.info("Generating USER_SESSION_STRING") -app = Client(':memory:', api_id=int(TELEGRAM_API), api_hash=TELEGRAM_HASH, bot_token=BOT_TOKEN) +app = Client('Slam', api_id=int(TELEGRAM_API), api_hash=TELEGRAM_HASH, bot_token=BOT_TOKEN) # Generate Telegraph Token sname = ''.join(random.SystemRandom().choices(string.ascii_letters, k=8)) @@ -211,15 +211,6 @@ except KeyError: logging.warning('MEGA Credentials not provided!') MEGA_EMAIL_ID = None MEGA_PASSWORD = None -try: - HEROKU_API_KEY = getConfig('HEROKU_API_KEY') - HEROKU_APP_NAME = getConfig('HEROKU_APP_NAME') - if len(HEROKU_API_KEY) == 0 or len(HEROKU_APP_NAME) == 0: - HEROKU_API_KEY = None - HEROKU_APP_NAME = None -except KeyError: - HEROKU_API_KEY = None - HEROKU_APP_NAME = None try: UPTOBOX_TOKEN = getConfig('UPTOBOX_TOKEN') except KeyError: @@ -338,14 +329,6 @@ try: IS_VPS = IS_VPS.lower() == 'true' except KeyError: IS_VPS = False -try: - SERVER_PORT = getConfig('SERVER_PORT') - if len(SERVER_PORT) == 0: - SERVER_PORT = None -except KeyError: - if IS_VPS: - logging.warning('SERVER_PORT not provided!') - SERVER_PORT = None try: TOKEN_PICKLE_URL = getConfig('TOKEN_PICKLE_URL') if len(TOKEN_PICKLE_URL) == 0: diff --git a/bot/__main__.py b/bot/__main__.py index 026560b..462ebaf 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -4,20 +4,19 @@ import os import asyncio from pyrogram import idle -from bot import app, alive from sys import executable from telegram import ParseMode from telegram.ext import CommandHandler from wserver import start_server_async -from bot import bot, dispatcher, updater, botStartTime, IGNORE_PENDING_REQUESTS, IS_VPS, SERVER_PORT +from bot import bot, app, dispatcher, updater, botStartTime, IGNORE_PENDING_REQUESTS, IS_VPS, PORT, alive, web from bot.helper.ext_utils import fs_utils from bot.helper.telegram_helper.bot_commands import BotCommands 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 bot.helper.telegram_helper import button_build -from .modules import authorize, list, cancel_mirror, mirror_status, mirror, clone, watch, shell, eval, torrent_search, delete, speedtest, count, reboot +from .modules import authorize, list, cancel_mirror, mirror_status, mirror, clone, watch, shell, eval, torrent_search, delete, speedtest, count def stats(update, context): @@ -71,6 +70,7 @@ def restart(update, context): f.write(f"{restart_message.chat.id}\n{restart_message.message_id}\n") fs_utils.clean_all() alive.terminate() + web.terminate() os.execl(executable, executable, "-m", "bot") @@ -207,7 +207,7 @@ def main(): fs_utils.start_cleanup() if IS_VPS: - asyncio.get_event_loop().run_until_complete(start_server_async(SERVER_PORT)) + asyncio.get_event_loop().run_until_complete(start_server_async(PORT)) # Check if the bot is restarting if os.path.isfile(".restartmsg"): diff --git a/bot/helper/__init__.py b/bot/helper/__init__.py index 0491ee9..e69de29 100644 --- a/bot/helper/__init__.py +++ b/bot/helper/__init__.py @@ -1,25 +0,0 @@ -import heroku3 - -from functools import wraps -from bot import HEROKU_API_KEY, HEROKU_APP_NAME - - -heroku_client = heroku3.from_key(HEROKU_API_KEY) if HEROKU_API_KEY else None - -def check_heroku(func): - @wraps(func) - async def heroku_cli(client, message): - heroku_app = None - if not heroku_client: - await message.reply_text("`Please Add HEROKU_API_KEY Key For This To Function To Work!`", parse_mode="markdown") - elif not HEROKU_APP_NAME: - await message.reply_text("`Please Add HEROKU_APP_NAME For This To Function To Work!`", parse_mode="markdown") - if HEROKU_APP_NAME and heroku_client: - try: - heroku_app = heroku_client.app(HEROKU_APP_NAME) - except: - await message.reply_text(message, "`Heroku Api Key And App Name Doesn't Match!`", parse_mode="markdown") - if heroku_app: - await func(client, message, heroku_app) - - return heroku_cli diff --git a/bot/helper/mirror_utils/download_utils/aria2_download.py b/bot/helper/mirror_utils/download_utils/aria2_download.py index 27ea0ab..2392e6f 100644 --- a/bot/helper/mirror_utils/download_utils/aria2_download.py +++ b/bot/helper/mirror_utils/download_utils/aria2_download.py @@ -22,7 +22,9 @@ class AriaDownloadHelper: if STOP_DUPLICATE and dl is not None: LOGGER.info('Checking File/Folder if already in Drive...') sname = aria2.get_download(gid).name - if dl.getListener().isTar: + if dl.getListener().isTar and dl.getListener().isZip: + sname = sname + ".zip" + elif dl.getListener().isTar: sname = sname + ".tar" if dl.getListener().extract: smsg = None diff --git a/bot/helper/mirror_utils/download_utils/mega_downloader.py b/bot/helper/mirror_utils/download_utils/mega_downloader.py index f8e6e7b..503c2b8 100644 --- a/bot/helper/mirror_utils/download_utils/mega_downloader.py +++ b/bot/helper/mirror_utils/download_utils/mega_downloader.py @@ -170,7 +170,9 @@ class MegaDownloadHelper: if STOP_DUPLICATE: LOGGER.info('Checking File/Folder if already in Drive') mname = node.getName() - if listener.isTar: + if listener.isTar and listener.isZip: + mname = mname + ".zip" + elif listener.isTar: mname = mname + ".tar" if listener.extract: smsg = None diff --git a/bot/helper/mirror_utils/download_utils/telegram_downloader.py b/bot/helper/mirror_utils/download_utils/telegram_downloader.py index 80e4750..4d5584d 100644 --- a/bot/helper/mirror_utils/download_utils/telegram_downloader.py +++ b/bot/helper/mirror_utils/download_utils/telegram_downloader.py @@ -101,13 +101,8 @@ class TelegramDownloadHelper(DownloadHelper): if download: if STOP_DUPLICATE: LOGGER.info('Checking File/Folder if already in Drive...') - if self.__listener.isTar: - name = name + ".tar" - if self.__listener.extract: - smsg = None - else: - gd = GoogleDriveHelper() - smsg, button = gd.drive_list(name, True) + gd = GoogleDriveHelper() + smsg, button = gd.drive_list(name, True) if smsg: sendMarkup("File/Folder is already available in Drive.\nHere are the search results:", self.__listener.bot, self.__listener.update, button) return diff --git a/bot/modules/reboot.py b/bot/modules/reboot.py deleted file mode 100644 index 68017ac..0000000 --- a/bot/modules/reboot.py +++ /dev/null @@ -1,18 +0,0 @@ -# Implement By https://github.com/jusidama18 -# Based on this https://github.com/DevsExpo/FridayUserbot/blob/master/plugins/heroku_helpers.py - -from pyrogram import filters -from bot import app, OWNER_ID, bot -from bot.helper import check_heroku - -@app.on_message(filters.command(['reboot', f'reboot@{bot.username}']) & filters.user(OWNER_ID)) -@check_heroku -async def gib_restart(client, message, hap): - msg_ = await message.reply_text("**[HEROKU] - Restarting**") - hap.restart() - -@app.on_message(filters.command(['shutdown', f'shutdown@{bot.username}']) & filters.user(OWNER_ID)) -@check_heroku -async def shutdown(client, message, app_): - msg_ = await message.reply_text("**[HEROKU] - Shutdown**\n\n**NOTE: You need to turn on manual from Heroku to use this bot again.**") - app_.process_formation()["worker"].scale(0) diff --git a/config_sample.env b/config_sample.env index b89a9df..7bbdf25 100644 --- a/config_sample.env +++ b/config_sample.env @@ -30,7 +30,7 @@ SHORTENER = "" SHORTENER_API = "" # qBittorrent IS_VPS = "" # Don't set this to True even if VPS, unless facing error with web server -SERVER_PORT = "80" # Only if IS_VPS is True +SERVER_PORT = "80" # Only For VPS even if is_vps is false BASE_URL_OF_BOT = "" # Web Link, Required for Heroku to avoid sleep or use worker if you don't want to use web (selection) # If you want to use Credentials externally from Index Links, fill these vars with the direct links # These are optional, if you don't know, simply leave them, don't fill anything in them. @@ -42,9 +42,6 @@ TORRENT_DIRECT_LIMIT = "" TAR_UNZIP_LIMIT = "" CLONE_LIMIT = "" MEGA_LIMIT = "" -# Optional for Heroku features (dyno restart (/reboot) and update cmd soon) -HEROKU_API_KEY = "" -HEROKU_APP_NAME = "" # View Link button to open file Index Link in browser instead of direct download link # You can figure out if it's compatible with your Index code or not, open any video from you Index and check if the END of link from browser link bar is ?a=view, if yes make it "True" it will work (Compatible with Bhadoo Index Code) VIEW_LINK = "" diff --git a/start.sh b/start.sh index c7c1baa..62dc5c7 100755 --- a/start.sh +++ b/start.sh @@ -1 +1 @@ -gunicorn wserver:start_server --bind 0.0.0.0:$PORT --worker-class aiohttp.GunicornWebWorker & ./aria.sh; python3 -m bot +./aria.sh; python3 -m bot diff --git a/wserver.py b/wserver.py index 6998425..b07bfb9 100644 --- a/wserver.py +++ b/wserver.py @@ -10,6 +10,10 @@ import asyncio from aiohttp import web import nodes +logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + handlers=[logging.FileHandler('log.txt'), logging.StreamHandler()], + level=logging.INFO) + LOGGER = logging.getLogger(__name__) routes = web.RouteTableDef() @@ -626,7 +630,7 @@ async def re_verfiy(paused, resumed, client, torr): if verify: break - LOGGER.error("Reverification Failed, correcting stuff...") + LOGGER.info("Reverification Failed: correcting stuff...") client.auth_log_out() client = qba.Client(host="localhost", port="8090", username="admin", password="adminadmin") @@ -645,6 +649,7 @@ async def re_verfiy(paused, resumed, client, torr): k += 1 if k > 4: return False + LOGGER.info("Verified") return True @@ -691,7 +696,7 @@ async def set_priority(request): await asyncio.sleep(2) if not await re_verfiy(pause, resume, client, torr): - LOGGER.error("The Torrent choose errored reverification failed") + LOGGER.error("Verification Failed") client.auth_log_out() return await list_torrent_contents(request) @@ -725,7 +730,7 @@ async def start_server(): return app -async def start_server_async(port=8080): +async def start_server_async(port=80): app = web.Application(middlewares=[e404_middleware]) app.add_routes(routes)