fix 定时清理媒体库,增加username字段

This commit is contained in:
honue 2023-10-27 15:12:10 +08:00
parent d8fcb4d240
commit 1ff571eb46
9 changed files with 240 additions and 198 deletions

View File

@ -170,7 +170,8 @@ class DownloadChain(ChainBase):
episodes: Set[int] = None, episodes: Set[int] = None,
channel: MessageChannel = None, channel: MessageChannel = None,
save_path: str = None, save_path: str = None,
userid: Union[str, int] = None) -> Optional[str]: userid: Union[str, int] = None,
username: str = None) -> Optional[str]:
""" """
下载及发送通知 下载及发送通知
:param context: 资源上下文 :param context: 资源上下文
@ -179,6 +180,7 @@ class DownloadChain(ChainBase):
:param channel: 通知渠道 :param channel: 通知渠道
:param save_path: 保存路径 :param save_path: 保存路径
:param userid: 用户ID :param userid: 用户ID
:param username: 调用下载的用户名/插件名
""" """
_torrent = context.torrent_info _torrent = context.torrent_info
_media = context.media_info _media = context.media_info
@ -267,6 +269,7 @@ class DownloadChain(ChainBase):
torrent_description=_torrent.description, torrent_description=_torrent.description,
torrent_site=_torrent.site_name, torrent_site=_torrent.site_name,
userid=userid, userid=userid,
username=username,
channel=channel.value if channel else None, channel=channel.value if channel else None,
date=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) date=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
) )
@ -321,7 +324,8 @@ class DownloadChain(ChainBase):
no_exists: Dict[int, Dict[int, NotExistMediaInfo]] = None, no_exists: Dict[int, Dict[int, NotExistMediaInfo]] = None,
save_path: str = None, save_path: str = None,
channel: MessageChannel = None, channel: MessageChannel = None,
userid: str = None) -> Tuple[List[Context], Dict[int, Dict[int, NotExistMediaInfo]]]: userid: str = None,
username: str = None) -> Tuple[List[Context], Dict[int, Dict[int, NotExistMediaInfo]]]:
""" """
根据缺失数据自动种子列表中组合择优下载 根据缺失数据自动种子列表中组合择优下载
:param contexts: 资源上下文列表 :param contexts: 资源上下文列表
@ -329,6 +333,7 @@ class DownloadChain(ChainBase):
:param save_path: 保存路径 :param save_path: 保存路径
:param channel: 通知渠道 :param channel: 通知渠道
:param userid: 用户ID :param userid: 用户ID
:param username: 调用下载的用户名/插件名
:return: 已经下载的资源列表剩余未下载到的剧集 no_exists[tmdb_id] = {season: NotExistMediaInfo} :return: 已经下载的资源列表剩余未下载到的剧集 no_exists[tmdb_id] = {season: NotExistMediaInfo}
""" """
# 已下载的项目 # 已下载的项目
@ -395,7 +400,7 @@ class DownloadChain(ChainBase):
for context in contexts: for context in contexts:
if context.media_info.type == MediaType.MOVIE: if context.media_info.type == MediaType.MOVIE:
if self.download_single(context, save_path=save_path, if self.download_single(context, save_path=save_path,
channel=channel, userid=userid): channel=channel, userid=userid, username=username):
# 下载成功 # 下载成功
downloaded_list.append(context) downloaded_list.append(context)
@ -463,12 +468,13 @@ class DownloadChain(ChainBase):
torrent_file=content if isinstance(content, Path) else None, torrent_file=content if isinstance(content, Path) else None,
save_path=save_path, save_path=save_path,
channel=channel, channel=channel,
userid=userid userid=userid,
username=username
) )
else: else:
# 下载 # 下载
download_id = self.download_single(context, save_path=save_path, download_id = self.download_single(context, save_path=save_path,
channel=channel, userid=userid) channel=channel, userid=userid, username=username)
if download_id: if download_id:
# 下载成功 # 下载成功
@ -528,7 +534,7 @@ class DownloadChain(ChainBase):
if torrent_episodes.issubset(set(need_episodes)): if torrent_episodes.issubset(set(need_episodes)):
# 下载 # 下载
download_id = self.download_single(context, save_path=save_path, download_id = self.download_single(context, save_path=save_path,
channel=channel, userid=userid) channel=channel, userid=userid, username=username)
if download_id: if download_id:
# 下载成功 # 下载成功
downloaded_list.append(context) downloaded_list.append(context)
@ -606,7 +612,8 @@ class DownloadChain(ChainBase):
episodes=selected_episodes, episodes=selected_episodes,
save_path=save_path, save_path=save_path,
channel=channel, channel=channel,
userid=userid userid=userid,
username=username
) )
if not download_id: if not download_id:
continue continue

