Skip to content

Commit

Permalink
check torrent add status
Browse files Browse the repository at this point in the history
  • Loading branch information
ch3p4ll3 committed Dec 20, 2023
1 parent 40d24b5 commit 912f1ba
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/bot/plugins/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import psutil

from .. import custom_filters
from ...db_management import write_support
from ...utils import convert_size
from ...configs import Configs
from .common import send_menu
Expand All @@ -18,6 +19,7 @@ async def access_denied_message(client: Client, message: Message) -> None:
@Client.on_message(filters.command("start") & custom_filters.check_user_filter)
async def start_command(client: Client, message: Message) -> None:
"""Start the bot."""
write_support("None", message.chat.id)
await send_menu(client, message.id, message.chat.id)


Expand Down
12 changes: 10 additions & 2 deletions src/bot/plugins/on_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ async def on_text(client: Client, message: Message) -> None:
category = db_management.read_support(message.from_user.id).split("#")[1]

repository = ClientRepo.get_client_manager(Configs.config.client.type)
repository.add_magnet(
response = repository.add_magnet(
magnet_link=magnet_link,
category=category
)

if not response:
await message.reply_text("Unable to add magnet link")
return

await send_menu(client, message.id, message.from_user.id)
db_management.write_support("None", message.from_user.id)

Expand All @@ -43,7 +47,11 @@ async def on_text(client: Client, message: Message) -> None:
await message.download(name)

repository = ClientRepo.get_client_manager(Configs.config.client.type)
repository.add_torrent(file_name=name, category=category)
response = repository.add_torrent(file_name=name, category=category)

if not response:
await message.reply_text("Unable to add magnet link")
return

await send_menu(client, message.id, message.from_user.id)
db_management.write_support("None", message.from_user.id)
Expand Down
8 changes: 4 additions & 4 deletions src/client_manager/client_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

class ClientManager(ABC):
@classmethod
def add_magnet(cls, magnet_link: Union[str, List[str]], category: str = None) -> None:
"""Add one or multiple magnet links with or without a category"""
def add_magnet(cls, magnet_link: Union[str, List[str]], category: str = None) -> bool:
"""Add one or multiple magnet links with or without a category, return true if successful"""
raise NotImplementedError

@classmethod
def add_torrent(cls, file_name: str, category: str = None) -> None:
"""Add one torrent file with or without a category"""
def add_torrent(cls, file_name: str, category: str = None) -> bool:
"""Add one torrent file with or without a category, return true if successful"""
raise NotImplementedError

@classmethod
Expand Down
11 changes: 7 additions & 4 deletions src/client_manager/qbittorrent_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,26 @@

class QbittorrentManager(ClientManager):
@classmethod
def add_magnet(cls, magnet_link: Union[str, List[str]], category: str = None) -> None:
def add_magnet(cls, magnet_link: Union[str, List[str]], category: str = None) -> bool:
if category == "None":
category = None

with qbittorrentapi.Client(**Configs.config.client.connection_string) as qbt_client:
logger.debug(f"Adding magnet with category {category}")
qbt_client.torrents_add(urls=magnet_link, category=category)
result = qbt_client.torrents_add(urls=magnet_link, category=category)

return result == "Ok."

@classmethod
def add_torrent(cls, file_name: str, category: str = None) -> None:
def add_torrent(cls, file_name: str, category: str = None) -> bool:
if category == "None":
category = None

try:
with qbittorrentapi.Client(**Configs.config.client.connection_string) as qbt_client:
logger.debug(f"Adding torrent with category {category}")
qbt_client.torrents_add(torrent_files=file_name, category=category)
result = qbt_client.torrents_add(torrent_files=file_name, category=category)
return result == "Ok."

except qbittorrentapi.exceptions.UnsupportedMediaType415Error:
pass
Expand Down

0 comments on commit 912f1ba

Please sign in to comment.