Skip to content

Commit

Permalink
Switch to pyrofork
Browse files Browse the repository at this point in the history
- Fix qbittorrent webui password
- Fix wrong message when upload path not added
- Other minor fix

Signed-off-by: anasty17 <[email protected]>
  • Loading branch information
anasty17 committed Aug 12, 2024
1 parent 66a57bc commit c9c75a6
Show file tree
Hide file tree
Showing 34 changed files with 407 additions and 309 deletions.
8 changes: 7 additions & 1 deletion bot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from sys import exit
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from aria2p import API as ariaAPI, Client as ariaClient
from asyncio import Lock, get_event_loop
Expand All @@ -24,6 +25,7 @@
from time import time
from tzlocal import get_localzone
from uvloop import install
from concurrent.futures import ThreadPoolExecutor

# from faulthandler import enable as faulthandler_enable
# faulthandler_enable()
Expand All @@ -40,6 +42,8 @@

botStartTime = time()
bot_loop = get_event_loop()
THREADPOOL = ThreadPoolExecutor(max_workers=99999)
bot_loop.set_default_executor(THREADPOOL)

basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
Expand Down Expand Up @@ -539,7 +543,7 @@
TELEGRAM_API,
TELEGRAM_HASH,
bot_token=BOT_TOKEN,
workers=1000,
workers=99999,
parse_mode=enums.ParseMode.HTML,
max_concurrent_transmissions=10,
).start()
Expand All @@ -556,7 +560,9 @@ def get_qb_options():
for k in list(qbit_options.keys()):
if k.startswith("rss"):
del qbit_options[k]
qbittorrent_client.app_set_preferences({"web_ui_password": "mltbmltb"})
else:
qbit_options["web_ui_password"] = "mltbmltb"
qb_opt = {**qbit_options}
qbittorrent_client.app_set_preferences(qb_opt)

Expand Down
25 changes: 19 additions & 6 deletions bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,31 +264,44 @@ async def main():
)
create_help_buttons()

bot.add_handler(MessageHandler(start, filters=command(BotCommands.StartCommand)))
bot.add_handler(
MessageHandler(
log, filters=command(BotCommands.LogCommand) & CustomFilters.sudo
start, filters=command(BotCommands.StartCommand, case_sensitive=True)
)
)
bot.add_handler(
MessageHandler(
restart, filters=command(BotCommands.RestartCommand) & CustomFilters.sudo
log,
filters=command(BotCommands.LogCommand, case_sensitive=True)
& CustomFilters.sudo,
)
)
bot.add_handler(
MessageHandler(
ping, filters=command(BotCommands.PingCommand) & CustomFilters.authorized
restart,
filters=command(BotCommands.RestartCommand, case_sensitive=True)
& CustomFilters.sudo,
)
)
bot.add_handler(
MessageHandler(
ping,
filters=command(BotCommands.PingCommand, case_sensitive=True)
& CustomFilters.authorized,
)
)
bot.add_handler(
MessageHandler(
bot_help,
filters=command(BotCommands.HelpCommand) & CustomFilters.authorized,
filters=command(BotCommands.HelpCommand, case_sensitive=True)
& CustomFilters.authorized,
)
)
bot.add_handler(
MessageHandler(
stats, filters=command(BotCommands.StatsCommand) & CustomFilters.authorized
stats,
filters=command(BotCommands.StatsCommand, case_sensitive=True)
& CustomFilters.authorized,
)
)
LOGGER.info("Bot Started!")
Expand Down
18 changes: 13 additions & 5 deletions bot/helper/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ def getConfigPath(self, dest):
)

async def isTokenExists(self, path, status):
if not self.upDest:
raise ValueError("No Upload Destination!")
if not is_gdrive_id(self.upDest) and not is_rclone_path(self.upDest):
raise ValueError("Wrong Upload Destination!")
if is_rclone_path(path):
config_path = self.getConfigPath(path)
if config_path != "rclone.conf" and status == "up":
Expand Down Expand Up @@ -176,7 +180,15 @@ async def beforeStart(self):
else ["aria2", "!qB"]
)
if self.link not in ["rcl", "gdl"]:
if not self.isYtDlp and not self.isJd:
if (
not self.isYtDlp
and not self.isJd
and (
is_gdrive_id(self.link)
or is_rclone_path(self.link)
or is_gdrive_link(self.link)
)
):
await self.isTokenExists(self.link, "dl")
elif self.link == "rcl":
if not self.isYtDlp and not self.isJd:
Expand Down Expand Up @@ -217,10 +229,6 @@ async def beforeStart(self):
)
elif (not self.upDest and default_upload == "gd") or self.upDest == "gd":
self.upDest = self.userDict.get("gdrive_id") or config_dict["GDRIVE_ID"]
if not self.upDest:
raise ValueError("No Upload Destination!")
if not is_gdrive_id(self.upDest) and not is_rclone_path(self.upDest):
raise ValueError("Wrong Upload Destination!")
if self.upDest not in ["rcl", "gdl"]:
await self.isTokenExists(self.upDest, "up")

