mirror of
https://github.com/amtoaer/bili-sync.git
synced 2026-09-05 07:28:04 +08:00
feat: 加入 recheck 功能,在本地文件被删除后重新标记为未下载
This commit is contained in:
+38
@@ -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.")
|
||||||
@@ -4,6 +4,7 @@ import sys
|
|||||||
import uvloop
|
import uvloop
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
|
from commands import recheck
|
||||||
from models import init_model
|
from models import init_model
|
||||||
from processor import cleanup, process
|
from processor import cleanup, process
|
||||||
from settings import settings
|
from settings import settings
|
||||||
@@ -18,6 +19,11 @@ async def entry() -> None:
|
|||||||
logger.info("Running once...")
|
logger.info("Running once...")
|
||||||
await process()
|
await process()
|
||||||
return
|
return
|
||||||
|
if any("recheck" in _ for _ in sys.argv):
|
||||||
|
# 重新检查
|
||||||
|
logger.info("Rechecking...")
|
||||||
|
await recheck()
|
||||||
|
return
|
||||||
logger.info("Running daemon...")
|
logger.info("Running daemon...")
|
||||||
while True:
|
while True:
|
||||||
await process()
|
await process()
|
||||||
|
|||||||
Reference in New Issue
Block a user