Byparr/main.py

53 lines
1.3 KiB
Python
Raw Normal View History

2024-07-24 13:57:40 +00:00
from __future__ import annotations
import asyncio
import time
import uvicorn
import uvicorn.config
from fastapi import FastAPI
from fastapi.responses import RedirectResponse
from src.models.requests import LinkRequest, LinkResponse
from src.utils import logger
2024-07-24 15:16:46 +00:00
from src.utils.browser import bypass_cloudflare, new_browser
2024-07-24 13:57:40 +00:00
from src.utils.extentions import download_extentions
download_extentions()
app = FastAPI(debug=True)
@app.get("/")
def read_root():
"""Redirect to /docs."""
logger.info("Redirecting to /docs")
return RedirectResponse(url="/docs", status_code=301)
@app.post("/v1")
async def read_item(request: LinkRequest):
"""Handle POST requests."""
2024-07-24 15:41:41 +00:00
# request.url = "https://nowsecure.nl"
2024-07-24 13:57:40 +00:00
logger.info(f"Request: {request}")
start_time = int(time.time() * 1000)
browser = await new_browser()
page = await browser.get(request.url)
challenged = await asyncio.wait_for(
bypass_cloudflare(page), timeout=request.maxTimeout
)
2024-07-24 15:16:46 +00:00
logger.info(f"Got webpage: {request.url}")
2024-07-24 13:57:40 +00:00
response = await LinkResponse.create(
page=page,
start_timestamp=start_time,
challenged=challenged,
)
browser.stop()
return response
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8191) # noqa: S104