diff --git a/main.py b/main.py index d8b7454..b2aee87 100644 --- a/main.py +++ b/main.py @@ -11,6 +11,7 @@ from fastapi.responses import RedirectResponse from src.models.requests import LinkRequest, LinkResponse from src.utils import logger from src.utils.browser import bypass_cloudflare, new_browser +from src.utils.consts import LOG_LEVEL from src.utils.extentions import download_extentions download_extentions() @@ -49,4 +50,4 @@ async def read_item(request: LinkRequest): if __name__ == "__main__": - uvicorn.run(app, host="0.0.0.0", port=8191) # noqa: S104 + uvicorn.run(app, host="0.0.0.0", port=8191, log_level=LOG_LEVEL) # noqa: S104 diff --git a/src/utils/__init__.py b/src/utils/__init__.py index 1f109bc..e0dde19 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -1,7 +1,8 @@ import logging -import os + +from src.utils.consts import LOG_LEVEL logger = logging.getLogger("uvicorn.error") -logger.setLevel(os.getenv("LOG_LEVEL") or logging.INFO) +logger.setLevel(LOG_LEVEL) if len(logger.handlers) == 0: logger.addHandler(logging.StreamHandler()) diff --git a/src/utils/browser.py b/src/utils/browser.py index b7108cc..b63244c 100644 --- a/src/utils/browser.py +++ b/src/utils/browser.py @@ -1,18 +1,16 @@ -import os from pathlib import Path import nodriver as webdriver from nodriver.core.element import Element from src.utils import logger +from src.utils.consts import CHALLENGE_TITLES from src.utils.extentions import downloaded_extentions -from src.utils.utils import CHALLENGE_TITLES async def new_browser(): config: webdriver.Config = webdriver.Config() config.sandbox = False - config.headless = bool(os.getenv("HEADLESS")) config.add_argument(f"--load-extension={','.join(downloaded_extentions)}") return await webdriver.start(config=config) @@ -23,6 +21,7 @@ async def bypass_cloudflare(page: webdriver.Tab): while True: await page await page.wait(0.5) + logger.debug(page.target.title) if page.target.title not in CHALLENGE_TITLES: return challenged if not challenged: diff --git a/src/utils/consts.py b/src/utils/consts.py index 2262b81..5862a79 100644 --- a/src/utils/consts.py +++ b/src/utils/consts.py @@ -1,5 +1,10 @@ +import logging +import os from pathlib import Path +LOG_LEVEL = os.getenv("LOG_LEVEL") or "INFO" +LOG_LEVEL = logging.getLevelName(LOG_LEVEL) + CHALLENGE_TITLES = [ # Cloudflare "Just a moment...", diff --git a/src/utils/utils.py b/src/utils/utils.py deleted file mode 100644 index 1d906fe..0000000 --- a/src/utils/utils.py +++ /dev/null @@ -1,24 +0,0 @@ -from venv import logger - -import nodriver as webdriver -from nodriver.core.element import Element - -from src.utils.consts import CHALLENGE_TITLES - - -async def new_browser(): - config: webdriver.Config = webdriver.Config() - config.sandbox = False - - return await webdriver.start(config=config) - - -async def bypass_cloudflare(page: webdriver.Tab): - while True: - await page - if page.target.title not in CHALLENGE_TITLES: - break - elem = await page.query_selector(".cf-turnstile-wrapper") - if isinstance(elem, Element): - logger.info(f"Clicking element: {elem}") - await elem.mouse_click()