From 5b98fc28dbe8e318f3b9cddb0b6ea3d38f4dd3ac Mon Sep 17 00:00:00 2001 From: amtoaer Date: Thu, 23 Nov 2023 00:50:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=A7=BB=E9=99=A4=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E6=96=87=E4=BB=B6=EF=BC=8C=E5=BF=BD=E7=95=A5=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=90=8D=E4=B8=AD=E7=9A=84=E9=9D=9E=E6=B3=95=E5=AD=97=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 8 +++++++- entry.py | 7 ++++++- main.py | 4 ---- processor.py | 15 ++++++++------- 4 files changed, 21 insertions(+), 13 deletions(-) delete mode 100644 main.py diff --git a/Makefile b/Makefile index a888252..13cb66d 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: install lint +.PHONY: install fmt start-daemon start-once install: @echo "Installing dependencies..." @@ -8,3 +8,9 @@ fmt: @echo "Formatting..." @poetry run black . @poetry run ruff --fix . + +start-daemon: + @poetry run python entry.py + +start-once: + @poetry run python entry.py --once \ No newline at end of file diff --git a/entry.py b/entry.py index a230d64..70b03bb 100644 --- a/entry.py +++ b/entry.py @@ -1,6 +1,8 @@ import asyncio import sys +from loguru import logger + from processor import process from settings import settings @@ -8,11 +10,14 @@ from settings import settings async def entry() -> None: if any("once" in _ for _ in sys.argv): # 单次运行 + logger.info("Running once...") await process() + return + logger.info("Running daemon...") while True: await process() await asyncio.sleep(settings.interval * 60) -def start() -> None: +if __name__ == "__main__": asyncio.run(entry()) diff --git a/main.py b/main.py deleted file mode 100644 index 9e5fcc0..0000000 --- a/main.py +++ /dev/null @@ -1,4 +0,0 @@ -from entry import start - -if __name__ == "__main__": - start() diff --git a/processor.py b/processor.py index 4cf2afb..0075143 100644 --- a/processor.py +++ b/processor.py @@ -81,16 +81,17 @@ async def process_favorite(favorite_id: int) -> None: async def process_video(save_path: Path, media: dict) -> None: title = media["title"] + safe_title = media["title"].replace("/", "_") logger.info("start to process video {}", title) if media["type"] != MediaType.VIDEO: logger.warning("Media {} is not a video, skipped.", title) return - final_path = save_path / f"{title}.mp4" + final_path = save_path / f"{safe_title}.mp4" if final_path.exists(): logger.info(f"{final_path} already exists, skipped.") return # 写入 nfo - nfo_path = save_path / f"{title}.nfo" + nfo_path = save_path / f"{safe_title}.nfo" EpisodeInfo( title=title, plot=media["intro"], @@ -99,7 +100,7 @@ async def process_video(save_path: Path, media: dict) -> None: aired=datetime.datetime.fromtimestamp(media["ctime"]), ).write_nfo(nfo_path) # 写入 poster - cover_path = save_path / f"{title}-poster.jpg" + cover_path = save_path / f"{safe_title}-poster.jpg" await download_content(media["cover"], cover_path) # 开始处理视频内容 v = video.Video(media["bvid"], credential=credential) @@ -108,7 +109,7 @@ async def process_video(save_path: Path, media: dict) -> None: ) streams = detector.detect_best_streams() if detector.check_flv_stream(): - tmp_path = save_path / f"{title}.flv" + tmp_path = save_path / f"{safe_title}.flv" await download_content(streams[0].url, tmp_path) process = await create_subprocess_exec( FFMPEG_COMMAND, @@ -122,8 +123,8 @@ async def process_video(save_path: Path, media: dict) -> None: tmp_path.unlink() else: tmp_video_path, tmp_audio_path = ( - save_path / f"{title}_video.m4s", - save_path / f"{title}_audio.m4s", + save_path / f"{safe_title}_video.m4s", + save_path / f"{safe_title}_audio.m4s", ) await asyncio.gather( download_content(streams[0].url, tmp_video_path), @@ -144,4 +145,4 @@ async def process_video(save_path: Path, media: dict) -> None: await process.communicate() tmp_video_path.unlink() tmp_audio_path.unlink() - logger.info(f"{final_path} downloaded successfully.") + logger.info(f"{title} downloaded successfully.")