Path: blob/master/bot/core/startup.py
1629 views
from aiofiles.os import path as aiopath, remove, makedirs1from aiofiles import open as aiopen2from aioshutil import rmtree3from asyncio import create_subprocess_exec, create_subprocess_shell, sleep4from importlib import import_module56from .. import (7aria2_options,8qbit_options,9nzb_options,10drives_ids,11drives_names,12index_urls,13user_data,14excluded_extensions,15included_extensions,16LOGGER,17rss_dict,18sabnzbd_client,19auth_chats,20sudo_users,21)22from ..helper.ext_utils.db_handler import database23from .config_manager import Config24from .telegram_manager import TgClient25from .torrent_manager import TorrentManager262728async def update_qb_options():29LOGGER.info("Get qBittorrent options from server")30if not qbit_options:31opt = await TorrentManager.qbittorrent.app.preferences()32qbit_options.update(opt)33del qbit_options["listen_port"]34for k in list(qbit_options.keys()):35if k.startswith("rss"):36del qbit_options[k]37qbit_options["web_ui_password"] = "mltbmltb"38await TorrentManager.qbittorrent.app.set_preferences(39{"web_ui_password": "mltbmltb"}40)41else:42await TorrentManager.qbittorrent.app.set_preferences(qbit_options)434445async def update_aria2_options():46LOGGER.info("Get aria2 options from server")47if not aria2_options:48op = await TorrentManager.aria2.getGlobalOption()49aria2_options.update(op)50else:51await TorrentManager.aria2.changeGlobalOption(aria2_options)525354async def update_nzb_options():55LOGGER.info("Get SABnzbd options from server")56while True:57try:58no = (await sabnzbd_client.get_config())["config"]["misc"]59nzb_options.update(no)60except:61await sleep(0.5)62continue63break646566async def load_settings():67if not Config.DATABASE_URL:68return69for p in ["thumbnails", "tokens", "rclone"]:70if await aiopath.exists(p):71await rmtree(p, ignore_errors=True)72await database.connect()73if database.db is not None:74BOT_ID = Config.BOT_TOKEN.split(":", 1)[0]75settings = import_module("config")76config_file = {77key: value.strip() if isinstance(value, str) else value78for key, value in vars(settings).items()79if not key.startswith("__")80}81old_config = await database.db.settings.deployConfig.find_one(82{"_id": BOT_ID}, {"_id": 0}83)84if old_config is None:85await database.db.settings.deployConfig.replace_one(86{"_id": BOT_ID}, config_file, upsert=True87)88if old_config and old_config != config_file:89LOGGER.info("Replacing existing deploy config in Database")90await database.db.settings.deployConfig.replace_one(91{"_id": BOT_ID}, config_file, upsert=True92)93else:94config_dict = await database.db.settings.config.find_one(95{"_id": BOT_ID}, {"_id": 0}96)97if config_dict:98Config.load_dict(config_dict)99100if pf_dict := await database.db.settings.files.find_one(101{"_id": BOT_ID}, {"_id": 0}102):103for key, value in pf_dict.items():104if value:105file_ = key.replace("__", ".")106async with aiopen(file_, "wb+") as f:107await f.write(value)108109if a2c_options := await database.db.settings.aria2c.find_one(110{"_id": BOT_ID}, {"_id": 0}111):112aria2_options.update(a2c_options)113114if qbit_opt := await database.db.settings.qbittorrent.find_one(115{"_id": BOT_ID}, {"_id": 0}116):117qbit_options.update(qbit_opt)118119if nzb_opt := await database.db.settings.nzb.find_one(120{"_id": BOT_ID}, {"_id": 0}121):122if await aiopath.exists("sabnzbd/SABnzbd.ini.bak"):123await remove("sabnzbd/SABnzbd.ini.bak")124((key, value),) = nzb_opt.items()125file_ = key.replace("__", ".")126async with aiopen(f"sabnzbd/{file_}", "wb+") as f:127await f.write(value)128129if await database.db.users.find_one():130for p in ["thumbnails", "tokens", "rclone"]:131if not await aiopath.exists(p):132await makedirs(p)133rows = database.db.users.find({})134async for row in rows:135uid = row["_id"]136del row["_id"]137thumb_path = f"thumbnails/{uid}.jpg"138rclone_config_path = f"rclone/{uid}.conf"139token_path = f"tokens/{uid}.pickle"140if row.get("THUMBNAIL"):141async with aiopen(thumb_path, "wb+") as f:142await f.write(row["THUMBNAIL"])143row["THUMBNAIL"] = thumb_path144if row.get("RCLONE_CONFIG"):145async with aiopen(rclone_config_path, "wb+") as f:146await f.write(row["RCLONE_CONFIG"])147row["RCLONE_CONFIG"] = rclone_config_path148if row.get("TOKEN_PICKLE"):149async with aiopen(token_path, "wb+") as f:150await f.write(row["TOKEN_PICKLE"])151row["TOKEN_PICKLE"] = token_path152user_data[uid] = row153LOGGER.info("Users data has been imported from Database")154155if await database.db.rss[BOT_ID].find_one():156rows = database.db.rss[BOT_ID].find({})157async for row in rows:158user_id = row["_id"]159del row["_id"]160rss_dict[user_id] = row161LOGGER.info("Rss data has been imported from Database.")162163164async def save_settings():165if database.db is None:166return167config_dict = Config.get_all()168await database.db.settings.config.replace_one(169{"_id": TgClient.ID}, config_dict, upsert=True170)171if await database.db.settings.aria2c.find_one({"_id": TgClient.ID}) is None:172await database.db.settings.aria2c.update_one(173{"_id": TgClient.ID}, {"$set": aria2_options}, upsert=True174)175if await database.db.settings.qbittorrent.find_one({"_id": TgClient.ID}) is None:176await database.save_qbit_settings()177if await database.db.settings.nzb.find_one({"_id": TgClient.ID}) is None:178async with aiopen("sabnzbd/SABnzbd.ini", "rb+") as pf:179nzb_conf = await pf.read()180await database.db.settings.nzb.update_one(181{"_id": TgClient.ID}, {"$set": {"SABnzbd__ini": nzb_conf}}, upsert=True182)183184185async def update_variables():186if (187Config.LEECH_SPLIT_SIZE > TgClient.MAX_SPLIT_SIZE188or Config.LEECH_SPLIT_SIZE == 2097152000189or not Config.LEECH_SPLIT_SIZE190):191Config.LEECH_SPLIT_SIZE = TgClient.MAX_SPLIT_SIZE192193Config.HYBRID_LEECH = bool(Config.HYBRID_LEECH and TgClient.IS_PREMIUM_USER)194Config.USER_TRANSMISSION = bool(195Config.USER_TRANSMISSION and TgClient.IS_PREMIUM_USER196)197198if Config.AUTHORIZED_CHATS:199aid = Config.AUTHORIZED_CHATS.split()200for id_ in aid:201chat_id, *thread_ids = id_.split("|")202chat_id = int(chat_id.strip())203if thread_ids:204thread_ids = list(map(lambda x: int(x.strip()), thread_ids))205auth_chats[chat_id] = thread_ids206else:207auth_chats[chat_id] = []208209if Config.SUDO_USERS:210aid = Config.SUDO_USERS.split()211for id_ in aid:212sudo_users.append(int(id_.strip()))213214if Config.EXCLUDED_EXTENSIONS:215fx = Config.EXCLUDED_EXTENSIONS.split()216for x in fx:217x = x.lstrip(".")218excluded_extensions.append(x.strip().lower())219220if Config.INCLUDED_EXTENSIONS:221fx = Config.INCLUDED_EXTENSIONS.split()222for x in fx:223x = x.lstrip(".")224included_extensions.append(x.strip().lower())225226if Config.GDRIVE_ID:227drives_names.append("Main")228drives_ids.append(Config.GDRIVE_ID)229index_urls.append(Config.INDEX_URL)230231if await aiopath.exists("list_drives.txt"):232async with aiopen("list_drives.txt", "r+") as f:233lines = await f.readlines()234for line in lines:235temp = line.split()236drives_ids.append(temp[1])237drives_names.append(temp[0].replace("_", " "))238if len(temp) > 2:239index_urls.append(temp[2])240else:241index_urls.append("")242243244async def load_configurations():245246if not await aiopath.exists(".netrc"):247async with aiopen(".netrc", "w"):248pass249250await (251await create_subprocess_shell(252"chmod 600 .netrc && cp .netrc /root/.netrc && chmod +x aria-nox-nzb.sh && ./aria-nox-nzb.sh"253)254).wait()255256if Config.BASE_URL:257await create_subprocess_shell(258f"gunicorn -k uvicorn.workers.UvicornWorker -w 1 web.wserver:app --bind 0.0.0.0:{Config.BASE_URL_PORT}"259)260261if await aiopath.exists("cfg.zip"):262if await aiopath.exists("/JDownloader/cfg"):263await rmtree("/JDownloader/cfg", ignore_errors=True)264await (265await create_subprocess_exec("7z", "x", "cfg.zip", "-o/JDownloader")266).wait()267268if await aiopath.exists("accounts.zip"):269if await aiopath.exists("accounts"):270await rmtree("accounts")271await (272await create_subprocess_exec(273"7z", "x", "-o.", "-aoa", "accounts.zip", "accounts/*.json"274)275).wait()276await (await create_subprocess_exec("chmod", "-R", "777", "accounts")).wait()277await remove("accounts.zip")278279if not await aiopath.exists("accounts"):280Config.USE_SERVICE_ACCOUNTS = False281282283