Expand Down
5 changes: 1 addition & 4 deletions bot/helper/ext_utils/bot_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
sleep,
)
from asyncio.subprocess import PIPE
from concurrent.futures import ThreadPoolExecutor
from functools import partial, wraps

from bot import user_data, config_dict, bot_loop
Expand All @@ -18,8 +17,6 @@
from bot.helper.ext_utils.telegraph_helper import telegraph
from bot.helper.telegram_helper.button_build import ButtonMaker

THREADPOOL = ThreadPoolExecutor(max_workers=1000)

COMMAND_USAGE = {}


Expand Down Expand Up @@ -206,7 +203,7 @@ def wrapper(*args, **kwargs):

async def sync_to_async(func, *args, wait=True, **kwargs):
pfunc = partial(func, *args, **kwargs)
future = bot_loop.run_in_executor(THREADPOOL, pfunc)
future = bot_loop.run_in_executor(None, pfunc)
return await future if wait else future


Expand Down
18 changes: 0 additions & 18 deletions bot/helper/ext_utils/db_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ async def db_load(self):
)
except Exception as e:
LOGGER.error(f"DataBase Collection Error: {e}")
self._conn.close
return
# Save Aria2c options
if await self._db.settings.aria2c.find_one({"_id": bot_id}) is None:
Expand Down Expand Up @@ -98,7 +97,6 @@ async def db_load(self):
del row["_id"]
rss_dict[user_id] = row
LOGGER.info("Rss data has been imported from Database.")
self._conn.close

async def update_deploy_config(self):
if self._err:
Expand All @@ -107,39 +105,34 @@ async def update_deploy_config(self):
await self._db.settings.deployConfig.replace_one(
{"_id": bot_id}, current_config, upsert=True
)
self._conn.close

async def update_config(self, dict_):
if self._err:
return
await self._db.settings.config.update_one(
{"_id": bot_id}, {"$set": dict_}, upsert=True
)
self._conn.close

async def update_aria2(self, key, value):
if self._err:
return
await self._db.settings.aria2c.update_one(
{"_id": bot_id}, {"$set": {key: value}}, upsert=True
)
self._conn.close

async def update_qbittorrent(self, key, value):
if self._err:
return
await self._db.settings.qbittorrent.update_one(
{"_id": bot_id}, {"$set": {key: value}}, upsert=True
)
self._conn.close

async def save_qbit_settings(self):
if self._err:
return
await self._db.settings.qbittorrent.replace_one(
{"_id": bot_id}, qbit_options, upsert=True
)
self._conn.close

async def update_private_file(self, path):
if self._err:
Expand All @@ -155,8 +148,6 @@ async def update_private_file(self, path):
)
if path == "config.env":
await self.update_deploy_config()
else:
self._conn.close

async def update_nzb_config(self):
async with aiopen("sabnzbd/SABnzbd.ini", "rb+") as pf:
Expand All @@ -176,7 +167,6 @@ async def update_user_data(self, user_id):
if data.get("token_pickle"):
del data["token_pickle"]
await self._db.users.replace_one({"_id": user_id}, data, upsert=True)
self._conn.close

async def update_user_doc(self, user_id, key, path=""):
if self._err:
Expand All @@ -189,7 +179,6 @@ async def update_user_doc(self, user_id, key, path=""):
await self._db.users.update_one(
{"_id": user_id}, {"$set": {key: doc_bin}}, upsert=True
)
self._conn.close

async def rss_update_all(self):
if self._err:
Expand All @@ -198,33 +187,28 @@ async def rss_update_all(self):
await self._db.rss[bot_id].replace_one(
{"_id": user_id}, rss_dict[user_id], upsert=True
)
self._conn.close

async def rss_update(self, user_id):
if self._err:
return
await self._db.rss[bot_id].replace_one(
{"_id": user_id}, rss_dict[user_id], upsert=True
)
self._conn.close

async def rss_delete(self, user_id):
if self._err:
return
await self._db.rss[bot_id].delete_one({"_id": user_id})
self._conn.close

async def add_incomplete_task(self, cid, link, tag):
if self._err:
return
await self._db.tasks[bot_id].insert_one({"_id": link, "cid": cid, "tag": tag})
self._conn.close

async def rm_complete_task(self, link):
if self._err:
return
await self._db.tasks[bot_id].delete_one({"_id": link})
self._conn.close

