add gracefull errors

This commit is contained in:
Thephaseless 2024-07-25 21:00:11 +00:00
parent 24a0271a04
commit af4750a52a

30
main.py
View File

@ -12,9 +12,7 @@ from src.models.requests import LinkRequest, LinkResponse
from src.utils import logger from src.utils import logger
from src.utils.browser import bypass_cloudflare, new_browser from src.utils.browser import bypass_cloudflare, new_browser
from src.utils.consts import LOG_LEVEL from src.utils.consts import LOG_LEVEL
from src.utils.extentions import download_extentions
download_extentions()
app = FastAPI(debug=True) app = FastAPI(debug=True)
@ -32,21 +30,25 @@ async def read_item(request: LinkRequest):
logger.info(f"Request: {request}") logger.info(f"Request: {request}")
start_time = int(time.time() * 1000) start_time = int(time.time() * 1000)
browser = await new_browser() browser = await new_browser()
page = await browser.get(request.url) try:
await page.bring_to_front() page = await browser.get(request.url)
await page.bring_to_front()
challenged = await asyncio.wait_for( challenged = await asyncio.wait_for(
bypass_cloudflare(page), timeout=request.maxTimeout bypass_cloudflare(page), timeout=request.maxTimeout
) )
logger.info(f"Got webpage: {request.url}") logger.info(f"Got webpage: {request.url}")
response = await LinkResponse.create( response = await LinkResponse.create(
page=page, page=page,
start_timestamp=start_time, start_timestamp=start_time,
challenged=challenged, challenged=challenged,
) )
browser.stop() except asyncio.TimeoutError:
logger.fatal("Couldn't complete the request")
finally:
browser.stop()
return response return response