MIA / app.py
SharryOG's picture
Update app.py
69b2f98 verified
raw
history blame
2.93 kB
from fastapi import FastAPI, Query, HTTPException
from pydantic import BaseModel
from webscout import WEBS
app = FastAPI()
class SearchQuery(BaseModel):
q: str
max_results: int = 10
timelimit: Optional[int] = None
safesearch: str = 'moderate'
region: str = 'wt-wt'
class ImageSearchQuery(BaseModel):
q: str
max_results: int = 10
safesearch: str = 'moderate'
region: str = 'wt-wt'
class VideoSearchQuery(BaseModel):
q: str
max_results: int = 10
safesearch: str = 'moderate'
region: str = 'wt-wt'
timelimit: Optional[int] = None
resolution: Optional[str] = None
duration: Optional[str] = None
class NewsSearchQuery(BaseModel):
q: str
max_results: int = 10
safesearch: str = 'moderate'
region: str = 'wt-wt'
timelimit: Optional[int] = None
class MapSearchQuery(BaseModel):
q: str
place: Optional[str] = None
max_results: int = 10
class TranslateQuery(BaseModel):
q: str
to: str = 'en'
class SuggestionQuery(BaseModel):
q: str
@app.get("/api/search", response_model=List[dict])
async def search_text(query: SearchQuery):
results = []
with WEBS() as webs:
for result in webs.text(**query.dict()):
results.append(result)
return results
@app.get("/api/images", response_model=List[dict])
async def search_images(query: ImageSearchQuery):
results = []
with WEBS() as webs:
for result in webs.images(**query.dict()):
results.append(result)
return results
@app.get("/api/videos", response_model=List[dict])
async def search_videos(query: VideoSearchQuery):
results = []
with WEBS() as webs:
for result in webs.videos(**query.dict()):
results.append(result)
return results
@app.get("/api/news", response_model=List[dict])
async def search_news(query: NewsSearchQuery):
results = []
with WEBS() as webs:
for result in webs.news(**query.dict()):
results.append(result)
return results
@app.get("/api/maps", response_model=List[dict])
async def search_maps(query: MapSearchQuery):
results = []
with WEBS() as webs:
for result in webs.maps(**query.dict()):
results.append(result)
return results
@app.get("/api/translate", response_model=dict)
async def translate_text(query: TranslateQuery):
with WEBS() as webs:
translation = webs.translate(**query.dict())
return {'translation': translation}
@app.get("/api/suggestions", response_model=List[str])
async def search_suggestions(query: SuggestionQuery):
if not query.q:
raise HTTPException(status_code=400, detail="Query parameter missing")
results = []
with WEBS() as webs:
for result in webs.suggestions(query.q):
results.append(result)
return results
@app.get("/api/health", response_model=dict)
async def health_check():
return {'status': 'working'}