Skip to content

Commit b454dc1

Browse files
committed
Better feedback performance
- by skippping qbt types in additional calls - fixed date removal (was not respecting --keep) - clarified some feedback text
1 parent 7d372fd commit b454dc1

2 files changed

Lines changed: 57 additions & 13 deletions

File tree

‎src/kiwixseeder/qbittorrent.py‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
# pyright: reportUnknownMemberType=false, reportArgumentType=false, reportUnknownArgumentType=false, reportUnknownParameterType=false, reportUnknownVariableType=false, reportMissingImports=false
2+
13
import datetime
24
import time
5+
from collections.abc import Generator
36
from dataclasses import dataclass
47
from typing import Self
58

@@ -109,10 +112,26 @@ def add_url(self, url: str, btih: str | None) -> str:
109112
finally:
110113
self.reload()
111114

115+
def get_all_added_ts(self, btihs: list[str]) -> list[int]:
116+
"""added-on timestamps for a list of btihs"""
117+
return [
118+
int(tdict["added_on"])
119+
for tdict in client.torrents.info(
120+
torrent_hashes=btihs, SIMPLE_RESPONSES=True
121+
)
122+
]
123+
124+
def get_all(self, btihs: list[str] | None = None) -> Generator[TorrentInfo]:
125+
"""TorrentInfo for all btihs or a supplied list of them"""
126+
if not btihs:
127+
btihs = list(self.btihs.keys())
128+
for tdict in client.torrents.info(torrent_hashes=btihs, SIMPLE_RESPONSES=True):
129+
yield TorrentInfo.from_torrentdict(tdict)
130+
112131
def get(self, ident: str) -> TorrentInfo:
113132
"""Torrent dict from its hash"""
114133
return TorrentInfo.from_torrentdict(
115-
client.torrents.info(torrent_hashes=ident, SIMPLE_RESPONSES=True)[0] # pyright: ignore[reportUnknownArgumentType, reportArgumentType]
134+
client.torrents.info(torrent_hashes=ident, SIMPLE_RESPONSES=True)[0]
116135
)
117136

118137
def get_or_none(
@@ -146,4 +165,11 @@ def remove(self, ident: str) -> bool:
146165
def total_size(self) -> int:
147166
"""total size of our torrents"""
148167
self.reload()
149-
return sum(self.get(btih).size for btih in self.btihs)
168+
return sum(
169+
[
170+
int(tdict["size"])
171+
for tdict in client.torrents.info(
172+
torrent_hashes=self.btihs, SIMPLE_RESPONSES=True
173+
)
174+
]
175+
)

‎src/kiwixseeder/runner.py‎

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ def run(self) -> int:
9292
f"in {QBT_CAT_NAME}"
9393
)
9494
if context.debug:
95-
for btih in self.manager.btihs:
96-
logger.debug(f"* {self.manager.get(btih)!s}")
95+
for torrent in self.manager.get_all():
96+
logger.debug(f"* {torrent!s}")
9797

9898
self.remove_outdated_torrents()
9999
self.reconcile_books_and_torrents()
@@ -162,10 +162,11 @@ def remove_outdated_torrents(self):
162162

163163
# reconciling existing torrents and books
164164
unselected_books = list(self.manager.btihs.keys())
165+
all_btihs = self.manager.btihs.items()
165166
for book in self.books:
166167
btihs = [
167168
btih
168-
for btih, fname in self.manager.btihs.items()
169+
for btih, fname in all_btihs
169170
# having condition on name first is important and it allows
170171
# us to only compare on btih if name matches.
171172
# we cant direclty compare on btih as it would require getting the
@@ -177,26 +178,41 @@ def remove_outdated_torrents(self):
177178
book.btih = btihs[0]
178179
unselected_books.remove(book.btih)
179180

181+
# get added_on for all btih
182+
all_added_ts: dict[str, int] = dict(
183+
zip(
184+
unselected_books,
185+
self.manager.get_all_added_ts(unselected_books),
186+
strict=False,
187+
)
188+
)
180189
# keep those that are within --keep duration
181-
keep_until = self.now - datetime.timedelta(seconds=context.keep_for)
190+
keep_until = (
191+
self.now - datetime.timedelta(seconds=context.keep_for)
192+
).timestamp()
182193
for btih in unselected_books:
183-
if self.manager.get(btih).added_on <= keep_until:
194+
# keep (remove from unselected) is added to BT after that date in the past
195+
if all_added_ts[btih] > keep_until:
184196
unselected_books.remove(btih)
197+
else:
198+
logger.error(
199+
f"{self.manager.get(btih).added_on} is before {keep_until}"
200+
)
185201

186202
if not unselected_books:
187203
logger.info("> None")
188204
return
189205

190206
logger.info(
191-
f"{self.banner}Removing {len(unselected_books)} outdated torrents "
192-
"(not in catalog, over --keep)…"
207+
f"{self.banner}Removing {len(unselected_books)} torrents: "
208+
"outside filters or not in catalog and anterior to --keep"
193209
)
194-
for btih in unselected_books:
195-
logger.info(f"- {self.manager.get(btih)!s}")
210+
for torrent in self.manager.get_all(btihs=unselected_books):
211+
logger.info(f"- {torrent!s}")
196212
if context.dry_run:
197213
continue
198-
if not self.manager.remove(btih):
199-
logger.error(f"Failed to remove {btih}")
214+
if not self.manager.remove(torrent.btih):
215+
logger.error(f"Failed to remove {torrent.btih}")
200216

201217
def ensure_storage(self):
202218
torrents_size = self.manager.total_size
@@ -213,6 +229,8 @@ def ensure_storage(self):
213229

214230
if total_size > context.max_storage:
215231
logger.error("Total size exceeds max-storage")
232+
if context.dry_run:
233+
logger.error(">> does not account removal in dry-mode")
216234
return True
217235

218236
def reconcile_books_and_torrents(self):

0 commit comments

Comments
 (0)