-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
47 lines (38 loc) · 1.25 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
import time
from utils import load_messages
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
app = FastAPI()
messages = load_messages()
@app.get("/")
async def root():
return {"message": "Hello, World!"}
@app.get("/query/{query}")
async def read_item(query):
query = query.lower()
indices = []
start = time.time()
for i in range(len(messages)):
text = str(messages[i]['text'])
if query in text:
indices.extend([messages[i]['id']])
end = time.time()
time_elapsed = end - start
return {"results": indices, "count": len(indices), "time": time_elapsed}
@app.get("/search/{query}", response_class=HTMLResponse)
async def read_item(query):
query = query.lower()
indices = []
start = time.time()
for i in range(len(messages)):
text = str(messages[i]['text'])
if query in text:
indices.extend([messages[i]['id']])
end = time.time()
time_elapsed = end - start
html_content = ""
for i in range(len(indices)):
html_content += '<script>window.location="https://tx.me/s/rememberbox/{}"</script>'.format(indices[i])
if not html_content:
return "No results found!"
return HTMLResponse(content=html_content, status_code=200)