Skip to content

Commit

Permalink
Merge pull request #110 from smkent/chrome-driver
Browse files Browse the repository at this point in the history
Include undetected_chromedriver binary in Docker image
  • Loading branch information
smkent committed Jan 28, 2024
2 parents 096707a + 8375843 commit 369a4ff
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 19 deletions.
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(
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()


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())
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

0 comments on commit 369a4ff

Please sign in to comment.