Compare commits

...

4 Commits
1.0.2 ... 1.0.6

Author SHA1 Message Date
amtoaer
bbde9d6ba6 fix: 修复判断 2023-11-30 18:12:15 +08:00
amtoaer
e040ab2d75 chore: 尝试降低更新凭据频率,防止风控 2023-11-30 17:50:36 +08:00
amtoaer
ad977e41d4 fix: 修复异步函数没有 await 的问题 2023-11-28 13:55:12 +08:00
amtoaer
dc612ec6f1 feat: 加入 recheck 功能,在本地文件被删除后重新标记为未下载 2023-11-26 12:51:27 +08:00
3 changed files with 49 additions and 5 deletions

38
commands.py Normal file
View File

@@ -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.")

View File

@@ -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()

View File

@@ -19,6 +19,8 @@ from settings import settings
client = httpx.AsyncClient(headers=HEADERS)
anchor = datetime.date.today()
async def cleanup() -> None:
await client.aclose()
@@ -93,12 +95,10 @@ async def manage_model(medias: list[dict], fav_list: FavoriteList) -> None:
async def process() -> None:
global anchor
if not await credential.check_valid():
logger.error("Credential is invalid, skipped.")
return
if await credential.check_refresh():
if datetime.date.today() >= anchor and await credential.check_refresh():
try:
credential.refresh()
await credential.refresh()
anchor = datetime.date.today() + datetime.timedelta(days=1)
logger.info("Credential refreshed.")
except Exception:
logger.exception("Failed to refresh credential.")