feat 下载时记录文件清单

This commit is contained in:
jxxghp 2023-08-31 08:37:00 +08:00
parent f80e5739ca
commit 593211c037
8 changed files with 69 additions and 27 deletions

View File

@ -104,9 +104,12 @@ class DownloadChain(ChainBase):
_folder_name = "" _folder_name = ""
if not torrent_file: 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: if not torrent_file:
return return
else:
# 获取种子文件的文件夹名和文件清单
_folder_name, _file_list = self.torrent.get_torrent_info(torrent_file)
# 下载目录 # 下载目录
if not save_path: if not save_path:
if settings.DOWNLOAD_CATEGORY and _media and _media.category: if settings.DOWNLOAD_CATEGORY and _media and _media.category:
@ -173,6 +176,17 @@ class DownloadChain(ChainBase):
torrent_description=_torrent.description, torrent_description=_torrent.description,
torrent_site=_torrent.site_name 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) self.post_download_message(meta=_meta, mediainfo=_media, torrent=_torrent, channel=channel)
# 下载成功后处理 # 下载成功后处理

View File

@ -1,8 +1,8 @@
from pathlib import Path from pathlib import Path
from typing import Any from typing import List
from app.db import DbOper from app.db import DbOper
from app.db.models.downloadhistory import DownloadHistory from app.db.models.downloadhistory import DownloadHistory, DownloadFiles
class DownloadHistoryOper(DbOper): 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 :param path: 数据key
""" """
return DownloadHistory.get_by_path(self._db, str(path)) 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查询下载记录 按Hash查询下载记录
:param download_hash: 数据key :param download_hash: 数据key
""" """
return DownloadHistory.get_by_hash(self._db, download_hash) return DownloadHistory.get_by_hash(self._db, download_hash)
def add(self, **kwargs): def add(self, **kwargs) -> DownloadHistory:
""" """
新增下载历史 新增下载历史
""" """
downloadhistory = DownloadHistory(**kwargs) downloadhistory = DownloadHistory(**kwargs)
return downloadhistory.create(self._db) 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) DownloadHistory.truncate(self._db)
def get_last_by(self, mtype=None, title: str = None, year: str = None, 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]:
""" """
按类型标题年份季集查询下载记录 按类型标题年份季集查询下载记录
""" """

View File

@ -116,9 +116,9 @@ class DownloadFiles(Base):
return db.query(DownloadFiles).filter(DownloadFiles.download_hash == download_hash).all() return db.query(DownloadFiles).filter(DownloadFiles.download_hash == download_hash).all()
@staticmethod @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() return db.query(DownloadFiles).filter(DownloadFiles.fullpath == fullpath).first()
@staticmethod @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() return db.query(DownloadFiles).filter(DownloadFiles.savepath == savepath).all()

View File

@ -2,7 +2,6 @@ import json
from typing import Any from typing import Any
from app.db import DbOper from app.db import DbOper
from app.db.models import Base
from app.db.models.plugin import PluginData from app.db.models.plugin import PluginData
from app.utils.object import ObjectUtils 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 :param plugin_id: 插件id

View File

@ -19,7 +19,7 @@ class SiteOper(DbOper):
return True, "新增站点成功" return True, "新增站点成功"
return False, "站点已存在" 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) 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:
""" """
更新站点 更新站点
""" """

View File

@ -32,7 +32,7 @@ class SubscribeOper(DbOper):
else: else:
return subscribe.id, "订阅已存在" 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) Subscribe.delete(self._db, rid=sid)
def update(self, sid: int, payload: dict): def update(self, sid: int, payload: dict) -> Subscribe:
""" """
更新订阅 更新订阅
""" """

View File

@ -48,7 +48,7 @@ class SystemConfigOper(DbOper, metaclass=Singleton):
conf = SystemConfig(key=key, value=value) conf = SystemConfig(key=key, value=value)
conf.create(self._db) conf.create(self._db)
def get(self, key: Union[str, SystemConfigKey] = None): def get(self, key: Union[str, SystemConfigKey] = None) -> Any:
""" """
获取系统设置 获取系统设置
""" """

View File

@ -1,5 +1,5 @@
import time import time
from typing import Any from typing import Any, List
from app.db import DbOper from app.db import DbOper
from app.db.models.transferhistory import TransferHistory 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 :param historyid: 转移历史id
""" """
return TransferHistory.get(self._db, historyid) 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 :param title: 数据key
""" """
return TransferHistory.list_by_title(self._db, title) 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 :param src: 数据key
""" """
return TransferHistory.get_by_src(self._db, src) 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) return TransferHistory(**kwargs).create(self._db)
def statistic(self, days: int = 7): def statistic(self, days: int = 7) -> List[Any]:
""" """
统计最近days天的下载历史数量 统计最近days天的下载历史数量
""" """
return TransferHistory.statistic(self._db, days) return TransferHistory.statistic(self._db, days)
def get_by(self, title: str = None, year: str = None, 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) TransferHistory.truncate(self._db)
def add_force(self, **kwargs): def add_force(self, **kwargs) -> TransferHistory:
""" """
新增转移历史相同源目录的记录会被删除 新增转移历史相同源目录的记录会被删除
""" """