Expose manager to web api

This commit is contained in:
zhbaor 2023-02-21 19:56:22 +08:00
parent 23c8f3884d
commit d10e9a7088
4 changed files with 56 additions and 5 deletions

30
backend/__init__.py Normal file
View file

@ -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"

View file

@ -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()
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):

19
poetry.lock generated
View file

@ -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"

View file

@ -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"