feat: recheck 对分 p 视频做适配,为所有的数据库批量操作指定 batch_size

This commit is contained in:
amtoaer
2024-02-24 21:37:34 +08:00
parent c21da25c6f
commit ec5776a0ed
2 changed files with 29 additions and 10 deletions
+23 -8
View File
@@ -13,17 +13,32 @@ from utils import aexists, aremove
async def recheck(): async def recheck():
"""刷新数据库中视频的状态,如果发现文件不存在则标记未下载,以便在下次任务重新下载,在自己手动删除文件后调用""" """刷新数据库中视频的状态,如果发现文件不存在则标记未下载,以便在下次任务重新下载,在自己手动删除文件后调用"""
items = await FavoriteItem.filter(type=MediaType.VIDEO, status=MediaStatus.NORMAL, downloaded=True)
exists = await asyncio.gather(*[aexists(item.video_path) for item in items]) async def is_ok(item: FavoriteItem) -> bool:
for item, exist in zip(items, exists): if len(item.pages):
if isinstance(exist, Exception): # 多 p 视频全部存在才算存在
logger.error("Error when checking file {} {}: {}.", item.bvid, item.name, exist) return all(await asyncio.gather(*[aexists(page.video_path) for page in item.pages]))
return await aexists(item.video_path)
items = await FavoriteItem.filter(
type=MediaType.VIDEO, status=MediaStatus.NORMAL, downloaded=True
).prefetch_related("pages")
items_to_update = []
for item in items:
for page in item.pages:
# 疑似 tortoise 的 bugprefetch_related 不会更新反向引用的字段,这里手动更新一下
page.favorite_item = item
items_ok = await asyncio.gather(*[is_ok(item) for item in items], return_exceptions=True)
for item, ok in zip(items, items_ok):
if isinstance(ok, Exception):
logger.error("Error when checking file {} {}: {}.", item.bvid, item.name, ok)
continue continue
if not exist: if not ok:
logger.info("File {} {} not exists, mark as not downloaded.", item.bvid, item.name) logger.info("Lack of file detected for {} {}, mark as not downloaded.", item.bvid, item.name)
item.downloaded = False item.downloaded = False
items_to_update.append(item)
logger.info("Updating database...") logger.info("Updating database...")
await FavoriteItem.bulk_update(items, fields=["downloaded"]) await FavoriteItem.bulk_update(items_to_update, fields=["downloaded"], batch_size=300)
logger.info("Database updated.") logger.info("Database updated.")
+6 -2
View File
@@ -46,7 +46,7 @@ async def update_favorite_item(medias: list[dict], fav_list: FavoriteList) -> No
uppers = [ uppers = [
Upper(mid=media["upper"]["mid"], name=media["upper"]["name"], thumb=media["upper"]["face"]) for media in medias Upper(mid=media["upper"]["mid"], name=media["upper"]["name"], thumb=media["upper"]["face"]) for media in medias
] ]
await Upper.bulk_create(uppers, on_conflict=["mid"], update_fields=["name", "thumb"]) await Upper.bulk_create(uppers, on_conflict=["mid"], update_fields=["name", "thumb"], batch_size=300)
items = [ items = [
FavoriteItem( FavoriteItem(
name=media["title"], name=media["title"],
@@ -67,6 +67,7 @@ async def update_favorite_item(medias: list[dict], fav_list: FavoriteList) -> No
items, items,
on_conflict=["bvid", "favorite_list_id"], on_conflict=["bvid", "favorite_list_id"],
update_fields=["name", "type", "desc", "cover", "ctime", "pubtime", "fav_time"], update_fields=["name", "type", "desc", "cover", "ctime", "pubtime", "fav_time"],
batch_size=300,
) )
@@ -173,7 +174,10 @@ async def process_favorite_item(
single_page = True single_page = True
else: else:
pages = await FavoriteItemPage.bulk_create( pages = await FavoriteItemPage.bulk_create(
pages, on_conflict=["favorite_item_id", "page"], update_fields=["cid", "name", "image"] pages,
on_conflict=["favorite_item_id", "page"],
update_fields=["cid", "name", "image"],
batch_size=300,
) )
if process_nfo: if process_nfo:
try: try: