Skip to content

Commit

Permalink
fix: fix Torrent type and complete event processing
Browse files Browse the repository at this point in the history
  • Loading branch information
verdel committed Apr 24, 2024
1 parent 739841e commit 4d0cd37
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 39 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "torrent-telegram-bot"
version = "0.1.2"
version = "0.1.3"
description = ""
authors = ["Vadim Aleksandrov <[email protected]>"]
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion torrent_telegram_bot/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.2"
__version__ = "0.1.3"
20 changes: 14 additions & 6 deletions torrent_telegram_bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ async def list_torrent_action(update, context, **kwargs):
if len(torrents) > 0:
try:
for torrent in torrents:
if torrent.done_date == datetime.fromtimestamp(0):
if torrent.done_date is None or torrent.done_date == datetime.fromtimestamp(0):
try:
eta = str(torrent.eta)
except ValueError:
Expand All @@ -309,16 +309,16 @@ async def list_torrent_action(update, context, **kwargs):
Procent: {round(torrent.progress, 2)}%
Speed: {tools.humanize_bytes(torrent.download_speed)}/s
ETA: {eta}
Peers: {torrent.num_seeds}
Peers: {torrent.num_seeds_download}
"""
)
else:
torrent_info = dedent(
f"""
*{torrent.name}*
Status: {torrent.status}
Speed: {tools.humanize_bytes(torrent.download_speed)}/s
Peers: {torrent.num_seeds}
Speed: {tools.humanize_bytes(torrent.upload_speed)}/s
Peers: {torrent.num_seeds_upload}
Ratio: {torrent.ratio}
"""
)
Expand Down Expand Up @@ -426,12 +426,20 @@ async def check_torrent_download_status(context): # noqa: C901
await db.remove_torrent_by_id(torrent[1])
else:
try:
if task is not None and task.done_date != datetime.fromtimestamp(0):
if (
task is not None
and task.done_date is not None
and task.done_date != datetime.fromtimestamp(0)
):
await db.complete_torrent(torrent[1])
except Exception as exc:
logger.error(f"{type(exc).__name__}({exc})")
else:
if task is not None and task.done_date != datetime.fromtimestamp(0):
if (
task is not None
and task.done_date is not None
and task.done_date != datetime.fromtimestamp(0)
):
response = f'Torrent "*{task.name}*" was successfully downloaded'
try:
notify_flag = False
Expand Down
8 changes: 6 additions & 2 deletions torrent_telegram_bot/custom_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ def __init__(
eta: datetime.timedelta | None,
progress: float,
download_speed: int,
num_seeds: int,
upload_speed: int,
num_seeds_download: int,
num_seeds_upload: int,
ratio: float,
):
self.torrent_id = torrent_id
Expand All @@ -21,5 +23,7 @@ def __init__(
self.eta = eta
self.progress = progress
self.download_speed = download_speed
self.num_seeds = num_seeds
self.upload_speed = upload_speed
self.num_seeds_download = num_seeds_download
self.num_seeds_upload = num_seeds_upload
self.ratio = ratio
12 changes: 9 additions & 3 deletions torrent_telegram_bot/qbittorrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ def get_torrents(self) -> list[Torrent]:
status=torrent.state,
progress=torrent.progress * 100,
download_speed=torrent.dlspeed,
num_seeds=torrent.num_seeds + torrent.num_leechs,
upload_speed=torrent.upspeed,
num_seeds_download=torrent.num_seeds + torrent.num_leechs,
num_seeds_upload=torrent.num_seeds + torrent.num_leechs,
ratio=torrent.ratio,
)
for torrent in torrents
Expand All @@ -64,7 +66,9 @@ def get_torrent(self, torrent_id: str) -> Torrent | None:
status=torrent.state,
progress=torrent.progress * 100,
download_speed=torrent.dlspeed,
num_seeds=torrent.num_seeds + torrent.num_leechs,
upload_speed=torrent.upspeed,
num_seeds_download=torrent.num_seeds + torrent.num_leechs,
num_seeds_upload=torrent.num_seeds + torrent.num_leechs,
ratio=torrent.ratio,
)

Expand Down Expand Up @@ -105,6 +109,8 @@ def add_torrent(self, torrent_data: bytes, **kwargs) -> Torrent:
status=torrent.state,
progress=torrent.progress * 100,
download_speed=torrent.dlspeed,
num_seeds=torrent.num_seeds + torrent.num_leechs,
upload_speed=torrent.upspeed,
num_seeds_download=torrent.num_seeds + torrent.num_leechs,
num_seeds_upload=torrent.num_seeds + torrent.num_leechs,
ratio=torrent.ratio,
)
47 changes: 21 additions & 26 deletions torrent_telegram_bot/transmission.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ def get_torrents(self) -> list[Torrent]:
status=torrent.status,
progress=torrent.progress,
download_speed=torrent.rate_download,
num_seeds=torrent.peers_sending_to_us,
ratio=torrent.rate_download,
upload_speed=torrent.rate_upload,
num_seeds_download=torrent.peers_sending_to_us,
num_seeds_upload=torrent.peers_getting_from_us,
ratio=torrent.ratio,
)
for torrent in torrents
]
Expand All @@ -52,8 +54,10 @@ def get_torrent(self, torrent_id: str) -> Torrent | None:
status=torrent.status,
progress=torrent.progress,
download_speed=torrent.rate_download,
num_seeds=torrent.peers_sending_to_us,
ratio=torrent.rate_download,
upload_speed=torrent.rate_upload,
num_seeds_download=torrent.peers_sending_to_us,
num_seeds_upload=torrent.peers_getting_from_us,
ratio=torrent.ratio,
)

def remove_torrent(self, torrent_id: str, delete_data: bool = True):
Expand All @@ -64,27 +68,18 @@ def add_torrent(self, torrent_data: bytes, **kwargs) -> Torrent:
"""Add torrent to client"""
if "download_dir" in kwargs:
torrent = self.client.add_torrent(torrent=torrent_data, download_dir=kwargs.get("download_dir"))
return Torrent(
torrent_id=str(torrent.id),
name=torrent.name,
done_date=torrent.done_date,
eta=torrent.eta,
status=torrent.status,
progress=torrent.progress,
download_speed=torrent.rate_download,
num_seeds=torrent.peers_sending_to_us,
ratio=torrent.rate_download,
)
else:
torrent = self.client.add_torrent(torrent=torrent_data)
return Torrent(
torrent_id=str(torrent.id),
name=torrent.name,
done_date=torrent.done_date,
eta=torrent.eta,
status=torrent.status,
progress=torrent.progress,
download_speed=torrent.rate_download,
num_seeds=torrent.peers_sending_to_us,
ratio=torrent.rate_download,
)
return Torrent(
torrent_id=str(torrent.id),
name=torrent.name,
done_date=None,
eta=None,
status="",
progress=0,
download_speed=0,
upload_speed=0,
num_seeds_download=0,
num_seeds_upload=0,
ratio=0,
)

0 comments on commit 4d0cd37

Please sign in to comment.