Skip to content

Commit

Permalink
Use one session for all requests by qbittorrent-api
Browse files Browse the repository at this point in the history
Thanks to @rmartin16

Signed-off-by: anasty17 <[email protected]>
  • Loading branch information
anasty17 committed May 31, 2024
1 parent 0b417b1 commit 7e1becb
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 107 deletions.
25 changes: 13 additions & 12 deletions bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,21 +499,22 @@
if not ospath.exists("accounts"):
config_dict["USE_SERVICE_ACCOUNTS"] = False


def get_qb_client():
return qbClient(
host="localhost",
port=8090,
VERIFY_WEBUI_CERTIFICATE=False,
REQUESTS_ARGS={"timeout": (30, 60)},
)

qbittorrent_client = qbClient(
host="localhost",
port=8090,
VERIFY_WEBUI_CERTIFICATE=False,
REQUESTS_ARGS={"timeout": (30, 60)},
HTTPADAPTER_ARGS={
"pool_maxsize": 500,
"max_retries": 10,
"pool_block": True,
},
)

sabnzbd_client = sabnzbdClient(
host="http://localhost",
api_key="mltb",
port="8070",
HTTPX_REQUETS_ARGS={"timeout": 10},
)


Expand Down Expand Up @@ -551,7 +552,7 @@ def get_qb_client():
def get_qb_options():
global qbit_options
if not qbit_options:
qbit_options = dict(get_qb_client().app_preferences())
qbit_options = dict(qbittorrent_client.app_preferences())
del qbit_options["listen_port"]
for k in list(qbit_options.keys()):
if k.startswith("rss"):
Expand All @@ -561,7 +562,7 @@ def get_qb_options():
for k, v in list(qb_opt.items()):
if v in ["", "*"]:
del qb_opt[k]
get_qb_client().app_set_preferences(qb_opt)
qbittorrent_client.app_set_preferences(qb_opt)


get_qb_options()
Expand Down
4 changes: 2 additions & 2 deletions bot/helper/ext_utils/files_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from subprocess import run as srun
from sys import exit as sexit

from bot import aria2, LOGGER, DOWNLOAD_DIR, get_qb_client
from bot import aria2, LOGGER, DOWNLOAD_DIR, qbittorrent_client
from bot.helper.ext_utils.bot_utils import sync_to_async, cmd_exec
from .exceptions import NotSupportedExtractionArchive

Expand Down Expand Up @@ -91,7 +91,7 @@ async def clean_download(path):

def clean_all():
aria2.remove_all(True)
get_qb_client().torrents_delete(torrent_hashes="all")
qbittorrent_client.torrents_delete(torrent_hashes="all")
try:
LOGGER.info("Cleaning Download Directory")
rmtree(DOWNLOAD_DIR, ignore_errors=True)
Expand Down
2 changes: 0 additions & 2 deletions bot/helper/listeners/nzb_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ async def _stop_duplicate(nzo_id):
task = await getTaskByGid(nzo_id)
await task.update()
task.listener.name = task.name()
if not hasattr(task, "listener"):
return
msg, button = await stop_duplicate_check(task.listener)
if msg:
_onDownloadError(msg, nzo_id, button)
Expand Down
49 changes: 25 additions & 24 deletions bot/helper/listeners/qbit_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
task_dict,
task_dict_lock,
Intervals,
get_qb_client,
qbittorrent_client,
config_dict,
QbTorrents,
qb_listener_lock,
Expand All @@ -21,40 +21,39 @@
from bot.helper.telegram_helper.message_utils import update_status_message


async def _remove_torrent(client, hash_, tag):
await sync_to_async(client.torrents_delete, torrent_hashes=hash_, delete_files=True)
async def _remove_torrent(hash_, tag):
await sync_to_async(
qbittorrent_client.torrents_delete, torrent_hashes=hash_, delete_files=True
)
async with qb_listener_lock:
if tag in QbTorrents:
del QbTorrents[tag]
await sync_to_async(client.torrents_delete_tags, tags=tag)
await sync_to_async(client.auth_log_out)
await sync_to_async(qbittorrent_client.torrents_delete_tags, tags=tag)


