fix downloader

This commit is contained in:
jxxghp
2025-06-26 20:52:17 +08:00
parent 27dd681d9f
commit 8068523d88
4 changed files with 225 additions and 200 deletions
+21 -7
View File
@@ -166,6 +166,7 @@ class QbittorrentModule(_ModuleBase, _DownloaderBase[Qbittorrent]):
if error:
return None, None, None, "无法连接qbittorrent下载器"
if torrents:
try:
for torrent in torrents:
# 名称与大小相等则认为是同一个种子
if torrent.get("name") == torrent_name and torrent.get("total_size") == torrent_size:
@@ -179,6 +180,8 @@ class QbittorrentModule(_ModuleBase, _DownloaderBase[Qbittorrent]):
logger.info(f"给种子 {torrent_hash} 打上标签:{settings.TORRENT_TAG}")
server.set_torrents_tag(ids=torrent_hash, tags=[settings.TORRENT_TAG])
return downloader or self.get_default_config_name(), torrent_hash, torrent_layout, f"下载任务已存在"
finally:
torrents.clear()
return None, None, None, f"添加种子任务失败:{content}"
else:
# 获取种子Hash
@@ -196,7 +199,7 @@ class QbittorrentModule(_ModuleBase, _DownloaderBase[Qbittorrent]):
file_ids = []
# 需要的集清单
sucess_epidised = []
try:
for torrent_file in torrent_files:
file_id = torrent_file.get("id")
file_name = torrent_file.get("name")
@@ -206,6 +209,8 @@ class QbittorrentModule(_ModuleBase, _DownloaderBase[Qbittorrent]):
file_ids.append(file_id)
else:
sucess_epidised = list(set(sucess_epidised).union(set(meta_info.episode_list)))
finally:
torrent_files.clear()
if sucess_epidised and file_ids:
# 选择文件
server.set_files(torrent_hash=torrent_hash, file_ids=file_ids, priority=0)
@@ -244,8 +249,9 @@ class QbittorrentModule(_ModuleBase, _DownloaderBase[Qbittorrent]):
if hashs:
# 按Hash获取
for name, server in servers.items():
torrents, _ = server.get_torrents(ids=hashs, tags=settings.TORRENT_TAG)
for torrent in torrents or []:
torrents, _ = server.get_torrents(ids=hashs, tags=settings.TORRENT_TAG) or []
try:
for torrent in torrents:
content_path = torrent.get("content_path")
if content_path:
torrent_path = Path(content_path)
@@ -261,11 +267,14 @@ class QbittorrentModule(_ModuleBase, _DownloaderBase[Qbittorrent]):
progress=torrent.get('progress') * 100,
state="paused" if torrent.get('state') in ("paused", "pausedDL") else "downloading",
))
finally:
torrents.clear()
elif status == TorrentStatus.TRANSFER:
# 获取已完成且未整理的
for name, server in servers.items():
torrents = server.get_completed_torrents(tags=settings.TORRENT_TAG)
for torrent in torrents or []:
torrents = server.get_completed_torrents(tags=settings.TORRENT_TAG) or []
try:
for torrent in torrents:
tags = torrent.get("tags") or []
if "已整理" in tags:
continue
@@ -282,11 +291,14 @@ class QbittorrentModule(_ModuleBase, _DownloaderBase[Qbittorrent]):
hash=torrent.get('hash'),
tags=torrent.get('tags')
))
finally:
torrents.clear()
elif status == TorrentStatus.DOWNLOADING:
# 获取正在下载的任务
for name, server in servers.items():
torrents = server.get_downloading_torrents(tags=settings.TORRENT_TAG)
for torrent in torrents or []:
torrents = server.get_downloading_torrents(tags=settings.TORRENT_TAG) or []
try:
for torrent in torrents:
meta = MetaInfo(torrent.get('name'))
ret_torrents.append(DownloadingTorrent(
downloader=name,
@@ -305,6 +317,8 @@ class QbittorrentModule(_ModuleBase, _DownloaderBase[Qbittorrent]):
'dlspeed')) if torrent.get(
'dlspeed') > 0 else ''
))
finally:
torrents.clear()
else:
return None
return ret_torrents # noqa
+8 -11
View File
@@ -12,16 +12,9 @@ from app.utils.string import StringUtils
class Qbittorrent:
_host: Optional[str] = None
_port: int = None
_username: Optional[str] = None
_password: Optional[str] = None
_category: Optional[bool] = False
_sequentail: Optional[bool] = False
_force_resume: Optional[bool] = False
qbc: Client = None
"""
qbittorrent下载器
"""
def __init__(self, host: Optional[str] = None, port: int = None,
username: Optional[str] = None, password: Optional[str] = None,
category: Optional[bool] = False, sequentail: Optional[bool] = False,
@@ -43,7 +36,6 @@ class Qbittorrent:
self._sequentail = sequentail
self._force_resume = force_resume
self._first_last_piece = first_last_piece
if self._host and self._port:
self.qbc = self.__login_qbittorrent()
def is_inactive(self) -> bool:
@@ -65,6 +57,8 @@ class Qbittorrent:
连接qbittorrent
:return: qbittorrent对象
"""
if not self._host or not self._port:
return None
try:
# 登录
logger.info(f"正在连接 qbittorrent{self._host}:{self._port}")
@@ -104,10 +98,13 @@ class Qbittorrent:
results = []
if not isinstance(tags, list):
tags = [tags]
try:
for torrent in torrents:
torrent_tags = [str(tag).strip() for tag in torrent.get("tags").split(',')]
if set(tags).issubset(set(torrent_tags)):
results.append(torrent)
finally:
torrents.clear()
return results, False
return torrents or [], False
except Exception as err:
+21 -6
View File
@@ -163,6 +163,7 @@ class TransmissionModule(_ModuleBase, _DownloaderBase[Transmission]):
if error:
return None, None, None, "无法连接transmission下载器"
if torrents:
try:
for torrent in torrents:
# 名称与大小相等则认为是同一个种子
if torrent.name == torrent_name and torrent.total_size == torrent_size:
@@ -181,6 +182,8 @@ class TransmissionModule(_ModuleBase, _DownloaderBase[Transmission]):
labels.append(settings.TORRENT_TAG)
server.set_torrent_tag(ids=torrent_hash, tags=labels)
return downloader or self.get_default_config_name(), torrent_hash, torrent_layout, f"下载任务已存在"
finally:
torrents.clear()
return None, None, None, f"添加种子任务失败:{content}"
else:
torrent_hash = torrent.hashString
@@ -192,6 +195,7 @@ class TransmissionModule(_ModuleBase, _DownloaderBase[Transmission]):
# 需要的文件信息
file_ids = []
unwanted_file_ids = []
try:
for torrent_file in torrent_files:
file_id = torrent_file.id
file_name = torrent_file.name
@@ -209,6 +213,8 @@ class TransmissionModule(_ModuleBase, _DownloaderBase[Transmission]):
server.set_unwanted_files(torrent_hash, unwanted_file_ids)
# 开始任务
server.start_torrents(torrent_hash)
finally:
torrent_files.clear()
return downloader or self.get_default_config_name(), torrent_hash, torrent_layout, "添加下载任务成功"
else:
return downloader or self.get_default_config_name(), torrent_hash, torrent_layout, "添加下载任务成功"
@@ -236,8 +242,9 @@ class TransmissionModule(_ModuleBase, _DownloaderBase[Transmission]):
if hashs:
# 按Hash获取
for name, server in servers.items():
torrents, _ = server.get_torrents(ids=hashs, tags=settings.TORRENT_TAG)
for torrent in torrents or []:
torrents, _ = server.get_torrents(ids=hashs, tags=settings.TORRENT_TAG) or []
try:
for torrent in torrents:
ret_torrents.append(TransferTorrent(
downloader=name,
title=torrent.name,
@@ -246,11 +253,14 @@ class TransmissionModule(_ModuleBase, _DownloaderBase[Transmission]):
size=torrent.total_size,
tags=",".join(torrent.labels or [])
))
finally:
torrents.clear()
elif status == TorrentStatus.TRANSFER:
# 获取已完成且未整理的
for name, server in servers.items():
torrents = server.get_completed_torrents(tags=settings.TORRENT_TAG)
for torrent in torrents or []:
torrents = server.get_completed_torrents(tags=settings.TORRENT_TAG) or []
try:
for torrent in torrents:
# 含"已整理"tag的不处理
if "已整理" in torrent.labels or []:
continue
@@ -269,11 +279,14 @@ class TransmissionModule(_ModuleBase, _DownloaderBase[Transmission]):
progress=torrent.progress,
state="paused" if torrent.status == "stopped" else "downloading",
))
finally:
torrents.clear()
elif status == TorrentStatus.DOWNLOADING:
# 获取正在下载的任务
for name, server in servers.items():
torrents = server.get_downloading_torrents(tags=settings.TORRENT_TAG)
for torrent in torrents or []:
torrents = server.get_downloading_torrents(tags=settings.TORRENT_TAG) or []
try:
for torrent in torrents:
meta = MetaInfo(torrent.name)
dlspeed = torrent.rate_download if hasattr(torrent, "rate_download") else torrent.rateDownload
upspeed = torrent.rate_upload if hasattr(torrent, "rate_upload") else torrent.rateUpload
@@ -291,6 +304,8 @@ class TransmissionModule(_ModuleBase, _DownloaderBase[Transmission]):
upspeed=StringUtils.str_filesize(upspeed),
left_time=StringUtils.str_secends(torrent.left_until_done / dlspeed) if dlspeed > 0 else ''
))
finally:
torrents.clear()
else:
return None
return ret_torrents # noqa
+10 -11
View File
@@ -1,4 +1,4 @@
from typing import Optional, Union, Tuple, List, Literal
from typing import Optional, Union, Tuple, List
import transmission_rpc
from transmission_rpc import Client, Torrent, File
@@ -9,14 +9,9 @@ from app.utils.url import UrlUtils
class Transmission:
_protocol: Literal["http", "https"] = "http"
_host: Optional[str] = None
_port: Optional[int] = None
_username: Optional[str] = None
_password: Optional[str] = None
trc: Optional[Client] = None
"""
Transmission下载器
"""
# 参考transmission web,仅查询需要的参数,加速种子搜索
_trarg = ["id", "name", "status", "labels", "hashString", "totalSize", "percentDone", "addedDate", "trackerList",
"trackerStats",
@@ -43,7 +38,6 @@ class Transmission:
return
self._username = username
self._password = password
if self._host and self._port:
self.trc = self.__login_transmission()
def __login_transmission(self) -> Optional[Client]:
@@ -51,10 +45,12 @@ class Transmission:
连接transmission
:return: transmission对象
"""
if not self._host or not self._port:
return None
try:
# 登录
logger.info(f"正在连接 transmission{self._protocol}://{self._host}:{self._port}")
trt = transmission_rpc.Client(protocol=self._protocol,
trt = transmission_rpc.Client(protocol=self._protocol, # noqa
host=self._host,
port=self._port,
username=self._username,
@@ -97,6 +93,7 @@ class Transmission:
if tags and not isinstance(tags, list):
tags = [tags]
ret_torrents = []
try:
for torrent in torrents:
# 状态过滤
if status and torrent.status not in status:
@@ -107,6 +104,8 @@ class Transmission:
if tags and not set(tags).issubset(set(labels)):
continue
ret_torrents.append(torrent)
finally:
torrents.clear()
return ret_torrents, False
def get_completed_torrents(self, ids: Union[str, list] = None,