Skip to content

Commit

Permalink
fix: fix errors after change transmission lib
Browse files Browse the repository at this point in the history
  • Loading branch information
verdel committed Feb 15, 2024
1 parent a7be455 commit 7a00161
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
28 changes: 14 additions & 14 deletions transmission_telegram_bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async def get_torrents(update, context, **kwargs):
db_torrents = await db.get_torrent_by_uid(update.effective_chat.id)
if db_torrents:
for db_entry in db_torrents:
torrent = transmission.get_torrent(db_entry[1])
torrent = transmission.get_torrent(int(db_entry[1]))
torrents.append(torrent)
elif permission == "all":
torrents = transmission.get_torrents()
Expand Down Expand Up @@ -172,7 +172,7 @@ async def download_torrent_logic(update, context):
await error_action(update, context)
else:
try:
await db.add_torrent(update.callback_query.message.chat.id, result.id)
await db.add_torrent(update.callback_query.message.chat.id, str(result.id))
except Exception:
await error_action(update, context)

Expand Down Expand Up @@ -237,8 +237,8 @@ async def delete_torrent_logic(update, context): # noqa: C901
else:
torrent_name = ""
try:
torrent_name = transmission.get_torrent(callback_data).name
transmission.remove_torrent(torrent_id=callback_data)
torrent_name = transmission.get_torrent(int(callback_data)).name
transmission.remove_torrent(torrent_id=int(callback_data))
except Exception:
await error_action(update, context)
if torrent_name != "":
Expand All @@ -265,7 +265,7 @@ async def delete_torrent_logic(update, context): # noqa: C901
)
else:
try:
torrent_name = transmission.get_torrent(callback_data).name
torrent_name = transmission.get_torrent(int(callback_data)).name
except Exception:
await error_action(update, context)
else:
Expand All @@ -292,7 +292,7 @@ async def list_torrent_action(update, context, **kwargs):
if len(torrents) > 0:
try:
for torrent in torrents:
if not torrent.doneDate:
if not torrent.done_date:
try:
eta = str(torrent.eta)
except ValueError:
Expand All @@ -302,19 +302,19 @@ async def list_torrent_action(update, context, **kwargs):
*{torrent.name}*
Status: {torrent.status}
Procent: {round(torrent.progress, 2)}%
Speed: {tools.humanize_bytes(torrent.rateDownload)}/s
Speed: {tools.humanize_bytes(torrent.rate_download)}/s
ETA: {eta}
Peers: {torrent.peersSendingToUs}
Peers: {torrent.peers_sending_to_us}
"""
)
else:
torrent_info = dedent(
f"""
*{torrent.name}*
Status: {torrent.status}
Speed: {tools.humanize_bytes(torrent.rateUpload)}/s
Peers: {torrent.peersGettingFromUs}
Ratio: {torrent.uploadRatio}
Speed: {tools.humanize_bytes(torrent.rate_upload)}/s
Peers: {torrent.peers_getting_from_us}
Ratio: {torrent.upload_ratio}
"""
)
await context.bot.send_message(
Expand Down Expand Up @@ -418,17 +418,17 @@ async def check_torrent_download_status(context): # noqa: C901
if torrents:
for torrent in torrents:
try:
task = transmission.get_torrent(torrent[1])
task = transmission.get_torrent(int(torrent[1]))
except Exception:
await db.remove_torrent_by_id(torrent[1])
else:
try:
if task.doneDate:
if task.done_date:
await db.complete_torrent(torrent[1])
except Exception as exc:
logger.error(f"{type(exc).__name__}({exc})")
else:
if task.doneDate:
if task.done_date:
response = f'Torrent "*{task.name}*" was successfully downloaded'
try:
notify_flag = False
Expand Down
12 changes: 5 additions & 7 deletions transmission_telegram_bot/transmission.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from typing import Any

from transmission_rpc import Client
from transmission_rpc import Client, Torrent


class Transmission(object):
Expand All @@ -23,19 +21,19 @@ def __get_client(self) -> Client:
"""Construct transmission client instance"""
return Client(host=self.address, port=self.port, username=self.user, password=self.password)

def get_torrents(self) -> list[Any]:
def get_torrents(self) -> list[Torrent]:
"""Get all torrents from transmission"""
return self.tc.get_torrents()

def get_torrent(self, torrent_id: str) -> Any:
def get_torrent(self, torrent_id: int) -> Torrent:
"""Get torrent by torrent id"""
return self.tc.get_torrent(torrent_id)

def remove_torrent(self, torrent_id: str, delete_data: bool = True):
def remove_torrent(self, torrent_id: int, delete_data: bool = True):
"""Remove torrent by torrent id"""
return self.tc.remove_torrent(ids=torrent_id, delete_data=delete_data)

def add_torrent(self, torrent_data: str = "", **kwargs):
def add_torrent(self, torrent_data: str = "", **kwargs) -> Torrent:
"""Add torrent to transmission"""
if "download_dir" in kwargs:
return self.tc.add_torrent(torrent=torrent_data, download_dir=kwargs.get("download_dir"))
Expand Down

0 comments on commit 7a00161

Please sign in to comment.