View File

@ -189,7 +189,7 @@ class MessageChain(ChainBase):
# 下载种子 # 下载种子
context: Context = cache_list[int(text) - 1] context: Context = cache_list[int(text) - 1]
# 下载 # 下载
self.downloadchain.download_single(context, userid=userid, channel=channel) self.downloadchain.download_single(context, userid=userid, channel=channel, username=username)
elif text.lower() == "p": elif text.lower() == "p":
# 上一页 # 上一页
@ -343,7 +343,8 @@ class MessageChain(ChainBase):
downloads, lefts = self.downloadchain.batch_download(contexts=cache_list, downloads, lefts = self.downloadchain.batch_download(contexts=cache_list,
no_exists=no_exists, no_exists=no_exists,
channel=channel, channel=channel,
userid=userid) userid=userid,
username=username)
if downloads and not lefts: if downloads and not lefts:
# 全部下载完成 # 全部下载完成
logger.info(f'{_current_media.title_year} 下载完成') logger.info(f'{_current_media.title_year} 下载完成')

View File

@ -302,7 +302,7 @@ class SubscribeChain(ChainBase):
# 自动下载 # 自动下载
downloads, lefts = self.downloadchain.batch_download(contexts=matched_contexts, downloads, lefts = self.downloadchain.batch_download(contexts=matched_contexts,
no_exists=no_exists) no_exists=no_exists, username=subscribe.username)
# 更新已经下载的集数 # 更新已经下载的集数
if downloads \ if downloads \
and meta.type == MediaType.TV \ and meta.type == MediaType.TV \
@ -621,7 +621,8 @@ class SubscribeChain(ChainBase):
logger.info(f'{mediainfo.title_year} 匹配完成,共匹配到{len(_match_context)}个资源') logger.info(f'{mediainfo.title_year} 匹配完成,共匹配到{len(_match_context)}个资源')
if _match_context: if _match_context:
# 批量择优下载 # 批量择优下载
downloads, lefts = self.downloadchain.batch_download(contexts=_match_context, no_exists=no_exists) downloads, lefts = self.downloadchain.batch_download(contexts=_match_context, no_exists=no_exists,
username=subscribe.username)
# 更新已经下载的集数 # 更新已经下载的集数
if downloads and meta.type == MediaType.TV: if downloads and meta.type == MediaType.TV:
self.__update_subscribe_note(subscribe=subscribe, downloads=downloads) self.__update_subscribe_note(subscribe=subscribe, downloads=downloads)

View File

@ -108,10 +108,10 @@ class DownloadHistoryOper(DbOper):
episode=episode, episode=episode,
tmdbid=tmdbid) tmdbid=tmdbid)
def list_by_user_date(self, date: str, userid: str = None) -> List[DownloadHistory]: def list_by_user_date(self, date: str, username: str = None) -> List[DownloadHistory]:
""" """
查询某用户某时间之的下载历史 查询某用户某时间之的下载历史
""" """
return DownloadHistory.list_by_user_date(db=self._db, return DownloadHistory.list_by_user_date(db=self._db,
date=date, date=date,
userid=userid) username=username)

View File

