| from fastapi import FastAPI, Query |
| from scrape import scrape, sanitize, convert |
| from search import search, format as format_results, extract_urls, images, videos |
| import asyncio |
| from fastapi.responses import JSONResponse |
|
|
| app = FastAPI() |
|
|
| @app.get("/scrape") |
| async def scrape_endpoint(url: str = Query('https://example.com')): |
| html = await sanitize(await scrape(url)) |
| md = await convert(html) |
| return JSONResponse(content={"markdown": md}) |
|
|
| @app.post("/sanitize") |
| async def sanitize_endpoint(content: str): |
| sanitized = await sanitize(content) |
| return {"sanitized": sanitized} |
|
|
| @app.post("/convert") |
| async def convert_endpoint(content: str): |
| markdown = await convert(content) |
| return {"markdown": markdown} |
|
|
| @app.get("/search") |
| def search_endpoint(prompt: str, page: int = 1, region: str = 'us-en', safesearch: str = 'off', timelimit: str = 'y'): |
| results = search(prompt, page, region, safesearch, timelimit) |
| return {"results": list(results)} |
|
|
| @app.get("/images") |
| def images_endpoint(prompt: str, page: int = 1, region: str = 'us-en', safesearch: str = 'off', timelimit: str = 'y'): |
| results = images(prompt, page, region, safesearch, timelimit) |
| return {"results": list(results)} |
|
|
| @app.get("/videos") |
| def videos_endpoint(prompt: str, page: int = 1, region: str = 'us-en', safesearch: str = 'off', timelimit: str = 'y'): |
| results = videos(prompt, page, region, safesearch, timelimit) |
| return {"results": list(results)} |
|
|
| @app.post("/format") |
| def format_endpoint(results: list, prompt: str): |
| formatted = format_results(results, prompt) |
| return {"formatted": formatted} |
|
|
| @app.post("/extract_urls") |
| def extract_urls_endpoint(results: list): |
| urls = extract_urls(results) |
| return {"urls": urls} |
|
|
| def run(): |
| import uvicorn |
| uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True) |
|
|
| if __name__ == "__main__": |
| run() |