@new_task
async def _onDownloadError(err, tor, button=None):
LOGGER.info(f"Cancelling Download: {tor.name}")
ext_hash = tor.hash
task = await getTaskByGid(ext_hash[:12])
if not hasattr(task, "client"):
return
await gather(
task.listener.onDownloadError(err, button),
sync_to_async(task.client.torrents_pause, torrent_hashes=ext_hash),
sync_to_async(qbittorrent_client.torrents_pause, torrent_hashes=ext_hash),
)
await sleep(0.3)
await _remove_torrent(task.client, ext_hash, tor.tags)
await _remove_torrent(ext_hash, tor.tags)


@new_task
async def _onSeedFinish(tor):
ext_hash = tor.hash
LOGGER.info(f"Cancelling Seed: {tor.name}")
task = await getTaskByGid(ext_hash[:12])
if not hasattr(task, "client"):
if not hasattr(task, "seeders_num"):
return
msg = f"Seeding stopped with Ratio: {round(tor.ratio, 3)} and Time: {get_readable_time(tor.seeding_time)}"
await task.listener.onUploadError(msg)
await _remove_torrent(task.client, ext_hash, tor.tags)
await _remove_torrent(ext_hash, tor.tags)


@new_task
Expand All @@ -75,11 +74,13 @@ async def _onDownloadComplete(tor):
tag = tor.tags
task = await getTaskByGid(ext_hash[:12])
if not task.listener.seed:
await sync_to_async(task.client.torrents_pause, torrent_hashes=ext_hash)
await sync_to_async(qbittorrent_client.torrents_pause, torrent_hashes=ext_hash)
if task.listener.select:
await clean_unwanted(task.listener.dir)
path = tor.content_path.rsplit("/", 1)[0]
res = await sync_to_async(task.client.torrents_files, torrent_hash=ext_hash)
res = await sync_to_async(
qbittorrent_client.torrents_files, torrent_hash=ext_hash
)
for f in res:
if f.priority == 0 and await aiopath.exists(f"{path}/{f.name}"):
try:
Expand All @@ -97,10 +98,8 @@ async def _onDownloadComplete(tor):
else:
removed = True
if removed:
await _remove_torrent(task.client, ext_hash, tag)
await _remove_torrent(ext_hash, tag)
return
else:
await sync_to_async(task.client.auth_log_out)
async with qb_listener_lock:
if tag in QbTorrents:
QbTorrents[tag]["seeding"] = True
Expand All @@ -109,18 +108,16 @@ async def _onDownloadComplete(tor):
await update_status_message(task.listener.message.chat.id)
LOGGER.info(f"Seeding started: {tor.name} - Hash: {ext_hash}")
else:
await _remove_torrent(task.client, ext_hash, tag)
await _remove_torrent(ext_hash, tag)