@ -38,6 +38,8 @@ class DownloadHistory(Base):
torrent_site = Column(String) torrent_site = Column(String)
# 下载用户 # 下载用户
userid = Column(String) userid = Column(String)
# 下载用户名/插件名
username = Column(String)
# 下载渠道 # 下载渠道
channel = Column(String) channel = Column(String)
# 创建时间 # 创建时间
@ -108,13 +110,13 @@ class DownloadHistory(Base):
@staticmethod @staticmethod
@db_query @db_query
def list_by_user_date(db: Session, date: str, userid: str = None): def list_by_user_date(db: Session, date: str, username: str = None):
""" """
查询某用户某时间之后的下载历史 查询某用户某时间之后的下载历史
""" """
if userid: if username:
result = db.query(DownloadHistory).filter(DownloadHistory.date < date, result = db.query(DownloadHistory).filter(DownloadHistory.date < date,
DownloadHistory.userid == userid).order_by( DownloadHistory.username == username).order_by(
DownloadHistory.id.desc()).all() DownloadHistory.id.desc()).all()
else: else:
result = db.query(DownloadHistory).filter(DownloadHistory.date < date).order_by( result = db.query(DownloadHistory).filter(DownloadHistory.date < date).order_by(
@ -165,7 +167,6 @@ class DownloadFiles(Base):
result = db.query(DownloadFiles).filter(DownloadFiles.savepath == savepath).all() result = db.query(DownloadFiles).filter(DownloadFiles.savepath == savepath).all()
return list(result) return list(result)
@staticmethod
@db_update @db_update
def delete_by_fullpath(db: Session, fullpath: str): def delete_by_fullpath(db: Session, fullpath: str):
db.query(DownloadFiles).filter(DownloadFiles.fullpath == fullpath, db.query(DownloadFiles).filter(DownloadFiles.fullpath == fullpath,

View File

@ -120,26 +120,26 @@ class AutoClean(_PluginBase):
days_ago = current_time - timedelta(days=int(self._cleandate)) days_ago = current_time - timedelta(days=int(self._cleandate))
clean_date = days_ago.strftime("%Y-%m-%d") clean_date = days_ago.strftime("%Y-%m-%d")
# 查询用户清理日期之后的下载历史 # 查询用户清理日期之前的下载历史,不填默认清理全部用户的下载
if not self._cleanuser: if not self._cleanuser:
downloadhis_list = self._downloadhis.list_by_user_date(date=clean_date) downloadhis_list = self._downloadhis.list_by_user_date(date=clean_date)
logger.info(f'获取到日期 {clean_date}的下载历史 {len(downloadhis_list)}') logger.info(f'获取到日期 {clean_date}的下载历史 {len(downloadhis_list)}')
self.__clean_history(date=clean_date, downloadhis_list=downloadhis_list) self.__clean_history(date=clean_date, downloadhis_list=downloadhis_list)
else: else:
for userid in str(self._cleanuser).split(","): for username in str(self._cleanuser).split(","):
downloadhis_list = self._downloadhis.list_by_user_date(date=clean_date, downloadhis_list = self._downloadhis.list_by_user_date(date=clean_date,
userid=userid) username=username)
logger.info( logger.info(
f'获取到用户 {userid} 日期 {clean_date} 之后的下载历史 {len(downloadhis_list)}') f'获取到用户 {username} 日期 {clean_date} 之前的下载历史 {len(downloadhis_list)}')
self.__clean_history(date=clean_date, downloadhis_list=downloadhis_list, userid=userid) self.__clean_history(date=clean_date, downloadhis_list=downloadhis_list)
def __clean_history(self, date: str, downloadhis_list: List[DownloadHistory], userid: str = None): def __clean_history(self, date: str, downloadhis_list: List[DownloadHistory]):
""" """
清理下载历史转移记录 清理下载历史转移记录
""" """
if not downloadhis_list: if not downloadhis_list:
logger.warn(f"未获取到日期 {date}的下载记录,停止运行") logger.warn(f"未获取到日期 {date}的下载记录,停止运行")
return return
# 读取历史记录 # 读取历史记录
@ -159,10 +159,10 @@ class AutoClean(_PluginBase):
# 输出分组结果 # 输出分组结果
for key, downloadhis_list in downloadhis_grouped_dict.items(): for key, downloadhis_list in downloadhis_grouped_dict.items():
logger.info(f"开始清理 {key}") logger.info(f"开始清理 {key}")
result = []
del_transferhis_cnt = 0 del_transferhis_cnt = 0
del_media_name = downloadhis_list[0].title del_media_name = downloadhis_list[0].title
del_media_user = downloadhis_list[0].userid del_media_user = downloadhis_list[0].username
del_media_type = downloadhis_list[0].type del_media_type = downloadhis_list[0].type
del_media_year = downloadhis_list[0].year del_media_year = downloadhis_list[0].year
del_media_season = downloadhis_list[0].seasons del_media_season = downloadhis_list[0].seasons
@ -198,6 +198,7 @@ class AutoClean(_PluginBase):
# 累加删除数量 # 累加删除数量
del_transferhis_cnt += len(transferhis_list) del_transferhis_cnt += len(transferhis_list)
if del_transferhis_cnt:
# 发送消息 # 发送消息
if self._notify: if self._notify:
self.post_message( self.post_message(
@ -205,10 +206,9 @@ class AutoClean(_PluginBase):
title="【定时清理媒体库任务完成】", title="【定时清理媒体库任务完成】",
text=f"清理媒体名称 {del_media_name}\n" text=f"清理媒体名称 {del_media_name}\n"
f"下载媒体用户 {del_media_user}\n" f"下载媒体用户 {del_media_user}\n"
f"删除历史记录 {del_transferhis_cnt}", f"删除历史记录 {del_transferhis_cnt}")
userid=userid)
history.append({ result.append({
"type": del_media_type, "type": del_media_type,
"title": del_media_name, "title": del_media_name,
"year": del_media_year, "year": del_media_year,
@ -219,7 +219,7 @@ class AutoClean(_PluginBase):
}) })
# 保存历史 # 保存历史
self.save_data("history", history) self.save_data("history", result)
def get_state(self) -> bool: def get_state(self) -> bool:
return self._enabled return self._enabled
@ -365,8 +365,8 @@ class AutoClean(_PluginBase):
'component': 'VTextField', 'component': 'VTextField',
'props': { 'props': {
'model': 'cleanuser', 'model': 'cleanuser',
'label': '清理下载用户', 'label': '清理下载用户名/插件名',
'placeholder': '多个用户,分割' 'placeholder': '多个用户,分割 支持按插件清理下载记录豆瓣想看、豆瓣榜单、RSS订阅'
} }
} }
] ]

