-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
70 lines (59 loc) · 1.86 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
70
import os
from typing import List
import uvicorn
from fastapi import Depends, FastAPI, HTTPException
from fastapi import status as http_status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
from fastapi.security import HTTPBearer
import schemas
import utils
from CONSTANTS import MAX_WORKERS
from invoke_scraper import invoke
from logger import get_logger
from utils import ErrorHandlerRoute
logger = get_logger(__name__)
auth_token = os.environ["AUTH_TOKEN"]
app = FastAPI(
title="Twitter Disaster",
description="",
version="0.1",
)
app.router.route_class = ErrorHandlerRoute
# TODO: This needs to be tuned
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def status():
return {"status": "OK"}
@app.post("/tweets", response_model=List[schemas.TweetData])
async def get_tweets_by_tag(tags: str, count: str, token=Depends(HTTPBearer())):
creds = token.credentials
if creds != auth_token:
raise HTTPException(
status_code=http_status.HTTP_403_FORBIDDEN, detail="Invalid auth token"
)
count = int(count)
if count < 10:
raise HTTPException(
status_code=http_status.HTTP_400_BAD_REQUEST, detail="Count should be >= 10"
)
results = invoke([tags], max_workers=MAX_WORKERS, no_of_tweets=count)
results = [*results]
tweets = utils.predictions(results)
return tweets
if __name__ == "__main__":
log_config = uvicorn.config.LOGGING_CONFIG
log_config["formatters"]["access"][
"fmt"
] = "%(asctime)s - %(levelname)s - %(message)s"
log_config["formatters"]["default"][
"fmt"
] = "%(asctime)s - %(levelname)s - %(message)s"
uvicorn.run("main:app", host="0.0.0.0", port=80, reload=True)