diff --git a/app/chain/download.py b/app/chain/download.py index c93f8424..fb6b7ca3 100644 --- a/app/chain/download.py +++ b/app/chain/download.py @@ -104,9 +104,12 @@ class DownloadChain(ChainBase): _folder_name = "" if not torrent_file: # 下载种子文件 - torrent_file, _folder_name, _ = self.download_torrent(_torrent, userid=userid) + torrent_file, _folder_name, _file_list = self.download_torrent(_torrent, userid=userid) if not torrent_file: return + else: + # 获取种子文件的文件夹名和文件清单 + _folder_name, _file_list = self.torrent.get_torrent_info(torrent_file) # 下载目录 if not save_path: if settings.DOWNLOAD_CATEGORY and _media and _media.category: @@ -173,6 +176,17 @@ class DownloadChain(ChainBase): torrent_description=_torrent.description, torrent_site=_torrent.site_name ) + # 登记下载文件 + self.downloadhis.add_files([ + { + "download_hash": _hash, + "downloader": settings.DOWNLOADER, + "fullpath": download_dir / _folder_name / file, + "savepath": str(download_dir / _folder_name), + "filepath": file, + "torrentname": _meta.org_string, + } for file in _file_list if file + ]) # 发送消息 self.post_download_message(meta=_meta, mediainfo=_media, torrent=_torrent, channel=channel) # 下载成功后处理 diff --git a/app/db/downloadhistory_oper.py b/app/db/downloadhistory_oper.py index b97d010d..ecf040df 100644 --- a/app/db/downloadhistory_oper.py +++ b/app/db/downloadhistory_oper.py @@ -1,8 +1,8 @@ from pathlib import Path -from typing import Any +from typing import List from app.db import DbOper -from app.db.models.downloadhistory import DownloadHistory +from app.db.models.downloadhistory import DownloadHistory, DownloadFiles class DownloadHistoryOper(DbOper): @@ -10,28 +10,57 @@ class DownloadHistoryOper(DbOper): 下载历史管理 """ - def get_by_path(self, path: Path) -> Any: + def get_by_path(self, path: Path) -> DownloadHistory: """ 按路径查询下载记录 :param path: 数据key """ return DownloadHistory.get_by_path(self._db, str(path)) - def get_by_hash(self, download_hash: str) -> Any: + def get_by_hash(self, download_hash: str) -> DownloadHistory: """ 按Hash查询下载记录 :param download_hash: 数据key """ return DownloadHistory.get_by_hash(self._db, download_hash) - def add(self, **kwargs): + def add(self, **kwargs) -> DownloadHistory: """ 新增下载历史 """ downloadhistory = DownloadHistory(**kwargs) return downloadhistory.create(self._db) - def list_by_page(self, page: int = 1, count: int = 30): + def add_files(self, file_items: List[dict]): + """ + 新增下载历史文件 + """ + for file_item in file_items: + downloadfile = DownloadFiles(**file_item) + downloadfile.create(self._db) + + def get_files_by_hash(self, download_hash: str) -> List[DownloadFiles]: + """ + 按Hash查询下载文件记录 + :param download_hash: 数据key + """ + return DownloadFiles.get_by_hash(self._db, download_hash) + + def get_file_by_fullpath(self, fullpath: str) -> DownloadFiles: + """ + 按fullpath查询下载文件记录 + :param fullpath: 数据key + """ + return DownloadFiles.get_by_fullpath(self._db, fullpath) + + def get_files_by_savepath(self, fullpath: str) -> List[DownloadFiles]: + """ + 按savepath查询下载文件记录 + :param fullpath: 数据key + """ + return DownloadFiles.get_by_savepath(self._db, fullpath) + + def list_by_page(self, page: int = 1, count: int = 30) -> List[DownloadHistory]: """ 分页查询下载历史 """ @@ -44,7 +73,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): + season: str = None, episode: str = None, tmdbid=None) -> List[DownloadHistory]: """ 按类型、标题、年份、季集查询下载记录 """ diff --git a/app/db/models/downloadhistory.py b/app/db/models/downloadhistory.py index cf406bf1..12066669 100644 --- a/app/db/models/downloadhistory.py +++ b/app/db/models/downloadhistory.py @@ -116,9 +116,9 @@ class DownloadFiles(Base): return db.query(DownloadFiles).filter(DownloadFiles.download_hash == download_hash).all() @staticmethod - def get_by_full_path(db: Session, fullpath: str): + def get_by_fullpath(db: Session, fullpath: str): return db.query(DownloadFiles).filter(DownloadFiles.fullpath == fullpath).first() @staticmethod - def get_by_save_path(db: Session, savepath: str): + def get_by_savepath(db: Session, savepath: str): return db.query(DownloadFiles).filter(DownloadFiles.savepath == savepath).all() diff --git a/app/db/plugindata_oper.py b/app/db/plugindata_oper.py index 99fbae26..4f2c29c0 100644 --- a/app/db/plugindata_oper.py +++ b/app/db/plugindata_oper.py @@ -2,7 +2,6 @@ import json from typing import Any from app.db import DbOper -from app.db.models import Base from app.db.models.plugin import PluginData from app.utils.object import ObjectUtils @@ -12,7 +11,7 @@ class PluginDataOper(DbOper): 插件数据管理 """ - def save(self, plugin_id: str, key: str, value: Any) -> Base: + def save(self, plugin_id: str, key: str, value: Any) -> PluginData: """ 保存插件数据 :param plugin_id: 插件id diff --git a/app/db/site_oper.py b/app/db/site_oper.py index 67d6a122..f332dc8b 100644 --- a/app/db/site_oper.py +++ b/app/db/site_oper.py @@ -19,7 +19,7 @@ class SiteOper(DbOper): return True, "新增站点成功" return False, "站点已存在" - def get(self, sid: int): + def get(self, sid: int) -> Site: """ 查询单个站点 """ @@ -31,7 +31,7 @@ class SiteOper(DbOper): """ return Site.list(self._db) - def list_active(self): + def list_active(self) -> List[Site]: """ 按状态获取站点列表 """ @@ -41,9 +41,9 @@ class SiteOper(DbOper): """ 删除站点 """ - return Site.delete(self._db, sid) + Site.delete(self._db, sid) - def update(self, sid: int, payload: dict): + def update(self, sid: int, payload: dict) -> Site: """ 更新站点 """ diff --git a/app/db/subscribe_oper.py b/app/db/subscribe_oper.py index 8263c7b9..ff13f6e5 100644 --- a/app/db/subscribe_oper.py +++ b/app/db/subscribe_oper.py @@ -32,7 +32,7 @@ class SubscribeOper(DbOper): else: return subscribe.id, "订阅已存在" - def exists(self, tmdbid: int, season: int): + def exists(self, tmdbid: int, season: int) -> bool: """ 判断是否存在 """ @@ -61,7 +61,7 @@ class SubscribeOper(DbOper): """ Subscribe.delete(self._db, rid=sid) - def update(self, sid: int, payload: dict): + def update(self, sid: int, payload: dict) -> Subscribe: """ 更新订阅 """ diff --git a/app/db/systemconfig_oper.py b/app/db/systemconfig_oper.py index aaeade0a..204b3e02 100644 --- a/app/db/systemconfig_oper.py +++ b/app/db/systemconfig_oper.py @@ -48,7 +48,7 @@ class SystemConfigOper(DbOper, metaclass=Singleton): conf = SystemConfig(key=key, value=value) conf.create(self._db) - def get(self, key: Union[str, SystemConfigKey] = None): + def get(self, key: Union[str, SystemConfigKey] = None) -> Any: """ 获取系统设置 """ diff --git a/app/db/transferhistory_oper.py b/app/db/transferhistory_oper.py index 6c0774ae..a9df4de1 100644 --- a/app/db/transferhistory_oper.py +++ b/app/db/transferhistory_oper.py @@ -1,5 +1,5 @@ import time -from typing import Any +from typing import Any, List from app.db import DbOper from app.db.models.transferhistory import TransferHistory @@ -10,28 +10,28 @@ class TransferHistoryOper(DbOper): 转移历史管理 """ - def get(self, historyid: int) -> Any: + def get(self, historyid: int) -> TransferHistory: """ 获取转移历史 :param historyid: 转移历史id """ return TransferHistory.get(self._db, historyid) - def get_by_title(self, title: str) -> Any: + def get_by_title(self, title: str) -> List[TransferHistory]: """ 按标题查询转移记录 :param title: 数据key """ return TransferHistory.list_by_title(self._db, title) - def get_by_src(self, src: str) -> Any: + def get_by_src(self, src: str) -> TransferHistory: """ 按源查询转移记录 :param src: 数据key """ return TransferHistory.get_by_src(self._db, src) - def add(self, **kwargs): + def add(self, **kwargs) -> TransferHistory: """ 新增转移历史 """ @@ -40,14 +40,14 @@ class TransferHistoryOper(DbOper): }) return TransferHistory(**kwargs).create(self._db) - def statistic(self, days: int = 7): + def statistic(self, days: int = 7) -> List[Any]: """ 统计最近days天的下载历史数量 """ return TransferHistory.statistic(self._db, days) def get_by(self, title: str = None, year: str = None, - season: str = None, episode: str = None, tmdbid: str = None) -> Any: + season: str = None, episode: str = None, tmdbid: str = None) -> List[TransferHistory]: """ 按类型、标题、年份、季集查询转移记录 """ @@ -70,7 +70,7 @@ class TransferHistoryOper(DbOper): """ TransferHistory.truncate(self._db) - def add_force(self, **kwargs): + def add_force(self, **kwargs) -> TransferHistory: """ 新增转移历史,相同源目录的记录会被删除 """