/
main.py
69 lines (57 loc) · 1.56 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from fastapi import FastAPI, Request
from pydantic import BaseModel
from typing import Union
from bilibili.search import bvid_to_music
app = FastAPI()
class Status(BaseModel):
status_code: int
msg: str
class BasicResp(BaseModel):
status: Status
@app.get("/", description="keep alive API", response_model=BasicResp)
async def root():
resp = {
"status": {
"status_code": 200,
"msg": "ok"
}
}
return resp
class MusicInfo(BaseModel):
name: str
author: str
source: str
duration: int
cover_image_url: str
class BproxyResp(BaseModel):
data: MusicInfo
status: Status
@app.get("/bproxy", description="a proxy service to fetch music info by BVid", response_model=Union[BproxyResp, BasicResp])
async def bproxy(request: Request, bvid: str=""):
try:
matched, name, author, source, duration, cover_image_url = await bvid_to_music(BVid=bvid)
resp = {
"data": {
"name": name,
"author": author,
"source": source,
"duration": duration,
"cover_image_url": cover_image_url,
},
"status": {
"status_code": 200,
"msg": "ok",
}
}
except Exception as e:
resp = {
"data": {},
"status": {
"status_code": 500,
"msg": str(e)
}
}
return resp
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)