From d10e9a70880260df03b8c05488258976385a58e2 Mon Sep 17 00:00:00 2001 From: Zhao Zuohong Date: Tue, 21 Feb 2023 19:56:22 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Expose=20manager=20to=20web=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/__init__.py | 30 ++++++++++++++++++++++++++++++ manager/__init__.py | 11 +++++++---- poetry.lock | 19 ++++++++++++++++++- pyproject.toml | 1 + 4 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 backend/__init__.py diff --git a/backend/__init__.py b/backend/__init__.py new file mode 100644 index 0000000..a510a25 --- /dev/null +++ b/backend/__init__.py @@ -0,0 +1,30 @@ +from bottle import request, response, Bottle +from manager import Manager +from json import dumps +from pathlib import Path + +manager = Manager() +app = Bottle() + + +@app.post("/video") +def add_videos(): + manager.add_videos_by_number(request.json) + return "OK" + + +@app.get("/progress") +def get_progress(): + response.content_type = "application/json" + return dumps(manager.get_progress()) + + +@app.post("/download") +def download(): + parent_dir = Path(request.json["parent_dir"]) + id = request.json["id"] + try: + manager.download(id, parent_dir) + return "OK" + except: + return "Not ready" diff --git a/manager/__init__.py b/manager/__init__.py index b4f51e5..9acbb5e 100644 --- a/manager/__init__.py +++ b/manager/__init__.py @@ -7,15 +7,18 @@ from pathlib import Path class Manager: def __init__(self): self.video_list: list[Video] = [] + self.bvid_set = set() - def add_videos_by_number(self, video_numbers: str): - for video_number in video_numbers.split(): + def add_videos_by_number(self, video_numbers: list[str]): + for video_number in video_numbers: try: video = Video(video_number) except: continue - Thread(target=video.get_info).start() - self.video_list.append(video) + if (bvid := video.v.get_bvid()) not in self.bvid_set: + self.bvid_set.add(bvid) + self.video_list.append(video) + Thread(target=video.get_info).start() def download(self, id: int, parent_dir: str | Path = Path(".")): if isinstance(parent_dir, str): diff --git a/poetry.lock b/poetry.lock index 25c2ea7..f55778d 100644 --- a/poetry.lock +++ b/poetry.lock @@ -347,6 +347,23 @@ type = "legacy" url = "https://pypi.tuna.tsinghua.edu.cn/simple" reference = "mirrors" +[[package]] +name = "bottle" +version = "0.12.23" +description = "Fast and simple WSGI-framework for small web-applications." +category = "main" +optional = false +python-versions = "*" +files = [ + {file = "bottle-0.12.23-py3-none-any.whl", hash = "sha256:9f1c363257c590bd34db5fad4693a7f06ff4217e9ad18337451de69c25137127"}, + {file = "bottle-0.12.23.tar.gz", hash = "sha256:683de3aa399fb26e87b274dbcf70b1a651385d459131716387abdc3792e04167"}, +] + +[package.source] +type = "legacy" +url = "https://pypi.tuna.tsinghua.edu.cn/simple" +reference = "mirrors" + [[package]] name = "brotli" version = "1.0.9" @@ -1653,4 +1670,4 @@ reference = "mirrors" [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "b6c2ea94f40c44e176ac7606d5a83e0629e41489541d8677ad5fc898de9d89d2" +content-hash = "355be7949f7315d306dda590f37dc6c75b8c29130154e934c9b50285807faab5" diff --git a/pyproject.toml b/pyproject.toml index 512813a..2e41b63 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,6 +9,7 @@ license = "GPL-3.0-only" python = "^3.11" bilibili-api-python = "^15.1.0" sanitize-filename = "^1.2.0" +bottle = "^0.12.23" [tool.poetry.group.dev.dependencies] black = "^23.1.0"