Skip to content

Commit def652c

Browse files
DDSRemclaude
andcommitted
fix(rtorrent): address code review feedback
- Replace direct _proxy access in transfer_completed with set_torrents_tag(overwrite=True) for proper encapsulation and error logging - Optimize episode collection by using set accumulation instead of repeated list-set conversions in loop - Fix type hint for hashs parameter in transfer_completed (str -> Union[str, list]) - Add overwrite parameter to set_torrents_tag to support tag replacement Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c35faf5 commit def652c

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

app/modules/rtorrent/__init__.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def __get_torrent_info() -> Tuple[Optional[Torrent], Optional[bytes]]:
196196
# 不需要的文件ID
197197
file_ids = []
198198
# 需要的集清单
199-
sucess_epidised = []
199+
sucess_epidised = set()
200200
try:
201201
for torrent_file in torrent_files:
202202
file_id = torrent_file.get("id")
@@ -206,10 +206,11 @@ def __get_torrent_info() -> Tuple[Optional[Torrent], Optional[bytes]]:
206206
or not set(meta_info.episode_list).issubset(episodes):
207207
file_ids.append(file_id)
208208
else:
209-
sucess_epidised = list(set(sucess_epidised).union(set(meta_info.episode_list)))
209+
sucess_epidised.update(meta_info.episode_list)
210210
finally:
211211
torrent_files.clear()
212212
del torrent_files
213+
sucess_epidised = list(sucess_epidised)
213214
if sucess_epidised and file_ids:
214215
# 设置不需要的文件优先级为0(不下载)
215216
server.set_files(torrent_hash=torrent_hash, file_ids=file_ids, priority=0)
@@ -321,7 +322,7 @@ def list_torrents(self, status: TorrentStatus = None,
321322
return None
322323
return ret_torrents # noqa
323324

324-
def transfer_completed(self, hashs: str, downloader: Optional[str] = None) -> None:
325+
def transfer_completed(self, hashs: Union[str, list], downloader: Optional[str] = None) -> None:
325326
"""
326327
转移完成后的处理
327328
:param hashs: 种子Hash
@@ -338,15 +339,7 @@ def transfer_completed(self, hashs: str, downloader: Optional[str] = None) -> No
338339
else:
339340
tags = ['已整理']
340341
# 直接设置完整标签(覆盖)
341-
if isinstance(hashs, str):
342-
hashs_list = [hashs]
343-
else:
344-
hashs_list = hashs
345-
for tid in hashs_list:
346-
try:
347-
server._proxy.d.custom1.set(tid, ",".join(tags))
348-
except Exception:
349-
pass
342+
server.set_torrents_tag(ids=hashs, tags=tags, overwrite=True)
350343
return None
351344

352345
def remove_torrents(self, hashs: Union[str, list], delete_file: Optional[bool] = True,

app/modules/rtorrent/rtorrent.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,12 @@ def set_files(self, torrent_hash: str = None, file_ids: list = None, priority: i
402402
logger.error(f"设置种子文件状态出错:{str(err)}")
403403
return False
404404

405-
def set_torrents_tag(self, ids: Union[str, list], tags: List[str]) -> bool:
405+
def set_torrents_tag(self, ids: Union[str, list], tags: List[str], overwrite: bool = False) -> bool:
406406
"""
407407
设置种子标签(使用d.custom1)
408+
:param ids: 种子Hash
409+
:param tags: 标签列表
410+
:param overwrite: 是否覆盖现有标签,默认为合并
408411
"""
409412
if not self._proxy:
410413
return False
@@ -414,12 +417,16 @@ def set_torrents_tag(self, ids: Union[str, list], tags: List[str]) -> bool:
414417
if isinstance(ids, str):
415418
ids = [ids]
416419
for tid in ids:
417-
# 获取现有标签
418-
existing = self._proxy.d.custom1(tid)
419-
existing_tags = [t.strip() for t in existing.split(",") if t.strip()] if existing else []
420-
# 合并标签
421-
merged = list(set(existing_tags + tags))
422-
self._proxy.d.custom1.set(tid, ",".join(merged))
420+
if overwrite:
421+
# 直接覆盖标签
422+
self._proxy.d.custom1.set(tid, ",".join(tags))
423+
else:
424+
# 获取现有标签
425+
existing = self._proxy.d.custom1(tid)
426+
existing_tags = [t.strip() for t in existing.split(",") if t.strip()] if existing else []
427+
# 合并标签
428+
merged = list(set(existing_tags + tags))
429+
self._proxy.d.custom1.set(tid, ",".join(merged))
423430
return True
424431
except Exception as err:
425432
logger.error(f"设置种子Tag出错:{str(err)}")

0 commit comments

Comments
 (0)