Byparr/main.py

70 lines
1.9 KiB
Python
Raw Normal View History

2024-07-24 13:57:40 +00:00
from __future__ import annotations
2024-09-13 18:03:38 +00:00
import logging
2024-07-24 13:57:40 +00:00
import time
import uvicorn.config
2024-11-24 22:00:58 +00:00
from fastapi import FastAPI
2024-07-24 13:57:40 +00:00
from fastapi.responses import RedirectResponse
2024-11-24 22:00:58 +00:00
from sbase import SB, BaseCase
2024-07-24 13:57:40 +00:00
2024-11-24 22:00:58 +00:00
from src.models.requests import LinkRequest, LinkResponse, Solution
2024-07-24 13:57:40 +00:00
from src.utils import logger
2024-07-25 00:06:56 +00:00
from src.utils.consts import LOG_LEVEL
2024-07-24 13:57:40 +00:00
2024-09-13 18:03:38 +00:00
app = FastAPI(debug=LOG_LEVEL == logging.DEBUG, log_level=LOG_LEVEL)
2024-07-24 13:57:40 +00:00
@app.get("/")
def read_root():
"""Redirect to /docs."""
logger.info("Redirecting to /docs")
return RedirectResponse(url="/docs", status_code=301)
2024-10-21 13:34:07 +00:00
@app.get("/health")
async def health_check():
"""Health check endpoint."""
logger.info("Health check")
2024-11-24 22:00:58 +00:00
# browser: Chrome = await new_browser()
# browser.get("https://google.com")
# browser.stop()
2024-10-21 13:34:07 +00:00
return {"status": "ok"}
2024-07-24 13:57:40 +00:00
@app.post("/v1")
2024-11-24 22:00:58 +00:00
def read_item(request: LinkRequest):
2024-07-24 13:57:40 +00:00
"""Handle POST requests."""
2024-11-24 22:00:58 +00:00
start_time = int(time.time() * 1000)
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}")
2024-11-24 22:00:58 +00:00
response: LinkResponse
2024-09-13 18:48:31 +00:00
2024-11-24 22:00:58 +00:00
# start_time = int(time.time() * 1000)
with SB(uc=True, locale_code="en", test=False, xvfb=True, ad_block=True) as sb:
sb: BaseCase
sb.uc_open_with_reconnect(request.url)
sb.uc_gui_click_captcha()
logger.info(f"Got webpage: {request.url}")
sb.save_screenshot("screenshot.png")
logger.info(f"Got webpage: {request.url}")
2024-09-13 18:48:31 +00:00
2024-11-24 22:00:58 +00:00
response = LinkResponse(
message="Success",
solution=Solution(
userAgent=sb.get_user_agent(),
url=sb.get_current_url(),
status=200,
cookies=sb.get_cookies(),
headers={},
response=sb.get_page_source(),
),
startTimestamp=start_time,
)
2024-09-13 18:48:31 +00:00
2024-07-24 13:57:40 +00:00
return response
if __name__ == "__main__":
2024-07-25 00:06:56 +00:00
uvicorn.run(app, host="0.0.0.0", port=8191, log_level=LOG_LEVEL) # noqa: S104