Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include undetected_chromedriver binary in Docker image #110

Merged
merged 3 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ COPY docker/entrypoint /

COPY . /python-build
RUN python3 -m pip install /python-build && rm -rf /python-build
RUN safeway-coupons-init-chromedriver

ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["/entrypoint"]
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ responses = "*"

[tool.poetry.scripts]
safeway-coupons = "safeway_coupons.app:main"
safeway-coupons-init-chromedriver = "safeway_coupons.chrome_driver:init"

[tool.poetry-dynamic-versioning]
enable = true
Expand Down
62 changes: 62 additions & 0 deletions safeway_coupons/chrome_driver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import contextlib
import subprocess
import sys
from pathlib import Path
from typing import Iterator

import undetected_chromedriver as uc # type: ignore

CHROMEDRIVER_PATH = (
Path.home()
/ ".local"
/ "share"
/ "undetected_chromedriver"
/ "undetected_chromedriver"
)


class ChromeDriverDoesNotExist(Exception):
pass


@contextlib.contextmanager
def chrome_driver(headless: bool = True) -> Iterator[uc.Chrome]:
options = uc.ChromeOptions()
options.headless = headless
for option in [
"--incognito",
"--no-sandbox",
"--disable-extensions",
"--disable-application-cache",
"--disable-gpu",
"--disable-setuid-sandbox",
"--disable-dev-shm-usage",
]:
options.add_argument(option)
if headless:
options.add_argument("--headless=new")
driver = uc.Chrome(options=options)
yield driver
driver.quit()


def chrome_driver_version() -> str:
if not CHROMEDRIVER_PATH.is_file():
raise ChromeDriverDoesNotExist(

Check warning on line 45 in safeway_coupons/chrome_driver.py

View check run for this annotation

Codecov / codecov/patch

safeway_coupons/chrome_driver.py#L44-L45

Added lines #L44 - L45 were not covered by tests
f"Error: {CHROMEDRIVER_PATH} does not exist"
)
cmd = [str(CHROMEDRIVER_PATH), "--version"]
print(f"+ {' '.join(cmd)}", file=sys.stderr)
result = subprocess.run(cmd, capture_output=True)
return result.stdout.decode()

Check warning on line 51 in safeway_coupons/chrome_driver.py

View check run for this annotation

Codecov / codecov/patch

safeway_coupons/chrome_driver.py#L48-L51

Added lines #L48 - L51 were not covered by tests


def init() -> None:
with contextlib.suppress(ChromeDriverDoesNotExist):
print(chrome_driver_version())
return
print("Initializing Chrome Driver")
with chrome_driver() as driver:
print("Connect to example.com")
driver.get("https://example.com")
print(chrome_driver_version())

Check warning on line 62 in safeway_coupons/chrome_driver.py

View check run for this annotation

Codecov / codecov/patch

safeway_coupons/chrome_driver.py#L55-L62

Added lines #L55 - L62 were not covered by tests
24 changes: 5 additions & 19 deletions safeway_coupons/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from selenium.webdriver.support.wait import WebDriverWait

from .accounts import Account
from .chrome_driver import chrome_driver
from .errors import AuthenticationFailure


Expand All @@ -32,8 +33,8 @@ def __init__(

class BaseSession:
USER_AGENT = (
"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:103.0) "
"Gecko/20100101 Firefox/103.0"
"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:122.0) "
"Gecko/20100101 Firefox/122.0"
)

@property
Expand Down Expand Up @@ -64,23 +65,9 @@ def __init__(self, account: Account, debug_dir: Optional[Path]) -> None:

@contextlib.contextmanager
def _chrome_driver(self, headless: bool = True) -> Iterator[uc.Chrome]:
options = uc.ChromeOptions()
options.headless = headless
for option in [
"--incognito",
"--no-sandbox",
"--disable-extensions",
"--disable-application-cache",
"--disable-gpu",
"--disable-setuid-sandbox",
"--disable-dev-shm-usage",
]:
options.add_argument(option)
if headless:
options.add_argument("--headless=new")
driver = uc.Chrome(options=options)
try:
yield driver
with chrome_driver(headless=headless) as driver:
yield driver
except WebDriverException as e:
attachments: List[Path] = []
if self.debug_dir:
Expand All @@ -91,7 +78,6 @@ def _chrome_driver(self, headless: bool = True) -> Iterator[uc.Chrome]:
raise ExceptionWithAttachments(
f"[{type(e).__name__}] {e}", attachments=attachments
) from e
driver.quit()

@staticmethod
def _sign_in_success(driver: uc.Chrome) -> bool:
Expand Down
Loading