From df0ba221f9a9e334fb2eafac4edb1c62f225224c Mon Sep 17 00:00:00 2001 From: jxxghp Date: Mon, 19 Jun 2023 16:17:46 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E8=BD=AC=E7=A7=BB=E5=8E=86=E5=8F=B2?= =?UTF-8?q?=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/chain/transfer.py | 23 +++++++++++++++++++++- app/db/downloadhistory_oper.py | 2 +- app/db/models/transferhistory.py | 33 ++++++++++++++++++++++++++++++++ app/db/transferhistory_oper.py | 24 +++++++++++++++++++++++ 4 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 app/db/models/transferhistory.py create mode 100644 app/db/transferhistory_oper.py diff --git a/app/chain/transfer.py b/app/chain/transfer.py index b8433237..cee79172 100644 --- a/app/chain/transfer.py +++ b/app/chain/transfer.py @@ -3,11 +3,13 @@ from pathlib import Path from typing import List, Optional, Union from app.chain import ChainBase +from app.core.config import settings from app.core.context import MediaInfo from app.core.meta import MetaBase from app.core.metainfo import MetaInfo from app.db.downloadhistory_oper import DownloadHistoryOper from app.db.models.downloadhistory import DownloadHistory +from app.db.transferhistory_oper import TransferHistoryOper from app.log import logger from app.schemas import TransferInfo, TransferTorrent from app.schemas.types import TorrentStatus, EventType, MediaType @@ -22,6 +24,7 @@ class TransferChain(ChainBase): def __init__(self): super().__init__() self.downloadhis = DownloadHistoryOper() + self.transferhis = TransferHistoryOper() def process(self, arg_str: str = None, userid: Union[str, int] = None) -> bool: """ @@ -99,7 +102,7 @@ class TransferChain(ChainBase): # 更新媒体图片 self.obtain_image(mediainfo=mediainfo) # 转移 - transferinfo: TransferInfo = self.transfer(mediainfo=mediainfo, path=Path(torrent.path)) + transferinfo: TransferInfo = self.transfer(mediainfo=mediainfo, path=torrent.path) if not transferinfo or not transferinfo.target_path: # 转移失败 logger.warn(f"{torrent.title} 入库失败") @@ -110,6 +113,24 @@ class TransferChain(ChainBase): userid=userid ), continue + # 新增转移历史记录 + self.transferhis.add( + src=str(torrent.path), + dest=str(transferinfo.target_path), + mode=settings.TRANSFER_TYPE, + type=mediainfo.type.value, + category=mediainfo.category, + title=mediainfo.title, + year=mediainfo.year, + tmdbid=mediainfo.tmdb_id, + imdbid=mediainfo.imdb_id, + tvdbid=mediainfo.tvdb_id, + doubanid=mediainfo.douban_id, + seasons=meta.season, + episodes=meta.episode, + image=mediainfo.get_poster_image(), + download_hash=torrent.hash + ) # 转移完成 self.transfer_completed(hashs=torrent.hash, transinfo=transferinfo) # 刮剥 diff --git a/app/db/downloadhistory_oper.py b/app/db/downloadhistory_oper.py index 27a75aeb..8008cd4f 100644 --- a/app/db/downloadhistory_oper.py +++ b/app/db/downloadhistory_oper.py @@ -7,7 +7,7 @@ from app.db.models.downloadhistory import DownloadHistory class DownloadHistoryOper(DbOper): """ - 插件数据管理 + 下载历史管理 """ def get_by_path(self, path: Path) -> Any: diff --git a/app/db/models/transferhistory.py b/app/db/models/transferhistory.py new file mode 100644 index 00000000..f025b936 --- /dev/null +++ b/app/db/models/transferhistory.py @@ -0,0 +1,33 @@ +import time + +from sqlalchemy import Column, Integer, String, Sequence +from sqlalchemy.orm import Session + +from app.db.models import Base + + +class TransferHistory(Base): + """ + 转移历史记录 + """ + id = Column(Integer, Sequence('id'), primary_key=True, index=True) + src = Column(String, index=True) + dest = Column(String) + mode = Column(String) + type = Column(String) + category = Column(String) + title = Column(String, index=True) + year = Column(String) + tmdbid = Column(Integer) + imdbid = Column(String) + tvdbid = Column(Integer) + doubanid = Column(String) + seasons = Column(Integer) + episodes = Column(String) + image = Column(String) + download_hash = Column(String) + date = Column(String, index=True, default=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) + + @staticmethod + def search_by_title(db: Session, title: str): + return db.query(TransferHistory).filter(TransferHistory.title == title).all() diff --git a/app/db/transferhistory_oper.py b/app/db/transferhistory_oper.py new file mode 100644 index 00000000..a45deb68 --- /dev/null +++ b/app/db/transferhistory_oper.py @@ -0,0 +1,24 @@ +from typing import Any + +from app.db import DbOper +from app.db.models.transferhistory import TransferHistory + + +class TransferHistoryOper(DbOper): + """ + 转移历史管理 + """ + + def get_by_title(self, title: str) -> Any: + """ + 按标题查询转移记录 + :param title: 数据key + """ + return TransferHistory.search_by_title(self._db, title) + + def add(self, **kwargs): + """ + 新增转移历史 + """ + transferhistory = TransferHistory(**kwargs) + return transferhistory.create(self._db)