From b340ccbb18ddf9557954f676947f2f49ad08f0fe Mon Sep 17 00:00:00 2001 From: InfinityPacer <160988576+InfinityPacer@users.noreply.github.com> Date: Wed, 9 Oct 2024 01:56:53 +0800 Subject: [PATCH 1/2] feat(DownloadSiteTag): add support for v2 plugin --- plugins.v2/downloadsitetag/__init__.py | 163 +++++++++++++++++-------- 1 file changed, 113 insertions(+), 50 deletions(-) diff --git a/plugins.v2/downloadsitetag/__init__.py b/plugins.v2/downloadsitetag/__init__.py index 6c473e2..804a99c 100644 --- a/plugins.v2/downloadsitetag/__init__.py +++ b/plugins.v2/downloadsitetag/__init__.py @@ -1,25 +1,28 @@ import datetime -import pytz import threading from typing import List, Tuple, Dict, Any, Optional -from app.core.context import Context -from app.core.event import eventmanager, Event -from app.schemas.types import EventType, MediaType -from app.core.config import settings -from app.log import logger -from app.plugins import _PluginBase -from app.modules.qbittorrent import Qbittorrent -from app.modules.transmission import Transmission -from app.db.downloadhistory_oper import DownloadHistoryOper -from app.db.models.downloadhistory import DownloadHistory +import pytz +from app.helper.sites import SitesHelper from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.triggers.cron import CronTrigger -from app.helper.sites import SitesHelper + +from app.core.config import settings +from app.core.context import Context +from app.core.event import eventmanager, Event +from app.db.downloadhistory_oper import DownloadHistoryOper +from app.db.models.downloadhistory import DownloadHistory +from app.helper.downloader import DownloaderHelper +from app.log import logger +from app.plugins import _PluginBase +from app.schemas import ServiceInfo +from app.schemas.types import EventType, MediaType from app.utils.string import StringUtils class DownloadSiteTag(_PluginBase): + # region 全局定义 + # 插件名称 plugin_name = "下载任务分类与标签" # 插件描述 @@ -44,10 +47,9 @@ class DownloadSiteTag(_PluginBase): # 退出事件 _event = threading.Event() # 私有属性 - downloader_qb = None - downloader_tr = None downloadhistory_oper = None sites_helper = None + downloaderhelper = None _scheduler = None _enabled = False _onlyonce = False @@ -61,11 +63,42 @@ class DownloadSiteTag(_PluginBase): _category_movie = None _category_tv = None _category_anime = None + _downloaders = None + + # Property + + @property + def service_infos(self) -> Optional[Dict[str, ServiceInfo]]: + """ + 服务信息 + """ + if not self._downloaders: + logger.warning("尚未配置下载器,请检查配置") + return None + + services = self.downloaderhelper.get_services(name_filters=self._downloaders) + if not services: + logger.warning("获取下载器实例失败,请检查配置") + return None + + active_services = {} + for service_name, service_info in services.items(): + if service_info.instance.is_inactive(): + logger.warning(f"下载器 {service_name} 未连接,请检查配置") + else: + active_services[service_name] = service_info + + if not active_services: + logger.warning("没有已连接的下载器,请检查配置") + return None + + return active_services + + # endregion def init_plugin(self, config: dict = None): - self.downloader_qb = Qbittorrent() - self.downloader_tr = Transmission() self.downloadhistory_oper = DownloadHistoryOper() + self.downloaderhelper = DownloaderHelper() self.sites_helper = SitesHelper() # 读取配置 if config: @@ -81,14 +114,7 @@ class DownloadSiteTag(_PluginBase): self._category_movie = config.get("category_movie") or "电影" self._category_tv = config.get("category_tv") or "电视" self._category_anime = config.get("category_anime") or "动漫" - if not ("interval_cron" in config): - # 新版本v1.6更新插件配置默认配置 - config["interval"] = self._interval - config["interval_cron"] = self._interval_cron - config["interval_time"] = self._interval_time - config["interval_unit"] = self._interval_unit - self.update_config(config) - logger.warn(f"{self.LOG_TAG}新版本v{self.plugin_version} 配置修正 ...") + self._downloaders = config.get("downloaders") # 停止现有任务 self.stop_service() @@ -179,6 +205,8 @@ class DownloadSiteTag(_PluginBase): """ 补全下载历史的标签与分类 """ + if not self.service_infos: + return logger.info(f"{self.LOG_TAG}开始执行 ...") # 记录处理的种子, 供辅种(无下载历史)使用 dispose_history = {} @@ -192,21 +220,21 @@ class DownloadSiteTag(_PluginBase): "agsvpt.trackers.work": "agsvpt.com", "tracker.cinefiles.info": "audiences.me", } - for DOWNLOADER in ["qbittorrent", "transmission"]: - logger.info(f"{self.LOG_TAG}开始扫描下载器 {DOWNLOADER} ...") + for name, service in self.service_infos.items(): + logger.info(f"{self.LOG_TAG}开始扫描下载器 {name} ...") # 获取下载器中的种子 - downloader_obj = self._get_downloader(DOWNLOADER) + downloader_obj = service.instance if not downloader_obj: - logger.error(f"{self.LOG_TAG} 获取下载器失败 {DOWNLOADER}") + logger.error(f"{self.LOG_TAG} 获取下载器失败 {name}") continue torrents, error = downloader_obj.get_torrents() # 如果下载器获取种子发生错误 或 没有种子 则跳过 if error or not torrents: continue - logger.info(f"{self.LOG_TAG}按时间重新排序 {DOWNLOADER} 种子数:{len(torrents)}") + logger.info(f"{self.LOG_TAG}按时间重新排序 {name} 种子数:{len(torrents)}") # 按添加时间进行排序, 时间靠前的按大小和名称加入处理历史, 判定为原始种子, 其他为辅种 - torrents = self._torrents_sort(torrents=torrents, dl_type=DOWNLOADER) - logger.info(f"{self.LOG_TAG}下载器 {DOWNLOADER} 分析种子信息中 ...") + torrents = self._torrents_sort(torrents=torrents, dl_type=service.type) + logger.info(f"{self.LOG_TAG}下载器 {name} 分析种子信息中 ...") for torrent in torrents: try: if self._event.is_set(): @@ -214,14 +242,14 @@ class DownloadSiteTag(_PluginBase): f"{self.LOG_TAG}停止服务") return # 获取已处理种子的key (size, name) - _key = self._torrent_key(torrent=torrent, dl_type=DOWNLOADER) + _key = self._torrent_key(torrent=torrent, dl_type=service.type) # 获取种子hash - _hash = self._get_hash(torrent=torrent, dl_type=DOWNLOADER) + _hash = self._get_hash(torrent=torrent, dl_type=service.type) if not _hash: continue # 获取种子当前标签 - torrent_tags = self._get_label(torrent=torrent, dl_type=DOWNLOADER) - torrent_cat = self._get_category(torrent=torrent, dl_type=DOWNLOADER) + torrent_tags = self._get_label(torrent=torrent, dl_type=service.type) + torrent_cat = self._get_category(torrent=torrent, dl_type=service.type) # 提取种子hash对应的下载历史 history: DownloadHistory = self.downloadhistory_oper.get_by_hash(_hash) if not history: @@ -241,7 +269,7 @@ class DownloadSiteTag(_PluginBase): history.torrent_site = None # 如果站点名称为空, 尝试通过trackers识别 elif not history.torrent_site: - trackers = self._get_trackers(torrent=torrent, dl_type=DOWNLOADER) + trackers = self._get_trackers(torrent=torrent, dl_type=service.type) for tracker in trackers: # 检查tracker是否包含特定的关键字,并进行相应的映射 for key, mapped_domain in tracker_mappings.items(): @@ -267,7 +295,7 @@ class DownloadSiteTag(_PluginBase): if self._enabled_media_tag and history.title: _tags.append(history.title) # 分类, 如果勾选开关的话