-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
48 lines (40 loc) · 1.46 KB
/
api.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
from fastapi import Request, FastAPI, Form
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from http import HTTPStatus
from typing import Dict
from elasticsearch import Elasticsearch
from src.utils import retrieved_documents, get_coordinates_list, get_tweet_text
#connect to elasticsearch
es_connection = Elasticsearch("http://localhost:9200")
#define a template object
templates = Jinja2Templates(directory="templates")
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get('/')
def _search_view(request: Request):
return templates.TemplateResponse("index.html", {
"request": request,
})
@app.get('/search')
async def _get_docs(request: Request) -> Dict:
"""
Return a response contains a coordinates list of dictionaries that contains the lat, lng,
score for each retrieved documents
"""
params = request.query_params.items()
params = dict(params)
docs = retrieved_documents(params, es_connection, "tweets_with_mapping2")
coordinates = get_coordinates_list(docs)
tweets_text = get_tweet_text(docs)
alert = False
#Check if the query respone has no documents
if len(coordinates) == 0:
alert = True
return {
"message": HTTPStatus.OK.phrase,
"status-code": HTTPStatus.OK,
"alert": alert,
"coordinates": coordinates,
"text_list": tweets_text,
}