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>
This commit is contained in:
DDSRem
2026-02-22 13:40:15 +08:00
parent c35faf5356
commit def652c768
2 changed files with 19 additions and 19 deletions

View File

@@ -196,7 +196,7 @@ class RtorrentModule(_ModuleBase, _DownloaderBase[Rtorrent]):
# 不需要的文件ID
file_ids = []
# 需要的集清单
sucess_epidised = []
sucess_epidised = set()
try:
for torrent_file in torrent_files:
file_id = torrent_file.get("id")
@@ -206,10 +206,11 @@ class RtorrentModule(_ModuleBase, _DownloaderBase[Rtorrent]):
or not set(meta_info.episode_list).issubset(episodes):
file_ids.append(file_id)
else:
sucess_epidised = list(set(sucess_epidised).union(set(meta_info.episode_list)))
sucess_epidised.update(meta_info.episode_list)
finally:
torrent_files.clear()
del torrent_files
sucess_epidised = list(sucess_epidised)
if sucess_epidised and file_ids:
# 设置不需要的文件优先级为0不下载
server.set_files(torrent_hash=torrent_hash, file_ids=file_ids, priority=0)
@@ -321,7 +322,7 @@ class RtorrentModule(_ModuleBase, _DownloaderBase[Rtorrent]):
return None
return ret_torrents # noqa
def transfer_completed(self, hashs: str, downloader: Optional[str] = None) -> None:
def transfer_completed(self, hashs: Union[str, list], downloader: Optional[str] = None) -> None:
"""
转移完成后的处理
:param hashs: 种子Hash
@@ -338,15 +339,7 @@ class RtorrentModule(_ModuleBase, _DownloaderBase[Rtorrent]):
else:
tags = ['已整理']
# 直接设置完整标签(覆盖)
if isinstance(hashs, str):
hashs_list = [hashs]
else:
hashs_list = hashs
for tid in hashs_list:
try:
server._proxy.d.custom1.set(tid, ",".join(tags))
except Exception:
pass
server.set_torrents_tag(ids=hashs, tags=tags, overwrite=True)
return None
def remove_torrents(self, hashs: Union[str, list], delete_file: Optional[bool] = True,

View File

@@ -402,9 +402,12 @@ class Rtorrent:
logger.error(f"设置种子文件状态出错:{str(err)}")
return False
def set_torrents_tag(self, ids: Union[str, list], tags: List[str]) -> bool:
def set_torrents_tag(self, ids: Union[str, list], tags: List[str], overwrite: bool = False) -> bool:
"""
设置种子标签使用d.custom1
:param ids: 种子Hash
:param tags: 标签列表
:param overwrite: 是否覆盖现有标签,默认为合并
"""
if not self._proxy:
return False
@@ -414,12 +417,16 @@ class Rtorrent:
if isinstance(ids, str):
ids = [ids]
for tid in ids:
# 获取现有标签
existing = self._proxy.d.custom1(tid)
existing_tags = [t.strip() for t in existing.split(",") if t.strip()] if existing else []
# 合并标签
merged = list(set(existing_tags + tags))
self._proxy.d.custom1.set(tid, ",".join(merged))
if overwrite:
# 直接覆盖标签
self._proxy.d.custom1.set(tid, ",".join(tags))
else:
# 获取现有标签
existing = self._proxy.d.custom1(tid)
existing_tags = [t.strip() for t in existing.split(",") if t.strip()] if existing else []
# 合并标签
merged = list(set(existing_tags + tags))
self._proxy.d.custom1.set(tid, ",".join(merged))
return True
except Exception as err:
logger.error(f"设置种子Tag出错{str(err)}")