chore: 整理代码逻辑,留出下载字幕的入口

This commit is contained in:
amtoaer
2023-12-06 00:00:42 +08:00
parent 46d1810e7c
commit de6eaeb4a6
5 changed files with 104 additions and 50 deletions
+7 -1
View File
@@ -1,4 +1,4 @@
.PHONY: install fmt start-daemon start-once .PHONY: install fmt start-daemon start-once db-init db-migrate db-upgrade sync-conf
install: install:
@echo "Installing dependencies..." @echo "Installing dependencies..."
@@ -23,3 +23,9 @@ db-migrate:
db-upgrade: db-upgrade:
@poetry run aerich upgrade @poetry run aerich upgrade
sync-conf:
@echo "Syncing config..."
@cp ${CONFIG_SRC} ./config/
@cp ${DB_SRC} ./data/
@echo "Done."
+51 -41
View File
@@ -1,11 +1,14 @@
import asyncio import asyncio
import functools
from pathlib import Path
from typing import Callable
from loguru import logger from loguru import logger
from constants import MediaStatus, MediaType from constants import MediaStatus, MediaType
from models import FavoriteItem, Upper from models import FavoriteItem
from processor import download_content, process_video from processor import process_favorite_item
from utils import aexists, amakedirs, aremove from utils import aexists, aremove
async def recheck(): async def recheck():
@@ -37,52 +40,59 @@ async def recheck():
logger.info("Database updated.") logger.info("Database updated.")
async def upper_thumb(): async def _refresh_favorite_item_info(
"""将up主的头像批量写入数据库,从不支持up主头像的版本升级上来后需要手动调用一次""" path_getter: Callable[[FavoriteItem], list[Path]],
makedir_tasks = [] process_poster: bool = False,
other_tasks = [] process_video: bool = False,
for upper in await Upper.all(): process_nfo: bool = False,
if all( process_upper: bool = False,
await asyncio.gather( process_subtitle: bool = False,
aexists(upper.thumb_path), aexists(upper.meta_path) ):
) items = await FavoriteItem.filter(downloaded=True).prefetch_related("upper")
):
logger.info(
"Upper {} {} already exists, skipped.", upper.mid, upper.name
)
makedir_tasks.append(amakedirs(upper.thumb_path.parent, exist_ok=True))
logger.info("Saving metadata for upper {} {}...", upper.mid, upper.name)
other_tasks.extend(
[
upper.save_metadata(),
download_content(upper.thumb, upper.thumb_path),
]
)
await asyncio.gather(*makedir_tasks)
await asyncio.gather(*other_tasks)
logger.info("All done.")
async def refresh_tags():
"""刷新已存在的视频的标签,从不支持标签的版本升级上来后需要手动调用一次"""
items = await FavoriteItem.filter(
downloaded=True,
tags=None,
).prefetch_related("upper")
await asyncio.gather( await asyncio.gather(
*[aremove(item.nfo_path) for item in items], *[aremove(path) for item in items for path in path_getter(item)],
return_exceptions=True, return_exceptions=True,
) )
await asyncio.gather( await asyncio.gather(
*[ *[
process_video( process_favorite_item(
item, item,
process_poster=False, process_poster=process_poster,
process_video=False, process_video=process_video,
process_nfo=True, process_nfo=process_nfo,
process_upper=False, process_upper=process_upper,
process_subtitle=process_subtitle,
) )
for item in items for item in items
], ],
return_exceptions=True, return_exceptions=True,
) )
refresh_nfo = functools.partial(
_refresh_favorite_item_info, lambda item: [item.nfo_path], process_nfo=True
)
refresh_poster = functools.partial(
_refresh_favorite_item_info,
lambda item: [item.poster_path],
process_poster=True,
)
refresh_video = functools.partial(
_refresh_favorite_item_info,
lambda item: [item.video_path],
process_video=True,
)
refresh_upper = functools.partial(
_refresh_favorite_item_info,
lambda item: item.upper_path,
process_upper=True,
)
refresh_subtitle = functools.partial(
_refresh_favorite_item_info,
lambda item: [item.subtitle_path],
process_subtitle=True,
)
+15 -5
View File
@@ -4,7 +4,14 @@ import sys
import uvloop import uvloop
from loguru import logger from loguru import logger
from commands import recheck, refresh_tags, upper_thumb from commands import (
recheck,
refresh_nfo,
refresh_poster,
refresh_subtitle,
refresh_upper,
refresh_video,
)
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
@@ -14,12 +21,15 @@ asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
async def entry() -> None: async def entry() -> None:
await init_model() await init_model()
for command, func in [ for command, func in (
("once", process), ("once", process),
("recheck", recheck), ("recheck", recheck),
("upper_thumb", upper_thumb), ("refresh_poster", refresh_poster),
("refresh_tags", refresh_tags), ("refresh_upper", refresh_upper),
]: ("refresh_nfo", refresh_nfo),
("refresh_video", refresh_video),
("refresh_subtitle", refresh_subtitle),
):
if any(command in _ for _ in sys.argv): if any(command in _ for _ in sys.argv):
logger.info("Running {}...", command) logger.info("Running {}...", command)
await func() await func()
+14
View File
@@ -133,6 +133,20 @@ class FavoriteItem(Model):
/ f"{self.bvid}-poster.jpg" / f"{self.bvid}-poster.jpg"
) )
@property
def upper_path(self) -> list[Path]:
return [
self.upper.thumb_path,
self.upper.meta_path,
]
@property
def subtitle_path(self) -> Path:
return (
Path(settings.path_mapper[self.favorite_list_id])
/ f"{self.bvid}.zh-CN.default.ass"
)
async def init_model() -> None: async def init_model() -> None:
await Tortoise.init(config=TORTOISE_ORM) await Tortoise.init(config=TORTOISE_ORM)
+16 -2
View File
@@ -149,19 +149,20 @@ async def process_favorite(favorite_id: int) -> None:
downloaded=False, downloaded=False,
).prefetch_related("upper") ).prefetch_related("upper")
await asyncio.gather( await asyncio.gather(
*[process_video(item) for item in all_unprocessed_items], *[process_favorite_item(item) for item in all_unprocessed_items],
return_exceptions=True, return_exceptions=True,
) )
logger.info("Favorite {} {} processed successfully.", favorite_id, title) logger.info("Favorite {} {} processed successfully.", favorite_id, title)
@concurrent_decorator(4) @concurrent_decorator(4)
async def process_video( async def process_favorite_item(
fav_item: FavoriteItem, fav_item: FavoriteItem,
process_poster=True, process_poster=True,
process_video=True, process_video=True,
process_nfo=True, process_nfo=True,
process_upper=True, process_upper=True,
process_subtitle=True,
) -> None: ) -> None:
logger.info("Start to process video {} {}", fav_item.bvid, fav_item.name) logger.info("Start to process video {} {}", fav_item.bvid, fav_item.name)
if fav_item.type != MediaType.VIDEO: if fav_item.type != MediaType.VIDEO:
@@ -234,6 +235,19 @@ async def process_video(
fav_item.bvid, fav_item.bvid,
fav_item.name, fav_item.name,
) )
if process_subtitle:
pass
# # 写入字幕,上游库获取字幕有 bug,暂时不做实现
# if not await aexists(fav_item.subtitle_path):
# await ass.make_ass_file_danmakus_protobuf(
# v, 0, str(fav_item.subtitle_path.resolve())
# )
# else:
# logger.info(
# "Subtitle of {} {} already exists, skipped.",
# fav_item.bvid,
# fav_item.name,
# )
if process_video: if process_video:
if await aexists(fav_item.video_path): if await aexists(fav_item.video_path):
fav_item.downloaded = True fav_item.downloaded = True