fix 同步删除插件
This commit is contained in:
parent
8739a67679
commit
053c89bf9f
@ -39,12 +39,13 @@ class DownloadHistoryOper(DbOper):
|
|||||||
downloadfile = DownloadFiles(**file_item)
|
downloadfile = DownloadFiles(**file_item)
|
||||||
downloadfile.create(self._db)
|
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查询下载文件记录
|
按Hash查询下载文件记录
|
||||||
:param download_hash: 数据key
|
: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:
|
def get_file_by_fullpath(self, fullpath: str) -> DownloadFiles:
|
||||||
"""
|
"""
|
||||||
@ -60,6 +61,13 @@ class DownloadHistoryOper(DbOper):
|
|||||||
"""
|
"""
|
||||||
return DownloadFiles.get_by_savepath(self._db, fullpath)
|
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]:
|
def list_by_page(self, page: int = 1, count: int = 30) -> List[DownloadHistory]:
|
||||||
"""
|
"""
|
||||||
分页查询下载历史
|
分页查询下载历史
|
||||||
|
@ -112,13 +112,27 @@ class DownloadFiles(Base):
|
|||||||
state = Column(Integer, nullable=False, default=1)
|
state = Column(Integer, nullable=False, default=1)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_by_hash(db: Session, download_hash: str):
|
def get_by_hash(db: Session, download_hash: str, state: int = None):
|
||||||
return db.query(DownloadFiles).filter(DownloadFiles.download_hash == download_hash).all()
|
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
|
@staticmethod
|
||||||
def get_by_fullpath(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).order_by(
|
||||||
|
DownloadHistory.id.desc()).first()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_by_savepath(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()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def delete_by_fullpath(db: Session, fullpath: str):
|
||||||
|
return db.query(DownloadFiles).filter(DownloadFiles.fullpath == fullpath,
|
||||||
|
DownloadFiles.state == 1).update(
|
||||||
|
{
|
||||||
|
"state": 0
|
||||||
|
}
|
||||||
|
)
|
||||||
|
@ -12,6 +12,7 @@ from apscheduler.triggers.cron import CronTrigger
|
|||||||
|
|
||||||
from app.core.config import settings
|
from app.core.config import settings
|
||||||
from app.core.event import eventmanager, Event
|
from app.core.event import eventmanager, Event
|
||||||
|
from app.db.downloadhistory_oper import DownloadHistoryOper
|
||||||
from app.db.models.transferhistory import TransferHistory
|
from app.db.models.transferhistory import TransferHistory
|
||||||
from app.db.transferhistory_oper import TransferHistoryOper
|
from app.db.transferhistory_oper import TransferHistoryOper
|
||||||
from app.log import logger
|
from app.log import logger
|
||||||
@ -57,11 +58,13 @@ class MediaSyncDel(_PluginBase):
|
|||||||
_del_source = False
|
_del_source = False
|
||||||
_exclude_path = None
|
_exclude_path = None
|
||||||
_transferhis = None
|
_transferhis = None
|
||||||
|
_downloadhis = None
|
||||||
qb = None
|
qb = None
|
||||||
tr = None
|
tr = None
|
||||||
|
|
||||||
def init_plugin(self, config: dict = None):
|
def init_plugin(self, config: dict = None):
|
||||||
self._transferhis = TransferHistoryOper(self.db)
|
self._transferhis = TransferHistoryOper(self.db)
|
||||||
|
self._downloadhis = DownloadHistoryOper(self.db)
|
||||||
self.episode = Episode()
|
self.episode = Episode()
|
||||||
self.qb = Qbittorrent()
|
self.qb = Qbittorrent()
|
||||||
self.tr = Transmission()
|
self.tr = Transmission()
|
||||||
@ -518,13 +521,16 @@ class MediaSyncDel(_PluginBase):
|
|||||||
year = None
|
year = None
|
||||||
del_cnt = 0
|
del_cnt = 0
|
||||||
stop_cnt = 0
|
stop_cnt = 0
|
||||||
|
error_cnt = 0
|
||||||
for transferhis in transfer_history:
|
for transferhis in transfer_history:
|
||||||
image = transferhis.image
|
image = transferhis.image
|
||||||
year = transferhis.year
|
year = transferhis.year
|
||||||
|
|
||||||
|
# 0、删除转移记录
|
||||||
|
self._transferhis.delete(transferhis.id)
|
||||||
|
|
||||||
# 删除种子任务
|
# 删除种子任务
|
||||||
if self._del_source:
|
if self._del_source:
|
||||||
# 0、删除转移记录
|
|
||||||
self._transferhis.delete(transferhis.id)
|
|
||||||
# 1、直接删除源文件
|
# 1、直接删除源文件
|
||||||
if transferhis.src and Path(transferhis.src).suffix in settings.RMT_MEDIAEXT:
|
if transferhis.src and Path(transferhis.src).suffix in settings.RMT_MEDIAEXT:
|
||||||
source_name = os.path.basename(transferhis.src)
|
source_name = os.path.basename(transferhis.src)
|
||||||
@ -534,12 +540,15 @@ class MediaSyncDel(_PluginBase):
|
|||||||
if transferhis.download_hash:
|
if transferhis.download_hash:
|
||||||
try:
|
try:
|
||||||
# 2、判断种子是否被删除完
|
# 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)
|
torrent_hash=transferhis.download_hash)
|
||||||
if delete_flag:
|
if not success_flag:
|
||||||
del_cnt += 1
|
error_cnt += 1
|
||||||
if stop_flag:
|
else:
|
||||||
stop_cnt += 1
|
if delete_flag:
|
||||||
|
del_cnt += 1
|
||||||
|
else:
|
||||||
|
stop_cnt += 1
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("删除种子失败,尝试删除源文件:%s" % str(e))
|
logger.error("删除种子失败,尝试删除源文件:%s" % str(e))
|
||||||
|
|
||||||
@ -560,7 +569,9 @@ class MediaSyncDel(_PluginBase):
|
|||||||
title="媒体库同步删除任务完成",
|
title="媒体库同步删除任务完成",
|
||||||
image=image,
|
image=image,
|
||||||
text=f"{msg}\n"
|
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()))}"
|
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'
|
image = 'https://emby.media/notificationicon.png'
|
||||||
del_cnt = 0
|
del_cnt = 0
|
||||||
stop_cnt = 0
|
stop_cnt = 0
|
||||||
|
error_cnt = 0
|
||||||
for transferhis in transfer_history:
|
for transferhis in transfer_history:
|
||||||
image = transferhis.image
|
image = transferhis.image
|
||||||
|
# 0、删除转移记录
|
||||||
self._transferhis.delete(transferhis.id)
|
self._transferhis.delete(transferhis.id)
|
||||||
|
|
||||||
# 删除种子任务
|
# 删除种子任务
|
||||||
if self._del_source:
|
if self._del_source:
|
||||||
# 0、删除转移记录
|
|
||||||
self._transferhis.delete(transferhis.id)
|
|
||||||
# 1、直接删除源文件
|
# 1、直接删除源文件
|
||||||
if transferhis.src and Path(transferhis.src).suffix in settings.RMT_MEDIAEXT:
|
if transferhis.src and Path(transferhis.src).suffix in settings.RMT_MEDIAEXT:
|
||||||
source_name = os.path.basename(transferhis.src)
|
source_name = os.path.basename(transferhis.src)
|
||||||
@ -684,12 +696,15 @@ class MediaSyncDel(_PluginBase):
|
|||||||
if transferhis.download_hash:
|
if transferhis.download_hash:
|
||||||
try:
|
try:
|
||||||
# 2、判断种子是否被删除完
|
# 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)
|
torrent_hash=transferhis.download_hash)
|
||||||
if delete_flag:
|
if not success_flag:
|
||||||
del_cnt += 1
|
error_cnt += 1
|
||||||
if stop_flag:
|
else:
|
||||||
stop_cnt += 1
|
if delete_flag:
|
||||||
|
del_cnt += 1
|
||||||
|
else:
|
||||||
|
stop_cnt += 1
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("删除种子失败,尝试删除源文件:%s" % str(e))
|
logger.error("删除种子失败,尝试删除源文件:%s" % str(e))
|
||||||
|
|
||||||
@ -701,7 +716,9 @@ class MediaSyncDel(_PluginBase):
|
|||||||
mtype=NotificationType.MediaServer,
|
mtype=NotificationType.MediaServer,
|
||||||
title="媒体库同步删除任务完成",
|
title="媒体库同步删除任务完成",
|
||||||
text=f"{msg}\n"
|
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()))}",
|
f"时间 {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))}",
|
||||||
image=image)
|
image=image)
|
||||||
|
|
||||||
@ -735,124 +752,77 @@ class MediaSyncDel(_PluginBase):
|
|||||||
plugin_id=plugin_id)
|
plugin_id=plugin_id)
|
||||||
logger.info(f"查询到 {history_key} 转种历史 {transfer_history}")
|
logger.info(f"查询到 {history_key} 转种历史 {transfer_history}")
|
||||||
|
|
||||||
# 删除历史标志
|
try:
|
||||||
del_history = False
|
# 删除本次种子记录
|
||||||
# 删除种子标志
|
self._downloadhis.delete_file_by_fullpath(fullpath=src)
|
||||||
delete_flag = True
|
|
||||||
# 是否需要暂停源下载器种子
|
|
||||||
stop_flag = False
|
|
||||||
|
|
||||||
# 如果有转种记录,则删除转种后的下载任务
|
# 根据种子hash查询剩余未删除的记录
|
||||||
if transfer_history and isinstance(transfer_history, dict):
|
downloadHisNoDel = self._downloadhis.get_files_by_hash(download_hash=torrent_hash, state=1)
|
||||||
download = transfer_history['to_download']
|
if downloadHisNoDel and len(downloadHisNoDel) > 0:
|
||||||
download_id = transfer_history['to_download_id']
|
logger.info(
|
||||||
delete_source = transfer_history['delete_source']
|
f"查询种子任务 {torrent_hash} 存在 {len(downloadHisNoDel)} 个未删除文件,执行暂停种子操作")
|
||||||
del_history = True
|
delete_flag = False
|
||||||
|
else:
|
||||||
|
logger.info(
|
||||||
|
f"查询种子任务 {torrent_hash} 文件已全部删除,执行删除种子操作")
|
||||||
|
delete_flag = True
|
||||||
|
|
||||||
# 转种后未删除源种时,同步删除源种
|
# 如果有转种记录,则删除转种后的下载任务
|
||||||
if not delete_source:
|
if transfer_history and isinstance(transfer_history, dict):
|
||||||
logger.info(f"{history_key} 转种时未删除源下载任务,开始删除源下载任务…")
|
download = transfer_history['to_download']
|
||||||
|
download_id = transfer_history['to_download_id']
|
||||||
|
delete_source = transfer_history['delete_source']
|
||||||
|
|
||||||
try:
|
# 删除种子
|
||||||
dl_files = self.chain.torrent_files(tid=torrent_hash)
|
if delete_flag:
|
||||||
if not dl_files:
|
# 删除转种记录
|
||||||
logger.info(f"未获取到 {settings.DOWNLOADER} - {torrent_hash} 种子文件,种子已被删除")
|
self.del_data(key=history_key, plugin_id=plugin_id)
|
||||||
else:
|
|
||||||
for dl_file in dl_files:
|
# 转种后未删除源种时,同步删除源种
|
||||||
dl_file_name = dl_file.get("name")
|
if not delete_source:
|
||||||
torrent_file = os.path.join(src, os.path.basename(dl_file_name))
|
logger.info(f"{history_key} 转种时未删除源下载任务,开始删除源下载任务…")
|
||||||
if Path(torrent_file).exists():
|
|
||||||
logger.warn(f"种子有文件被删除,种子文件{torrent_file}暂未删除,暂停种子")
|
# 删除源种子
|
||||||
delete_flag = False
|
logger.info(f"删除源下载器下载任务:{settings.DOWNLOADER} - {torrent_hash}")
|
||||||
stop_flag = True
|
|
||||||
break
|
|
||||||
if delete_flag:
|
|
||||||
logger.info(f"删除下载任务:{settings.DOWNLOADER} - {torrent_hash}")
|
|
||||||
self.chain.remove_torrents(torrent_hash)
|
self.chain.remove_torrents(torrent_hash)
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"删除源下载任务 {history_key} 失败: {str(e)}")
|
|
||||||
|
|
||||||
# 如果是False则说明种子文件没有完全被删除,暂停种子,暂不处理
|
# 删除转种后任务
|
||||||
if delete_flag:
|
logger.info(f"删除转种后下载任务:{download} - {download_id}")
|
||||||
try:
|
# 删除转种后下载任务
|
||||||
# 转种download
|
if download == "transmission":
|
||||||
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,
|
self.tr.delete_torrents(delete_file=True,
|
||||||
ids=download_id)
|
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:
|
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,
|
self.qb.delete_torrents(delete_file=True,
|
||||||
ids=download_id)
|
ids=download_id)
|
||||||
|
else:
|
||||||
|
# 暂停种子
|
||||||
|
# 转种后未删除源种时,同步暂停源种
|
||||||
|
if not delete_source:
|
||||||
|
logger.info(f"{history_key} 转种时未删除源下载任务,开始暂停源下载任务…")
|
||||||
|
|
||||||
# 删除转种记录
|
# 暂停源种子
|
||||||
if del_history:
|
logger.info(f"暂停源下载器下载任务:{settings.DOWNLOADER} - {torrent_hash}")
|
||||||
self.del_data(key=history_key, plugin_id=plugin_id)
|
self.chain.stop_torrents(torrent_hash)
|
||||||
|
|
||||||
# 处理辅种
|
else:
|
||||||
self.__del_seed(download=download, download_id=download_id, action_flag="del")
|
# 未转种de情况
|
||||||
except Exception as e:
|
if delete_flag:
|
||||||
logger.error(f"删除转种辅种下载任务失败: {str(e)}")
|
# 删除源种子
|
||||||
|
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)
|
||||||
|
|
||||||
# 判断是否暂停
|
# 处理辅种
|
||||||
if not delete_flag:
|
self.__del_seed(download=download, download_id=download_id, action_flag="del" if delete_flag else 'stop')
|
||||||
logger.error("开始暂停种子")
|
|
||||||
# 暂停种子
|
|
||||||
if stop_flag:
|
|
||||||
# 暂停源种
|
|
||||||
self.chain.stop_torrents(torrent_hash)
|
|
||||||
logger.info(f"种子:{settings.DOWNLOADER} - {torrent_hash} 暂停")
|
|
||||||
|
|
||||||
# 暂停转种
|
return delete_flag, True
|
||||||
if del_history:
|
except Exception as e:
|
||||||
if download == "qbittorrent":
|
logger.error(f"删种失败: {e}")
|
||||||
self.qb.stop_torrents(download_id)
|
return False, False
|
||||||
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
|
|
||||||
|
|
||||||
def __del_seed(self, download, download_id, action_flag):
|
def __del_seed(self, download, download_id, action_flag):
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user