fix 同步删除插件

This commit is contained in:
thsrite 2023-08-31 14:37:10 +08:00
parent 8739a67679
commit 053c89bf9f
3 changed files with 119 additions and 127 deletions

View File

@ -39,12 +39,13 @@ class DownloadHistoryOper(DbOper):
downloadfile = DownloadFiles(**file_item)
downloadfile.create(self._db)
def get_files_by_hash(self, download_hash: str) -> List[DownloadFiles]:
def get_files_by_hash(self, download_hash: str, state: int = None) -> List[DownloadFiles]:
"""
按Hash查询下载文件记录
:param download_hash: 数据key
:param state: 删除状态
"""
return DownloadFiles.get_by_hash(self._db, download_hash)
return DownloadFiles.get_by_hash(self._db, download_hash, state)
def get_file_by_fullpath(self, fullpath: str) -> DownloadFiles:
"""
@ -60,6 +61,13 @@ class DownloadHistoryOper(DbOper):
"""
return DownloadFiles.get_by_savepath(self._db, fullpath)
def delete_file_by_fullpath(self, fullpath: str):
"""
按fullpath删除下载文件记录
:param fullpath: 数据key
"""
DownloadFiles.delete_file_by_fullpath(self._db, fullpath)
def list_by_page(self, page: int = 1, count: int = 30) -> List[DownloadHistory]:
"""
分页查询下载历史

View File

@ -112,13 +112,27 @@ class DownloadFiles(Base):
state = Column(Integer, nullable=False, default=1)
@staticmethod
def get_by_hash(db: Session, download_hash: str):
def get_by_hash(db: Session, download_hash: str, state: int = None):
if state:
return db.query(DownloadFiles).filter(DownloadFiles.download_hash == download_hash,
DownloadFiles.state == state).all()
else:
return db.query(DownloadFiles).filter(DownloadFiles.download_hash == download_hash).all()
@staticmethod
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).order_by(
DownloadHistory.id.desc()).first()
@staticmethod
def get_by_savepath(db: Session, savepath: str):
return db.query(DownloadFiles).filter(DownloadFiles.savepath == savepath).all()
@staticmethod
def delete_by_fullpath(db: Session, fullpath: str):
return db.query(DownloadFiles).filter(DownloadFiles.fullpath == fullpath,
DownloadFiles.state == 1).update(
{
"state": 0
}
)

View File

@ -12,6 +12,7 @@ from apscheduler.triggers.cron import CronTrigger
from app.core.config import settings
from app.core.event import eventmanager, Event
from app.db.downloadhistory_oper import DownloadHistoryOper
from app.db.models.transferhistory import TransferHistory
from app.db.transferhistory_oper import TransferHistoryOper
from app.log import logger
@ -57,11 +58,13 @@ class MediaSyncDel(_PluginBase):
_del_source = False
_exclude_path = None
_transferhis = None
_downloadhis = None
qb = None
tr = None
def init_plugin(self, config: dict = None):
self._transferhis = TransferHistoryOper(self.db)
self._downloadhis = DownloadHistoryOper(self.db)
self.episode = Episode()
self.qb = Qbittorrent()
self.tr = Transmission()
@ -518,13 +521,16 @@ class MediaSyncDel(_PluginBase):
year = None
del_cnt = 0
stop_cnt = 0
error_cnt = 0
for transferhis in transfer_history:
image = transferhis.image
year = transferhis.year
# 删除种子任务
if self._del_source:
# 0、删除转移记录
self._transferhis.delete(transferhis.id)
# 删除种子任务
if self._del_source:
# 1、直接删除源文件
if transferhis.src and Path(transferhis.src).suffix in settings.RMT_MEDIAEXT:
source_name = os.path.basename(transferhis.src)
@ -534,11 +540,14 @@ class MediaSyncDel(_PluginBase):
if transferhis.download_hash:
try:
# 2、判断种子是否被删除完
delete_flag, stop_flag = self.handle_torrent(src=source_path,
delete_flag, success_flag = self.handle_torrent(src=transferhis.src,
torrent_hash=transferhis.download_hash)
if not success_flag:
error_cnt += 1
else:
if delete_flag:
del_cnt += 1
if stop_flag:
else:
stop_cnt += 1
except Exception as e:
logger.error("删除种子失败,尝试删除源文件:%s" % str(e))
@ -560,7 +569,9 @@ class MediaSyncDel(_PluginBase):
title="媒体库同步删除任务完成",
image=image,
text=f"{msg}\n"
f"数量 删除{del_cnt}个 暂停{stop_cnt}\n"
f"删除{del_cnt}\n"
f"暂停{stop_cnt}\n"
f"错误{error_cnt}\n"
f"时间 {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))}"
)
@ -668,13 +679,14 @@ class MediaSyncDel(_PluginBase):
image = 'https://emby.media/notificationicon.png'
del_cnt = 0
stop_cnt = 0
error_cnt = 0
for transferhis in transfer_history:
image = transferhis.image
self._transferhis.delete(transferhis.id)
# 删除种子任务
if self._del_source:
# 0、删除转移记录
self._transferhis.delete(transferhis.id)
# 删除种子任务
if self._del_source:
# 1、直接删除源文件
if transferhis.src and Path(transferhis.src).suffix in settings.RMT_MEDIAEXT:
source_name = os.path.basename(transferhis.src)
@ -684,11 +696,14 @@ class MediaSyncDel(_PluginBase):
if transferhis.download_hash:
try:
# 2、判断种子是否被删除完
delete_flag, stop_flag = self.handle_torrent(src=source_path,
delete_flag, success_flag = self.handle_torrent(src=transferhis.src,
torrent_hash=transferhis.download_hash)
if not success_flag:
error_cnt += 1
else:
if delete_flag:
del_cnt += 1
if stop_flag:
else:
stop_cnt += 1
except Exception as e:
logger.error("删除种子失败,尝试删除源文件:%s" % str(e))
@ -701,7 +716,9 @@ class MediaSyncDel(_PluginBase):
mtype=NotificationType.MediaServer,
title="媒体库同步删除任务完成",
text=f"{msg}\n"
f"数量 删除{del_cnt}个 暂停{stop_cnt}\n"
f"删除{del_cnt}\n"
f"暂停{stop_cnt}\n"
f"错误{error_cnt}\n"
f"时间 {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))}",
image=image)
@ -735,124 +752,77 @@ class MediaSyncDel(_PluginBase):
plugin_id=plugin_id)
logger.info(f"查询到 {history_key} 转种历史 {transfer_history}")
# 删除历史标志
del_history = False
# 删除种子标志
try:
# 删除本次种子记录
self._downloadhis.delete_file_by_fullpath(fullpath=src)
# 根据种子hash查询剩余未删除的记录
downloadHisNoDel = self._downloadhis.get_files_by_hash(download_hash=torrent_hash, state=1)
if downloadHisNoDel and len(downloadHisNoDel) > 0:
logger.info(
f"查询种子任务 {torrent_hash} 存在 {len(downloadHisNoDel)} 个未删除文件,执行暂停种子操作")
delete_flag = False
else:
logger.info(
f"查询种子任务 {torrent_hash} 文件已全部删除,执行删除种子操作")
delete_flag = True
# 是否需要暂停源下载器种子
stop_flag = False
# 如果有转种记录,则删除转种后的下载任务
if transfer_history and isinstance(transfer_history, dict):
download = transfer_history['to_download']
download_id = transfer_history['to_download_id']
delete_source = transfer_history['delete_source']
del_history = True
# 删除种子
if delete_flag:
# 删除转种记录
self.del_data(key=history_key, plugin_id=plugin_id)
# 转种后未删除源种时,同步删除源种
if not delete_source:
logger.info(f"{history_key} 转种时未删除源下载任务,开始删除源下载任务…")
try:
dl_files = self.chain.torrent_files(tid=torrent_hash)
if not dl_files:
logger.info(f"未获取到 {settings.DOWNLOADER} - {torrent_hash} 种子文件,种子已被删除")
else:
for dl_file in dl_files:
dl_file_name = dl_file.get("name")
torrent_file = os.path.join(src, os.path.basename(dl_file_name))
if Path(torrent_file).exists():
logger.warn(f"种子有文件被删除,种子文件{torrent_file}暂未删除,暂停种子")
delete_flag = False
stop_flag = True
break
if delete_flag:
logger.info(f"删除下载任务:{settings.DOWNLOADER} - {torrent_hash}")
# 删除源种子
logger.info(f"删除源下载器下载任务:{settings.DOWNLOADER} - {torrent_hash}")
self.chain.remove_torrents(torrent_hash)
except Exception as e:
logger.error(f"删除源下载任务 {history_key} 失败: {str(e)}")
# 如果是False则说明种子文件没有完全被删除暂停种子暂不处理
if delete_flag:
try:
# 转种download
# 删除转种后任务
logger.info(f"删除转种后下载任务:{download} - {download_id}")
# 删除转种后下载任务
if download == "transmission":
dl_files = self.tr.get_files(tid=download_id)
if not dl_files:
logger.info(f"未获取到 {download} - {download_id} 种子文件,种子已被删除")
else:
for dl_file in dl_files:
dl_file_name = dl_file.name
if not transfer_history or not stop_flag:
torrent_file = os.path.join(src, os.path.basename(dl_file_name))
if Path(torrent_file).exists():
logger.info(f"种子有文件被删除,种子文件{torrent_file}暂未删除,暂停种子")
delete_flag = False
stop_flag = True
break
if delete_flag:
# 删除源下载任务或转种后下载任务
logger.info(f"删除下载任务:{download} - {download_id}")
self.tr.delete_torrents(delete_file=True,
ids=download_id)
# 删除转种记录
if del_history:
self.del_data(key=history_key, plugin_id=plugin_id)
# 处理辅种
self.__del_seed(download=download, download_id=download_id, action_flag="del")
else:
dl_files = self.qb.get_files(tid=download_id)
if not dl_files:
logger.info(f"未获取到 {download} - {download_id} 种子文件,种子已被删除")
else:
for dl_file in dl_files:
dl_file_name = dl_file.get("name")
if not transfer_history or not stop_flag:
torrent_file = os.path.join(src, os.path.basename(dl_file_name))
if Path(torrent_file).exists():
logger.info(f"种子有文件被删除,种子文件{torrent_file}暂未删除,暂停种子")
delete_flag = False
stop_flag = True
break
if delete_flag:
# 删除源下载任务或转种后下载任务
logger.info(f"删除下载任务:{download} - {download_id}")
self.qb.delete_torrents(delete_file=True,
ids=download_id)
else:
# 暂停种子
# 转种后未删除源种时,同步暂停源种
if not delete_source:
logger.info(f"{history_key} 转种时未删除源下载任务,开始暂停源下载任务…")
# 删除转种记录
if del_history:
self.del_data(key=history_key, plugin_id=plugin_id)
# 暂停源种子
logger.info(f"暂停源下载器下载任务:{settings.DOWNLOADER} - {torrent_hash}")
self.chain.stop_torrents(torrent_hash)
else:
# 未转种de情况
if delete_flag:
# 删除源种子
logger.info(f"删除源下载器下载任务:{download} - {download_id}")
self.chain.remove_torrents(download_id)
else:
# 暂停源种子
logger.info(f"暂停源下载器下载任务:{download} - {download_id}")
self.chain.stop_torrents(download_id)
# 处理辅种
self.__del_seed(download=download, download_id=download_id, action_flag="del")
self.__del_seed(download=download, download_id=download_id, action_flag="del" if delete_flag else 'stop')
return delete_flag, True
except Exception as e:
logger.error(f"删除转种辅种下载任务失败: {str(e)}")
# 判断是否暂停
if not delete_flag:
logger.error("开始暂停种子")
# 暂停种子
if stop_flag:
# 暂停源种
self.chain.stop_torrents(torrent_hash)
logger.info(f"种子:{settings.DOWNLOADER} - {torrent_hash} 暂停")
# 暂停转种
if del_history:
if download == "qbittorrent":
self.qb.stop_torrents(download_id)
logger.info(f"转种:{download} - {download_id} 暂停")
else:
self.tr.stop_torrents(download_id)
logger.info(f"转种:{download} - {download_id} 暂停")
# 暂停辅种
self.__del_seed(download=download, download_id=download_id, action_flag="stop")
return delete_flag, stop_flag
logger.error(f"删种失败: {e}")
return False, False
def __del_seed(self, download, download_id, action_flag):
"""