fix #144 交互命令下载 /dt 种子url

This commit is contained in:
thsrite
2024-11-19 20:53:05 +08:00
parent a552d40e66
commit 004a2f7be0
2 changed files with 106 additions and 38 deletions

View File

@@ -444,11 +444,12 @@
"name": "添加种子下载",
"description": "选择下载器,添加种子任务。",
"labels": "站点",
"version": "2.0",
"version": "2.1",
"icon": "download.png",
"author": "thsrite",
"level": 1,
"history": {
"v2.1": "交互命令下载 /dt 种子url",
"v2.0": "兼容V2版本",
"v1.1": "支持选择MoviePilot配置的下载路径",
"v1.0": "删除下载器中该站点辅种,保留该站点没有辅种的种子"

View File

@@ -1,14 +1,16 @@
from typing import Any, List, Dict, Tuple, Optional
from app.core.event import eventmanager, Event
from app.db.site_oper import SiteOper
from app.plugins import _PluginBase
from app.log import logger
from app.schemas.types import EventType
from app.utils.string import StringUtils
from app.schemas import ServiceInfo
from app.helper.downloader import DownloaderHelper
from app.helper.directory import DirectoryHelper
class DownloadTorrent(_PluginBase):
# 插件名称
plugin_name = "添加种子下载"
@@ -17,7 +19,7 @@ class DownloadTorrent(_PluginBase):
# 插件图标
plugin_icon = "download.png"
# 插件版本
plugin_version = "2.0"
plugin_version = "2.1"
# 插件作者
plugin_author = "thsrite"
# 作者主页
@@ -31,6 +33,7 @@ class DownloadTorrent(_PluginBase):
# 私有属性
_is_paused = False
_enabled = False
_save_path = None
_mp_path = None
_downloader = None
@@ -45,6 +48,7 @@ class DownloadTorrent(_PluginBase):
self.site = SiteOper()
if config:
self._enabled = config.get("enabled")
self._is_paused = config.get("is_paused")
self._save_path = config.get("save_path")
self._mp_path = config.get("mp_path")
@@ -54,36 +58,66 @@ class DownloadTorrent(_PluginBase):
# 下载种子
if self._torrent_urls:
for torrent_url in str(self._torrent_urls).split("\n"):
# 获取种子对应站点cookie
domain = StringUtils.get_url_domain(torrent_url)
if not domain:
logger.error(f"种子 {torrent_url} 获取站点域名失败,跳过处理")
continue
# 查询站点
site = self.site.get_by_domain(domain)
if not site or not site.cookie:
logger.error(f"种子 {torrent_url} 获取站点cookie失败跳过处理")
continue
service = self.service_info(self._downloader)
download_id = self.__download(service=service,
content=torrent_url,
save_path=self._save_path or self._mp_path,
cookie=site.cookie)
if download_id:
logger.info(f"种子添加下载成功 {torrent_url} 保存位置 {self._save_path or self._mp_path}")
else:
logger.error(f"种子添加下载失败 {torrent_url} 保存位置 {self._save_path or self._mp_path}")
self.__download_torrent(torrent_url)
self.update_config({
"downloader": self._downloader,
"save_path": self._save_path,
"enabled": self._enabled,
"mp_path": self._mp_path,
"is_paused": self._is_paused
})
def __download_torrent(self, torrent_url: str):
"""
下载种子
"""
# 获取种子对应站点cookie
domain = StringUtils.get_url_domain(torrent_url)
if not domain:
logger.error(f"种子 {torrent_url} 获取站点域名失败,跳过处理")
return None, None
# 查询站点
site = self.site.get_by_domain(domain)
if not site or not site.cookie:
logger.error(f"种子 {torrent_url} 获取站点cookie失败跳过处理")
return None, None
service = self.service_info(self._downloader)
download_id = self.__download(service=service,
content=torrent_url,
save_path=self._save_path or self._mp_path,
cookie=site.cookie)
if download_id:
logger.info(f"种子添加下载成功 {torrent_url} 保存位置 {self._save_path or self._mp_path}")
return site.name, f"种子添加下载成功, 保存位置 {self._save_path or self._mp_path}"
else:
logger.error(f"种子添加下载失败 {torrent_url} 保存位置 {self._save_path or self._mp_path}")
return site.name, f"种子添加下载失败, 保存位置 {self._save_path or self._mp_path}"
@eventmanager.register(EventType.PluginAction)
def remote_sync_one(self, event: Event = None):
if event:
event_data = event.event_data
if not event_data or event_data.get("action") != "download_torrent":
return
args = event_data.get("arg_str")
if not args:
logger.error(f"缺少参数:{event_data}")
return
site_name, result = self.__download_torrent(args)
if not result:
self.post_message(channel=event.event_data.get("channel"),
title="添加种子下载失败",
userid=event.event_data.get("user"))
else:
self.post_message(channel=event.event_data.get("channel"),
title=f"{site_name} {result}",
userid=event.event_data.get("user"))
def service_info(self, name: str) -> Optional[ServiceInfo]:
"""
服务信息
@@ -101,8 +135,8 @@ class DownloadTorrent(_PluginBase):
logger.warning(f"下载器 {name} 未连接,请检查配置")
return None
return service
def __download(self, service: ServiceInfo, content: bytes,
def __download(self, service: ServiceInfo, content: str,
save_path: str, cookie: str) -> Optional[str]:
"""
添加下载任务
@@ -112,13 +146,13 @@ class DownloadTorrent(_PluginBase):
downloader = service.instance
if self.downloader_helper.is_downloader("qbittorrent", service=service):
torrent = downloader.add_torrent(content=content,
download_dir=save_path,
is_paused=self._is_paused,
cookie=cookie)
download_dir=save_path,
is_paused=self._is_paused,
cookie=cookie)
if not torrent:
return None
else:
return torrent
return torrent
elif self.downloader_helper.is_downloader("transmission", service=service):
# 添加任务
torrent = downloader.add_torrent(content=content,
@@ -133,13 +167,22 @@ class DownloadTorrent(_PluginBase):
logger.error(f"不支持的下载器类型")
return None
def get_state(self) -> bool:
return False
return self._enabled
@staticmethod
def get_command() -> List[Dict[str, Any]]:
pass
return [
{
"cmd": "/dt",
"event": EventType.PluginAction,
"desc": "种子下载",
"category": "",
"data": {
"action": "download_torrent"
}
}
]
def get_api(self) -> List[Dict[str, Any]]:
pass
@@ -148,12 +191,35 @@ class DownloadTorrent(_PluginBase):
"""
拼装插件配置页面需要返回两块数据1、页面配置2、数据结构
"""
dir_conf = [{'title': d.name, 'value': d.download_path} for d in self.directory_helper.get_local_download_dirs()]
downloader_options = [{"title": config.name, "value": config.name} for config in self.downloader_helper.get_configs().values()]
dir_conf = [{'title': d.name, 'value': d.download_path} for d in
self.directory_helper.get_local_download_dirs()]
downloader_options = [{"title": config.name, "value": config.name} for config in
self.downloader_helper.get_configs().values()]
return [
{
'component': 'VForm',
'content': [
{
'component': 'VRow',
'content': [
{
'component': 'VCol',
'props': {
'cols': 12,
'md': 4
},
'content': [
{
'component': 'VSwitch',
'props': {
'model': 'enabled',
'label': '启用插件',
}
}
]
},
]
},
{
'component': 'VRow',
'content': [
@@ -169,7 +235,7 @@ class DownloadTorrent(_PluginBase):
'props': {
'model': 'downloader',
'label': '下载器',
'items': downloader_options
'items': downloader_options
}
}
]
@@ -299,6 +365,7 @@ class DownloadTorrent(_PluginBase):
], {
"downloader": "qb",
"is_paused": False,
"enabled": False,
"save_path": "",
"mp_path": "",
"torrent_urls": ""
@@ -311,4 +378,4 @@ class DownloadTorrent(_PluginBase):
"""
退出插件
"""
pass
pass