-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathmain.py
53 lines (41 loc) · 1.44 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
import re
from parser import VideoSource, parse_video_id, parse_video_share_url
import uvicorn
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
app = FastAPI()
templates = Jinja2Templates(directory="templates")
@app.get("/", response_class=HTMLResponse)
async def read_item(request: Request):
return templates.TemplateResponse(
request=request,
name="index.html",
context={
"title": "github.com/wujunwei928/parse-video-py Demo",
},
)
@app.get("/video/share/url/parse")
async def share_url_parse(url: str):
url_reg = re.compile(r"http[s]?:\/\/[\w.-]+[\w\/-]*[\w.-]*\??[\w=&:\-\+\%]*[/]*")
video_share_url = url_reg.search(url).group()
try:
video_info = await parse_video_share_url(video_share_url)
return {"code": 200, "msg": "解析成功", "data": video_info.__dict__}
except Exception as err:
return {
"code": 500,
"msg": str(err),
}
@app.get("/video/id/parse")
async def video_id_parse(source: VideoSource, video_id: str):
try:
video_info = await parse_video_id(source, video_id)
return {"code": 200, "msg": "解析成功", "data": video_info.__dict__}
except Exception as err:
return {
"code": 500,
"msg": str(err),
}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)