File size: 1,882 Bytes
ffce262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()