lanbogao's picture
Set token env name to HF_X_TOKEN.
4bfa0b3
raw
history blame
1.09 kB
import os
from fastapi import FastAPI, Header, Request
from fastapi.responses import JSONResponse
from typing import Annotated
from fetchYoutubeSubtitle import fetchSubtitle, fetchSubtitleUrls
token = os.getenv("HF_X_TOKEN")
app = FastAPI()
@app.get("/")
def read_root(request:Request):
print(request.headers)
print(request.client.host)
print(request.client.port)
return {"Hello": "World!"}
@app.get("/json")
def read_json():
return JSONResponse(content={"Hello": "World!"})
@app.get("/subtitle/")
async def get_subtitle(url: str, subtype: str="srt", x_token: Annotated[str | None, Header()] = None):
if token != x_token:
return JSONResponse({"error": "Invalid token"})
subtitle = await fetchSubtitle(url,subType=subtype)
return JSONResponse(content=subtitle)
@app.get("/subtitle-urls/")
async def get_subtitleUrls(url: str, x_token: Annotated[str | None, Header()] = None):
if token != x_token:
return JSONResponse({"error": "Invalid token"})
subtitles = await fetchSubtitleUrls(url)
return JSONResponse(content=subtitles)