async def _qb_listener():
client = get_qb_client()
while True:
async with qb_listener_lock:
try:
torrents = await sync_to_async(client.torrents_info)
torrents = await sync_to_async(qbittorrent_client.torrents_info)
if len(torrents) == 0:
Intervals["qb"] = ""
await sync_to_async(client.auth_log_out)
break
for tor_info in torrents:
tag = tor_info.tags
Expand All @@ -137,7 +134,8 @@ async def _qb_listener():
_onDownloadError("Dead Torrent!", tor_info)
else:
await sync_to_async(
client.torrents_reannounce, torrent_hashes=tor_info.hash
qbittorrent_client.torrents_reannounce,
torrent_hashes=tor_info.hash,
)
elif state == "downloading":
QbTorrents[tag]["stalled_time"] = time()
Expand All @@ -155,7 +153,8 @@ async def _qb_listener():
msg += f"Size: {tor_info.size} Total Size: {tor_info.total_size}"
LOGGER.warning(msg)
await sync_to_async(
client.torrents_recheck, torrent_hashes=tor_info.hash
qbittorrent_client.torrents_recheck,
torrent_hashes=tor_info.hash,
)
QbTorrents[tag]["rechecked"] = True
elif (
Expand All @@ -166,11 +165,13 @@ async def _qb_listener():
_onDownloadError("Dead Torrent!", tor_info)
else:
await sync_to_async(
client.torrents_reannounce, torrent_hashes=tor_info.hash
qbittorrent_client.torrents_reannounce,
torrent_hashes=tor_info.hash,
)
elif state == "missingFiles":
await sync_to_async(
client.torrents_recheck, torrent_hashes=tor_info.hash
qbittorrent_client.torrents_recheck,
torrent_hashes=tor_info.hash,
)
elif state == "error":
_onDownloadError(
Expand Down
22 changes: 13 additions & 9 deletions bot/helper/mirror_leech_utils/download_utils/qbit_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from bot import (
task_dict,
task_dict_lock,
get_qb_client,
qbittorrent_client,
LOGGER,
config_dict,
non_queued_dl,
Expand Down Expand Up @@ -40,7 +40,6 @@ def _get_hash_file(fpath):


async def add_qb_torrent(listener, path, ratio, seed_time):
client = get_qb_client()
try:
url = listener.link
tpath = None
Expand All @@ -49,7 +48,7 @@ async def add_qb_torrent(listener, path, ratio, seed_time):
tpath = listener.link
add_to_queue, event = await check_running_tasks(listener)
op = await sync_to_async(
client.torrents_add,
qbittorrent_client.torrents_add,
url,
tpath,
path,
Expand All @@ -60,11 +59,13 @@ async def add_qb_torrent(listener, path, ratio, seed_time):
headers={"user-agent": "Wget/1.12"},
)
if op.lower() == "ok.":
tor_info = await sync_to_async(client.torrents_info, tag=f"{listener.mid}")
tor_info = await sync_to_async(
qbittorrent_client.torrents_info, tag=f"{listener.mid}"
)
if len(tor_info) == 0:
while True:
tor_info = await sync_to_async(
client.torrents_info, tag=f"{listener.mid}"
qbittorrent_client.torrents_info, tag=f"{listener.mid}"
)
if len(tor_info) > 0:
break
Expand Down Expand Up @@ -95,7 +96,7 @@ async def add_qb_torrent(listener, path, ratio, seed_time):
meta = await sendMessage(listener.message, metamsg)
while True:
tor_info = await sync_to_async(
client.torrents_info, tag=f"{listener.mid}"
qbittorrent_client.torrents_info, tag=f"{listener.mid}"
)
if len(tor_info) == 0:
await deleteMessage(meta)
Expand All @@ -115,7 +116,9 @@ async def add_qb_torrent(listener, path, ratio, seed_time):

ext_hash = tor_info.hash
if not add_to_queue:
await sync_to_async(client.torrents_pause, torrent_hashes=ext_hash)
await sync_to_async(
qbittorrent_client.torrents_pause, torrent_hashes=ext_hash
)
SBUTTONS = bt_selection_buttons(ext_hash)
msg = "Your download paused. Choose files then press Done Selecting button to start downloading."
await sendMessage(listener.message, msg, SBUTTONS)
Expand All @@ -131,7 +134,9 @@ async def add_qb_torrent(listener, path, ratio, seed_time):
async with task_dict_lock:
task_dict[listener.mid].queued = False

await sync_to_async(client.torrents_resume, torrent_hashes=ext_hash)
await sync_to_async(
qbittorrent_client.torrents_resume, torrent_hashes=ext_hash
)
LOGGER.info(
f"Start Queued Download from Qbittorrent: {tor_info.name} - Hash: {ext_hash}"
)
Expand All @@ -140,4 +145,3 @@ async def add_qb_torrent(listener, path, ratio, seed_time):
finally:
if tpath and await aiopath.exists(listener.link):
await remove(listener.link)
await sync_to_async(client.auth_log_out)
32 changes: 18 additions & 14 deletions bot/helper/mirror_leech_utils/status_utils/qbit_status.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from asyncio import sleep
from asyncio import sleep, gather

from bot import LOGGER, get_qb_client, QbTorrents, qb_listener_lock
from bot import LOGGER, qbittorrent_client, QbTorrents, qb_listener_lock
from bot.helper.ext_utils.bot_utils import sync_to_async
from bot.helper.ext_utils.status_utils import (
MirrorStatus,
Expand All @@ -9,9 +9,9 @@
)


def get_download(client, tag, old_info=None):
def get_download(tag, old_info=None):
try:
res = client.torrents_info(tag=tag)[0]
res = qbittorrent_client.torrents_info(tag=tag)[0]
return res or old_info
except Exception as e:
LOGGER.error(f"{e}: Qbittorrent, while getting torrent info. Tag: {tag}")
Expand All @@ -20,14 +20,13 @@ def get_download(client, tag, old_info=None):

class QbittorrentStatus:
def __init__(self, listener, seeding=False, queued=False):
self.client = get_qb_client()
self.queued = queued
self.seeding = seeding
self.listener = listener
self._info = None

def update(self):
self._info = get_download(self.client, f"{self.listener.mid}", self._info)
self._info = get_download(f"{self.listener.mid}", self._info)

def progress(self):
return f"{round(self._info.progress * 100, 2)}%"
Expand Down Expand Up @@ -96,7 +95,9 @@ def hash(self):
async def cancel_task(self):
self.listener.isCancelled = True
await sync_to_async(self.update)
await sync_to_async(self.client.torrents_pause, torrent_hashes=self._info.hash)
await sync_to_async(
qbittorrent_client.torrents_pause, torrent_hashes=self._info.hash
)
if not self.seeding:
if self.queued:
LOGGER.info(f"Cancelling QueueDL: {self.name()}")
Expand All @@ -105,14 +106,17 @@ async def cancel_task(self):
LOGGER.info(f"Cancelling Download: {self._info.name}")
msg = "Download stopped by user!"
await sleep(0.3)
await self.listener.onDownloadError(msg)
await sync_to_async(
self.client.torrents_delete,
torrent_hashes=self._info.hash,
delete_files=True,
await gather(
self.listener.onDownloadError(msg),
sync_to_async(
qbittorrent_client.torrents_delete,
torrent_hashes=self._info.hash,
delete_files=True,
),
sync_to_async(
qbittorrent_client.torrents_delete_tags, tags=self._info.tags
),
)
await sync_to_async(self.client.torrents_delete_tags, tags=self._info.tags)
await sync_to_async(self.client.auth_log_out)
async with qb_listener_lock:
if self._info.tags in QbTorrents:
del QbTorrents[self._info.tags]
6 changes: 3 additions & 3 deletions bot/modules/bot_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
IS_PREMIUM_USER,
task_dict,
qbit_options,
get_qb_client,
qbittorrent_client,
sabnzbd_client,
LOGGER,
bot,
Expand Down Expand Up @@ -382,7 +382,7 @@ async def edit_qbit(_, message, pre_message, key):
value = float(value)
elif value.isdigit():
value = int(value)
await sync_to_async(get_qb_client().app_set_preferences, {key: value})
await sync_to_async(().app_set_preferences, {key: value})
qbit_options[key] = value
await update_buttons(pre_message, "qbit")
await deleteMessage(message)
Expand Down Expand Up @@ -746,7 +746,7 @@ async def edit_bot_settings(client, query):
await DbManager().update_aria2(data[2], "")
elif data[1] == "emptyqbit":
await query.answer()
await sync_to_async(get_qb_client().app_set_preferences, {data[2]: value})
await sync_to_async(qbittorrent_client.app_set_preferences, {data[2]: value})
qbit_options[data[2]] = ""
await update_buttons(message, "qbit")
if DATABASE_URL:
Expand Down
Loading

0 comments on commit 7e1becb

Please sign in to comment.