diff --git a/app/chain/__init__.py b/app/chain/__init__.py index 8d0266a8..58111cd9 100644 --- a/app/chain/__init__.py +++ b/app/chain/__init__.py @@ -319,7 +319,7 @@ class ChainBase(metaclass=ABCMeta): def torrent_files(self, tid: str) -> Optional[Union[TorrentFilesList, List[File]]]: """ - 根据种子文件,选择并添加下载任务 + 获取种子文件 :param tid: 种子Hash :return: 种子文件 """ diff --git a/app/db/downloadhistory_oper.py b/app/db/downloadhistory_oper.py index 1e02ff31..c26766cc 100644 --- a/app/db/downloadhistory_oper.py +++ b/app/db/downloadhistory_oper.py @@ -1,5 +1,5 @@ from pathlib import Path -from typing import Any +from typing import Any, List from app.db import DbOper from app.db.models.downloadhistory import DownloadHistory @@ -44,7 +44,7 @@ class DownloadHistoryOper(DbOper): DownloadHistory.truncate(self._db) def get_last_by(self, mtype=None, title: str = None, year: str = None, - season: str = None, episode: str = None, tmdbid=None) -> DownloadHistory: + season: str = None, episode: str = None, tmdbid=None): """ 按类型、标题、年份、季集查询下载记录 """ diff --git a/app/db/models/downloadhistory.py b/app/db/models/downloadhistory.py index 9d472350..0c39bc80 100644 --- a/app/db/models/downloadhistory.py +++ b/app/db/models/downloadhistory.py @@ -58,29 +58,29 @@ class DownloadHistory(Base): """ if tmdbid and not season and not episode: return db.query(DownloadHistory).filter(DownloadHistory.tmdbid == tmdbid).order_by( - DownloadHistory.id.desc()).first() + DownloadHistory.id.desc()).all() if tmdbid and season and not episode: return db.query(DownloadHistory).filter(DownloadHistory.tmdbid == tmdbid, DownloadHistory.seasons == season).order_by( - DownloadHistory.id.desc()).first() + DownloadHistory.id.desc()).all() if tmdbid and season and episode: return db.query(DownloadHistory).filter(DownloadHistory.tmdbid == tmdbid, DownloadHistory.seasons == season, DownloadHistory.episodes == episode).order_by( - DownloadHistory.id.desc()).first() + DownloadHistory.id.desc()).all() # 电视剧所有季集|电影 if not season and not episode: return db.query(DownloadHistory).filter(DownloadHistory.type == mtype, DownloadHistory.title == title, DownloadHistory.year == year).order_by( - DownloadHistory.id.desc()).first() + DownloadHistory.id.desc()).all() # 电视剧某季 if season and not episode: return db.query(DownloadHistory).filter(DownloadHistory.type == mtype, DownloadHistory.title == title, DownloadHistory.year == year, DownloadHistory.seasons == season).order_by( - DownloadHistory.id.desc()).first() + DownloadHistory.id.desc()).all() # 电视剧某季某集 if season and episode: return db.query(DownloadHistory).filter(DownloadHistory.type == mtype, @@ -88,4 +88,4 @@ class DownloadHistory(Base): DownloadHistory.year == year, DownloadHistory.seasons == season, DownloadHistory.episodes == episode).order_by( - DownloadHistory.id.desc()).first() + DownloadHistory.id.desc()).all() diff --git a/app/plugins/dirmonitor/__init__.py b/app/plugins/dirmonitor/__init__.py index 9ab4be31..239e7908 100644 --- a/app/plugins/dirmonitor/__init__.py +++ b/app/plugins/dirmonitor/__init__.py @@ -1,3 +1,4 @@ +import os import re import threading import time @@ -16,6 +17,8 @@ from app.core.metainfo import MetaInfo from app.db.downloadhistory_oper import DownloadHistoryOper from app.db.transferhistory_oper import TransferHistoryOper from app.log import logger +from app.modules.qbittorrent import Qbittorrent +from app.modules.transmission import Transmission from app.plugins import _PluginBase from app.schemas import Notification, NotificationType, TransferInfo from app.schemas.types import EventType @@ -82,6 +85,8 @@ class DirMonitor(_PluginBase): _exclude_keywords = "" # 存储源目录与目的目录关系 _dirconf: Dict[str, Path] = {} + qb = None + tr = None def init_plugin(self, config: dict = None): self.transferhis = TransferHistoryOper() @@ -104,6 +109,9 @@ class DirMonitor(_PluginBase): self.stop_service() if self._enabled: + self.qb = Qbittorrent() + self.tr = Transmission() + # 启动任务 monitor_dirs = self._monitor_dirs.split("\n") if not monitor_dirs: @@ -255,12 +263,10 @@ class DirMonitor(_PluginBase): return # 获取downloadhash - downloadHis = self.downloadhis.get_last_by(mtype=mediainfo.type.value, - title=mediainfo.title, - year=mediainfo.year, - season=file_meta.season, - episode=file_meta.episode, - tmdbid=mediainfo.tmdb_id) + download_hash = self.get_download_hash(file_name=os.path.basename(event_path), + mediainfo=mediainfo, + file_meta=file_meta) + # 新增转移成功历史记录 self.transferhis.add_force( src=event_path, @@ -277,7 +283,7 @@ class DirMonitor(_PluginBase): seasons=file_meta.season, episodes=file_meta.episode, image=mediainfo.get_poster_image(), - download_hash=downloadHis.download_hash if downloadHis else None, + download_hash=download_hash, status=1, date=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) ) @@ -300,6 +306,37 @@ class DirMonitor(_PluginBase): except Exception as e: logger.error("目录监控发生错误:%s - %s" % (str(e), traceback.format_exc())) + def get_download_hash(self, file_name, mediainfo, file_meta): + """ + 获取download_hash + """ + downloadHis = self.downloadhis.get_last_by(mtype=mediainfo.type.value, + title=mediainfo.title, + year=mediainfo.year, + season=file_meta.season, + episode=file_meta.episode, + tmdbid=mediainfo.tmdb_id) + if downloadHis: + for his in downloadHis: + # qb + if settings.DOWNLOADER == "qbittorrent": + files = self.qb.get_files(tid=his.download_hash) + if files: + for file in files: + torrent_file_name = file.get("name") + if str(file_name) == str(os.path.basename(torrent_file_name)): + return his.download_hash + # tr + if settings.DOWNLOADER == "transmission": + files = self.tr.get_files(tid=his.download_hash) + if files: + for file in files: + torrent_file_name = file.name + if str(file_name) == str(os.path.basename(torrent_file_name)): + return his.download_hash + + return None + def get_state(self) -> bool: return self._enabled