lanbogao commited on
Commit
5d322d8
1 Parent(s): c997914

Add api check.

Browse files
Files changed (3) hide show
  1. Dockerfile +1 -1
  2. main.py +14 -4
  3. requirements.txt +5 -5
Dockerfile CHANGED
@@ -1,4 +1,4 @@
1
- FROM python:3.9
2
 
3
  WORKDIR /code
4
 
 
1
+ FROM python:3.10
2
 
3
  WORKDIR /code
4
 
main.py CHANGED
@@ -1,11 +1,17 @@
1
- from fastapi import FastAPI
 
2
  from fastapi.responses import JSONResponse
 
3
  from fetchYoutubeSubtitle import fetchSubtitle, fetchSubtitleUrls
4
 
 
5
  app = FastAPI()
6
 
7
  @app.get("/")
8
- def read_root():
 
 
 
9
  return {"Hello": "World!"}
10
 
11
 
@@ -15,12 +21,16 @@ def read_json():
15
 
16
 
17
  @app.get("/subtitle/")
18
- async def get_subtitle(url: str, subtype: str="srt"):
 
 
19
  subtitle = await fetchSubtitle(url,subType=subtype)
20
  return JSONResponse(content=subtitle)
21
 
22
 
23
  @app.get("/subtitle-urls/")
24
- async def get_subtitleUrls(url: str):
 
 
25
  subtitles = await fetchSubtitleUrls(url)
26
  return JSONResponse(content=subtitles)
 
1
+ import os
2
+ from fastapi import FastAPI, Header, Request
3
  from fastapi.responses import JSONResponse
4
+ from typing import Annotated
5
  from fetchYoutubeSubtitle import fetchSubtitle, fetchSubtitleUrls
6
 
7
+ token = os.getenv("APP_KEY")
8
  app = FastAPI()
9
 
10
  @app.get("/")
11
+ def read_root(request:Request):
12
+ print(request.headers)
13
+ print(request.client.host)
14
+ print(request.client.port)
15
  return {"Hello": "World!"}
16
 
17
 
 
21
 
22
 
23
  @app.get("/subtitle/")
24
+ async def get_subtitle(url: str, subtype: str="srt", x_token: Annotated[str | None, Header()] = None):
25
+ if token != x_token:
26
+ return JSONResponse({"error": "Invalid token"})
27
  subtitle = await fetchSubtitle(url,subType=subtype)
28
  return JSONResponse(content=subtitle)
29
 
30
 
31
  @app.get("/subtitle-urls/")
32
+ async def get_subtitleUrls(url: str, x_token: Annotated[str | None, Header()] = None):
33
+ if token != x_token:
34
+ return JSONResponse({"error": "Invalid token"})
35
  subtitles = await fetchSubtitleUrls(url)
36
  return JSONResponse(content=subtitles)
requirements.txt CHANGED
@@ -1,7 +1,7 @@
1
- fastapi==0.74.*
2
- requests==2.27.*
3
- sentencepiece==0.1.*
4
- torch==1.11.*
5
- transformers==4.*
6
  uvicorn[standard]==0.17.*
7
  yt-dlp
 
1
+ fastapi==0.95.*
2
+ # requests==2.27.*
3
+ # sentencepiece==0.1.*
4
+ # torch==1.11.*
5
+ # transformers==4.*
6
  uvicorn[standard]==0.17.*
7
  yt-dlp