async def get_incomplete_tasks(self):
notifier_dict = {}
Expand All @@ -242,11 +226,9 @@ async def get_incomplete_tasks(self):
else:
notifier_dict[row["cid"]] = {row["tag"]: [row["_id"]]}
await self._db.tasks[bot_id].drop()
self._conn.close
return notifier_dict # return a dict ==> {cid: {tag: [_id, _id, ...]}}

async def trunc_table(self, name):
if self._err:
return
await self._db[name][bot_id].drop()
self._conn.close
6 changes: 3 additions & 3 deletions bot/helper/ext_utils/files_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from re import split as re_split, I, search as re_search, escape
from shutil import rmtree
from subprocess import run as srun
from sys import exit as sexit
from sys import exit

from bot import aria2, LOGGER, DOWNLOAD_DIR, qbittorrent_client
from bot.helper.ext_utils.bot_utils import sync_to_async, cmd_exec
Expand Down Expand Up @@ -105,10 +105,10 @@ def exit_clean_up(signal, frame):
LOGGER.info("Please wait, while we clean up and stop the running downloads")
clean_all()
srun(["pkill", "-9", "-f", "gunicorn|aria2c|qbittorrent-nox|ffmpeg|java"])
sexit(0)
exit(0)
except KeyboardInterrupt:
LOGGER.warning("Force Exiting before the cleanup finishes!")
sexit(1)
exit(1)


async def clean_unwanted(path, custom_list=None):
Expand Down
6 changes: 6 additions & 0 deletions bot/helper/ext_utils/media_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ async def take_ss(video_file, ss_nb) -> bool:
"1",
"-frames:v",
"1",
"-threads",
f"{cpu_count() // 2}",
output,
]
cap_time += interval
Expand Down Expand Up @@ -288,6 +290,8 @@ async def get_audio_thumb(audio_file):
"-an",
"-vcodec",
"copy",
"-threads",
f"{cpu_count() // 2}",
des_dir,
]
_, err, code = await cmd_exec(cmd)
Expand Down Expand Up @@ -321,6 +325,8 @@ async def create_thumbnail(video_file, duration):
"thumbnail",
"-frames:v",
"1",
"-threads",
f"{cpu_count() // 2}",
des_dir,
]
try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from bot.helper.ext_utils.links_utils import is_share_link
from bot.helper.ext_utils.status_utils import speed_string_to_bytes

_caches = {}
user_agent = (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0"
)
Expand Down
10 changes: 3 additions & 7 deletions bot/helper/mirror_leech_utils/download_utils/jd_download.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from asyncio import wait_for, Event, wrap_future, sleep
from asyncio import wait_for, Event, sleep
from functools import partial
from pyrogram.filters import regex, user
from pyrogram.handlers import CallbackQueryHandler
Expand All @@ -16,7 +16,7 @@
jd_lock,
jd_downloads,
)
from bot.helper.ext_utils.bot_utils import new_thread, retry_function, new_task
from bot.helper.ext_utils.bot_utils import retry_function
from bot.helper.ext_utils.jdownloader_booter import jdownloader
from bot.helper.ext_utils.task_manager import (
check_running_tasks,
Expand All @@ -36,7 +36,6 @@
)


@new_task
async def configureDownload(_, query, obj):
data = query.data.split()
message = query.message
Expand All @@ -56,7 +55,6 @@ def __init__(self, listener):
self.listener = listener
self.event = Event()

@new_thread
async def _event_handler(self):
pfunc = partial(configureDownload, obj=self)
handler = self.listener.client.add_handler(
Expand All @@ -75,15 +73,14 @@ async def _event_handler(self):
self.listener.client.remove_handler(*handler)

async def waitForConfigurations(self):
future = self._event_handler()
buttons = ButtonMaker()
buttons.ubutton("Select", "https://my.jdownloader.org")
buttons.ibutton("Done Selecting", "jdq sdone")
buttons.ibutton("Cancel", "jdq cancel")
button = buttons.build_menu(2)
msg = f"Disable/Remove the unwanted files or change variants or edit files names from myJdownloader site for <b>{self.listener.name}</b> but don't start it manually!\n\nAfter finish press Done Selecting!\nTimeout: 300s"
self._reply_to = await sendMessage(self.listener.message, msg, button)
await wrap_future(future)
await self._event_handler()
if not self.listener.isCancelled:
await deleteMessage(self._reply_to)
return self.listener.isCancelled
Expand All @@ -94,7 +91,6 @@ async def add_jd_download(listener, path):
if jdownloader.device is None:
await listener.onDownloadError(jdownloader.error)
return

try:
await wait_for(retry_function(jdownloader.device.jd.version), timeout=10)
except:
Expand Down
Loading

0 comments on commit c9c75a6

Please sign in to comment.