From dc612ec6f1a1dd41897a541df24496c0357c99d5 Mon Sep 17 00:00:00 2001 From: amtoaer Date: Sun, 26 Nov 2023 12:51:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8A=A0=E5=85=A5=20recheck=20?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=9C=A8=E6=9C=AC=E5=9C=B0=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=A2=AB=E5=88=A0=E9=99=A4=E5=90=8E=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E6=A0=87=E8=AE=B0=E4=B8=BA=E6=9C=AA=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- commands.py | 38 ++++++++++++++++++++++++++++++++++++++ entry.py | 6 ++++++ 2 files changed, 44 insertions(+) create mode 100644 commands.py diff --git a/commands.py b/commands.py new file mode 100644 index 0000000..7beb617 --- /dev/null +++ b/commands.py @@ -0,0 +1,38 @@ +import asyncio + +from aiofiles.os import path +from loguru import logger + +from constants import MediaStatus, MediaType +from models import FavoriteItem + + +async def recheck(): + """刷新数据库中视频的状态,如果发现文件不存在则标记未下载,以便在下次任务重新下载,在自己手动删除文件后调用""" + items = await FavoriteItem.filter( + type=MediaType.VIDEO, + status=MediaStatus.NORMAL, + downloaded=True, + ) + exists = await asyncio.gather( + *[path.exists(item.video_path) for item in items] + ) + for item, exist in zip(items, exists): + if isinstance(exist, Exception): + logger.error( + "Error when checking file {} {}: {}", + item.bvid, + item.name, + exist, + ) + continue + if not exist: + logger.info( + "File {} {} not exists, mark as not downloaded.", + item.bvid, + item.name, + ) + item.downloaded = False + logger.info("Updating database...") + await FavoriteItem.bulk_update(items, fields=["downloaded"]) + logger.info("Database updated.") diff --git a/entry.py b/entry.py index 4815de2..4abf665 100644 --- a/entry.py +++ b/entry.py @@ -4,6 +4,7 @@ import sys import uvloop from loguru import logger +from commands import recheck from models import init_model from processor import cleanup, process from settings import settings @@ -18,6 +19,11 @@ async def entry() -> None: logger.info("Running once...") await process() return + if any("recheck" in _ for _ in sys.argv): + # 重新检查 + logger.info("Rechecking...") + await recheck() + return logger.info("Running daemon...") while True: await process()