-
Notifications
You must be signed in to change notification settings - Fork 113
/
app.py
83 lines (59 loc) · 2.34 KB
/
app.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from dotenv import load_dotenv
load_dotenv()
import logging
import os
from typing import Annotated, Optional
from urllib.parse import urlencode
from fastapi import FastAPI, Form, Request
from fastapi.responses import HTMLResponse, PlainTextResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from src.itemlist import ItemList
from src.utils import find_time_slice
YOUTUBE_APIS = os.environ["APIS"].split(";")
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
logger = logging.getLogger(__name__)
fapp = FastAPI()
fapp.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
@fapp.get("/", response_class=HTMLResponse)
async def home(request: Request):
return templates.TemplateResponse("home.html", {"request": request})
@fapp.post("/", response_class=HTMLResponse)
async def home(
request: Request,
search_string: Annotated[str, Form()],
range_start: Annotated[Optional[str], Form()],
range_end: Annotated[Optional[str], Form()],
custom_speed: Annotated[Optional[str], Form()],
youtube_api: Annotated[Optional[str], Form()],
):
range_start = int(range_start) if range_start else 1
range_end = int(range_end) if range_end else 500
custom_speed = float(custom_speed) if custom_speed else None
if range_start > range_end:
range_start, range_end = range_end, range_start
try:
logger.info(f"Input TS({find_time_slice()}): {search_string}")
youtube_api = youtube_api if youtube_api else YOUTUBE_APIS[find_time_slice()]
items = ItemList(
search_string, range_start, range_end, custom_speed, youtube_api
)
await items.do_async_work()
output = items.get_output_string()
except Exception as e:
output = [[f"Error: {e}"]]
logger.error(f"{output}")
return templates.TemplateResponse(
"home.html", {"request": request, "playlist_detail": output}
)
@fapp.get("/healthz")
def healthz():
return "Success"
@fapp.get("/ads.txt", response_class=PlainTextResponse)
def static_from_root_google():
return "google.com, pub-8874895270666721, DIRECT, f08c47fec0942fa0"
if __name__ == "__main__":
fapp.run(
use_reloader=True, debug=False, host="0.0.0.0", port=10000, access_log=False
)