mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-04 23:17:20 +08:00
229 lines
7.9 KiB
Python
229 lines
7.9 KiB
Python
from typing import Dict, List, Optional, cast
|
|
|
|
from sqlalchemy import delete as sqlalchemy_delete, update as sqlalchemy_update
|
|
|
|
from app.db.base import DbOper
|
|
from app.db.models.downloadhistory import DownloadHistory, DownloadFiles
|
|
from app.schemas.types import MediaSource
|
|
|
|
|
|
class DownloadHistoryOper(DbOper):
|
|
"""
|
|
下载历史管理
|
|
"""
|
|
|
|
def get_by_path(self, path: str) -> Optional[DownloadHistory]:
|
|
"""
|
|
按路径查询下载记录
|
|
:param path: 数据key
|
|
"""
|
|
return DownloadHistory.get_by_path(self._db, path)
|
|
|
|
def get_by_hash(self, download_hash: str) -> Optional[DownloadHistory]:
|
|
"""
|
|
按Hash查询下载记录
|
|
:param download_hash: 数据key
|
|
"""
|
|
return DownloadHistory.get_by_hash(self._db, download_hash)
|
|
|
|
def get_by_hashes(self, download_hashes: List[str]) -> Dict[str, DownloadHistory]:
|
|
"""
|
|
批量按 Hash 查询下载记录,并返回以 Hash 为键的映射。
|
|
"""
|
|
histories = DownloadHistory.get_by_hashes(self._db, download_hashes)
|
|
return {
|
|
history.download_hash: history
|
|
for history in histories
|
|
if history and history.download_hash
|
|
}
|
|
|
|
def get_by_media_identity(
|
|
self, media_source: MediaSource, media_id: str,
|
|
music_type: Optional[str] = None,
|
|
) -> List[DownloadHistory]:
|
|
"""
|
|
按规范媒体身份查询下载记录。
|
|
:param media_source: 媒体数据源
|
|
:param media_id: 数据源原生 ID
|
|
:param music_type: 音乐实体类型
|
|
"""
|
|
return DownloadHistory.get_by_media_identity(
|
|
self._db,
|
|
media_source=media_source,
|
|
media_id=media_id,
|
|
music_type=music_type,
|
|
)
|
|
|
|
def add(self, **kwargs):
|
|
"""
|
|
新增下载历史
|
|
"""
|
|
DownloadHistory(**kwargs).create(self._db)
|
|
|
|
def add_files(self, file_items: List[dict]):
|
|
"""
|
|
新增下载历史文件
|
|
"""
|
|
for file_item in file_items:
|
|
downloadfile = DownloadFiles(**file_item)
|
|
downloadfile.create(self._db)
|
|
|
|
def truncate_files(self):
|
|
"""
|
|
清空下载历史文件记录
|
|
"""
|
|
DownloadFiles.truncate(self._db)
|
|
|
|
def get_files_by_hash(self, download_hash: str, state: Optional[int] = None) -> List[DownloadFiles]:
|
|
"""
|
|
按Hash查询下载文件记录
|
|
:param download_hash: 数据key
|
|
:param state: 删除状态
|
|
"""
|
|
return DownloadFiles.get_by_hash(self._db, download_hash, state)
|
|
|
|
def get_file_by_fullpath(self, fullpath: str) -> Optional[DownloadFiles]:
|
|
"""
|
|
按fullpath查询下载文件记录
|
|
:param fullpath: 数据key
|
|
"""
|
|
return cast(Optional[DownloadFiles],
|
|
DownloadFiles.get_by_fullpath(self._db, fullpath=fullpath, all_files=False))
|
|
|
|
def get_files_by_fullpath(self, fullpath: str) -> List[DownloadFiles]:
|
|
"""
|
|
按fullpath查询下载文件记录
|
|
:param fullpath: 数据key
|
|
"""
|
|
return cast(List[DownloadFiles],
|
|
DownloadFiles.get_by_fullpath(self._db, fullpath=fullpath, all_files=True))
|
|
|
|
def get_files_by_savepath(self, fullpath: str) -> List[DownloadFiles]:
|
|
"""
|
|
按savepath查询下载文件记录
|
|
:param fullpath: 数据key
|
|
"""
|
|
return DownloadFiles.get_by_savepath(self._db, fullpath)
|
|
|
|
def delete_file_by_fullpath(self, fullpath: str):
|
|
"""
|
|
按fullpath删除下载文件记录
|
|
:param fullpath: 数据key
|
|
"""
|
|
DownloadFiles.delete_by_fullpath(self._db, fullpath)
|
|
|
|
def stage_delete_file_by_fullpath(self, fullpath: str) -> None:
|
|
"""暂存指定完整路径的下载文件记录删除。"""
|
|
self._db.execute(
|
|
sqlalchemy_update(DownloadFiles)
|
|
.where(
|
|
DownloadFiles.fullpath == fullpath,
|
|
DownloadFiles.state == 1,
|
|
)
|
|
.values(state=0)
|
|
)
|
|
|
|
def get_hash_by_fullpath(self, fullpath: str) -> Optional[str]:
|
|
"""
|
|
按fullpath查询下载文件记录hash
|
|
:param fullpath: 数据key
|
|
"""
|
|
fileinfo = cast(Optional[DownloadFiles],
|
|
DownloadFiles.get_by_fullpath(self._db, fullpath=fullpath, all_files=False))
|
|
if fileinfo:
|
|
return fileinfo.download_hash
|
|
return ""
|
|
|
|
def list_by_page(self, page: int = 1, count: int = 30) -> List[DownloadHistory]:
|
|
"""
|
|
分页查询下载历史
|
|
"""
|
|
return DownloadHistory.list_by_page(self._db, page, count)
|
|
|
|
async def async_list_by_page(
|
|
self,
|
|
page: int = 1,
|
|
count: int = 30,
|
|
) -> List[DownloadHistory]:
|
|
"""异步分页查询下载历史。"""
|
|
return await DownloadHistory.async_list_by_page(self._db, page, count)
|
|
|
|
async def async_delete_history(self, historyid: int):
|
|
"""
|
|
异步删除下载记录。
|
|
"""
|
|
await DownloadHistory.async_delete(self._db, historyid)
|
|
|
|
def truncate(self):
|
|
"""
|
|
清空下载记录
|
|
"""
|
|
DownloadHistory.truncate(self._db)
|
|
|
|
def get_last_by(self, mtype=None, title: Optional[str] = None, year: Optional[str] = None,
|
|
season: Optional[str] = None, episode: Optional[str] = None,
|
|
media_source: Optional[MediaSource] = None,
|
|
media_id: Optional[str] = None) -> List[DownloadHistory]:
|
|
"""
|
|
按类型、标题、年份、季集查询下载记录
|
|
媒体身份 + mtype 或 title + year
|
|
"""
|
|
return DownloadHistory.get_last_by(db=self._db,
|
|
mtype=mtype,
|
|
title=title,
|
|
year=year,
|
|
season=season,
|
|
episode=episode,
|
|
media_source=media_source,
|
|
media_id=media_id)
|
|
|
|
def list_by_user_date(self, date: str, username: Optional[str] = None) -> List[DownloadHistory]:
|
|
"""
|
|
查询某用户某时间之前的下载历史
|
|
"""
|
|
return DownloadHistory.list_by_user_date(db=self._db,
|
|
date=date,
|
|
username=username)
|
|
|
|
def list_by_date(
|
|
self, date: str, type: str, media_source: MediaSource, media_id: str,
|
|
seasons: Optional[str] = None,
|
|
) -> List[DownloadHistory]:
|
|
"""
|
|
查询某时间之后的下载历史
|
|
"""
|
|
return DownloadHistory.list_by_date(db=self._db,
|
|
date=date,
|
|
type=type,
|
|
media_source=media_source,
|
|
media_id=media_id,
|
|
seasons=seasons)
|
|
|
|
def list_by_type(self, mtype: str, days: int = 7) -> List[DownloadHistory]:
|
|
"""
|
|
获取指定类型的下载历史
|
|
"""
|
|
return DownloadHistory.list_by_type(db=self._db,
|
|
mtype=mtype,
|
|
days=days)
|
|
|
|
def delete_history(self, historyid):
|
|
"""
|
|
删除下载记录
|
|
"""
|
|
DownloadHistory.delete(self._db, historyid)
|
|
|
|
def stage_delete_history(self, historyid: int) -> None:
|
|
"""暂存下载记录删除,不由模型装饰器提交事务。"""
|
|
self._db.execute(
|
|
sqlalchemy_delete(DownloadHistory).where(
|
|
DownloadHistory.id == historyid
|
|
)
|
|
)
|
|
|
|
def delete_downloadfile(self, downloadfileid):
|
|
"""
|
|
删除下载文件记录
|
|
"""
|
|
DownloadFiles.delete(self._db, downloadfileid)
|