From 955c3c0b3d973e530a374af69578a5d449ff9451 Mon Sep 17 00:00:00 2001 From: yzqzss Date: Wed, 1 Nov 2023 20:08:12 +0800 Subject: [PATCH] feat(api): ready to use --- biliarchiver/cli_tools/biliarchiver.py | 27 ++++++-------------------- biliarchiver/rest_api/bilivid.py | 24 +++++++++++++++-------- 2 files changed, 22 insertions(+), 29 deletions(-) diff --git a/biliarchiver/cli_tools/biliarchiver.py b/biliarchiver/cli_tools/biliarchiver.py index 4f60a8c..7ec6ce4 100644 --- a/biliarchiver/cli_tools/biliarchiver.py +++ b/biliarchiver/cli_tools/biliarchiver.py @@ -82,30 +82,15 @@ def auth(): @biliarchiver.command(help=click.style(_("运行 API"), fg="cyan")) -@click.option( - "--host", - type=str, - default="127.0.0.1", - show_default=True, -) -@click.option( - "--port", - type=int, - default=8000, - show_default=True, -) -def api(**kwargs): +def api(): try: import fastapi - import uvicorn except ImportError: - print("Please install fastapi and uvicorn first.") - print('pip install "uvicorn[standard]" fastapi') - return - - from biliarchiver.rest_api.main import app - - uvicorn.run(app, **kwargs) + print("Please fastapi first") + print('pip install fastapi') + print("------------------------") + print("Then, install any ASGI server you like and to run the app manually: ()") + print('biliarchiver.rest_api.main:app') if __name__ == "__main__": diff --git a/biliarchiver/rest_api/bilivid.py b/biliarchiver/rest_api/bilivid.py index fe7a4e4..cc56708 100644 --- a/biliarchiver/rest_api/bilivid.py +++ b/biliarchiver/rest_api/bilivid.py @@ -3,10 +3,6 @@ from enum import Enum import time from typing import Optional -from biliarchiver.cli_tools.bili_archive_bvids import _down -from biliarchiver.cli_tools.up_command import DEFAULT_COLLECTION - - class VideoStatus(str, Enum): pending = "pending" downloading = "downloading" @@ -37,14 +33,20 @@ class BiliVideo: try: process = await asyncio.create_subprocess_exec(*cmd) retcode = await process.wait() - except (KeyboardInterrupt, SystemExit, Exception): + process = None + except (KeyboardInterrupt, SystemExit, Exception) as e: if process: process.terminate() await process.wait() - print("download terminated") + print("download terminated:", e) return -1 else: return retcode + finally: + if process: + process.terminate() + await process.wait() + print("download terminated: (finally)") async def up(self) -> int: from asyncio import subprocess @@ -56,11 +58,17 @@ class BiliVideo: try: process = await subprocess.create_subprocess_exec(*cmd) retcode = await process.wait() - except (KeyboardInterrupt, SystemExit, Exception): + process = None + except (KeyboardInterrupt, SystemExit, Exception) as e: if process: process.terminate() await process.wait() - print("upload terminated") + print("upload terminated", e) return -1 else: return retcode + finally: + if process: + process.terminate() + await process.wait() + print("upload terminated: (finally)")