-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
60 lines (44 loc) · 1.85 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
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import Response
from duckduckgo_search import ddg, ddg_news
from models import SearchRequest, SearchResponse, ContentRequest, ContentResponse
from utils import get_text
from browser import get_site, get_raw
# create a new FastAPI app with cors enabled
app = FastAPI(title="Web Search")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
def index():
return {"message": "Hello World"}
@app.post("/search", description="Search for a query", response_model=SearchResponse)
def search(search_req: SearchRequest):
results = ddg(search_req.query)
return {"results": results}
@app.post('/news', description="Get the latest news", response_model=SearchResponse)
def news(search_req: SearchRequest):
results = ddg_news(search_req.query)
return {"results": results}
@app.post("/content", description="Get the content of a URL", response_model=ContentResponse)
def content(content_req: ContentRequest):
resp = get_site(content_req.url)
text = get_text(resp)
return {"content": text}
@app.post("/content/raw", description="Get the raw content of a URL. Use for getting raw files from GitHub or getting text files.", response_model=ContentResponse)
def content_raw(content_req: ContentRequest):
resp = get_raw(content_req.url)
return {"content": resp}
@app.get('/.well-known/ai-plugin.json', include_in_schema=False)
def read_ai_plugin_json() -> Response:
with open('ai-plugin.json', 'r') as f:
return Response(f.read(), media_type='application/json')
@app.get('/robots.txt', include_in_schema=False)
def read_robots_txt() -> Response:
with open('robots.txt', 'r') as f:
return Response(f.read(), media_type='text/plain')