View File

@ -506,7 +506,8 @@ class DoubanSync(_PluginBase):
action = "subscribe" action = "subscribe"
else: else:
# 自动下载 # 自动下载
downloads, lefts = self.downloadchain.batch_download(contexts=contexts, no_exists=no_exists) downloads, lefts = self.downloadchain.batch_download(contexts=contexts, no_exists=no_exists,
username="豆瓣想看")
if downloads and not lefts: if downloads and not lefts:
# 全部下载完成 # 全部下载完成
logger.info(f'{mediainfo.title_year} 下载完成') logger.info(f'{mediainfo.title_year} 下载完成')

View File

@ -628,7 +628,8 @@ class RssSubscribe(_PluginBase):
media_info=mediainfo, media_info=mediainfo,
torrent_info=torrentinfo, torrent_info=torrentinfo,
), ),
save_path=self._save_path save_path=self._save_path,
username="RSS订阅"
) )
if not result: if not result:
logger.error(f'{title} 下载失败') logger.error(f'{title} 下载失败')

View File

@ -0,0 +1,30 @@
"""1.0.11
Revision ID: 06abf3e7090b
Revises: d633ca6cd572
Create Date: 2023-10-27 12:22:56.213376
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '06abf3e7090b'
down_revision = 'd633ca6cd572'
branch_labels = None
depends_on = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
try:
with op.batch_alter_table("downloadhistory") as batch_op:
batch_op.add_column(sa.Column('username', sa.String, nullable=True))
except Exception as e:
pass
# ### end Alembic commands ###
def downgrade() -> None:
pass