|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | +from typing import Tuple |
| 4 | +from PIL import Image |
| 5 | +from selenium import webdriver |
| 6 | +from selenium.webdriver.chrome.options import Options |
| 7 | +from selenium.webdriver.common.by import By |
| 8 | +from selenium.webdriver.support import expected_conditions as EC |
| 9 | +from selenium.webdriver.support.ui import WebDriverWait |
| 10 | + |
| 11 | + |
| 12 | +def setup_driver(cookie_value: str) -> webdriver.Chrome: |
| 13 | + """Set up a headless Chrome WebDriver and authenticate with a session cookie. |
| 14 | +
|
| 15 | + Args: |
| 16 | + cookie_value (str): The session cookie value for authentication. |
| 17 | +
|
| 18 | + Returns: |
| 19 | + webdriver.Chrome: Configured Chrome WebDriver instance. |
| 20 | + """ |
| 21 | + options = Options() |
| 22 | + options.add_argument('--headless=new') |
| 23 | + options.add_argument('--window-size=1920,1080') |
| 24 | + driver = webdriver.Chrome(options=options) |
| 25 | + driver.get('https://adventofcode.com') |
| 26 | + driver.add_cookie( |
| 27 | + {'name': 'session', 'value': cookie_value.lstrip('session='), 'domain': 'adventofcode.com'} |
| 28 | + ) |
| 29 | + return driver |
| 30 | + |
| 31 | + |
| 32 | +def crop_image( |
| 33 | + input_path: str, output_path: str, crop_box: Tuple[int, int, int, int] = (0, 0, 640, 621) |
| 34 | +) -> None: |
| 35 | + """Crop an image to the specified dimensions and save it to the output path. |
| 36 | +
|
| 37 | + Args: |
| 38 | + input_path (str): Path to the input image file. |
| 39 | + output_path (str): Path to save the cropped image. |
| 40 | + crop_box (Tuple[int, int, int, int], optional): |
| 41 | + The cropping box defined as (left, upper, right, lower). |
| 42 | + Defaults to (0, 0, 640, 621). |
| 43 | + """ |
| 44 | + with Image.open(input_path) as img: |
| 45 | + cropped = img.crop(crop_box) |
| 46 | + cropped.save(output_path) |
| 47 | + |
| 48 | + |
| 49 | +def take_screenshot(driver: webdriver.Chrome, url: str, selector: str, output_name: str) -> None: |
| 50 | + """Capture a screenshot of a web element specified by a CSS selector and crop it. |
| 51 | +
|
| 52 | + Args: |
| 53 | + driver (webdriver.Chrome): The WebDriver instance used to navigate and take screenshots. |
| 54 | + url (str): The URL of the web page to capture. |
| 55 | + selector (str): The CSS selector of the element to capture. |
| 56 | + output_name (str): Path to save the screenshot. |
| 57 | + """ |
| 58 | + driver.get(url) |
| 59 | + element = WebDriverWait(driver, 10).until( |
| 60 | + EC.presence_of_element_located((By.CSS_SELECTOR, selector)) |
| 61 | + ) |
| 62 | + element.screenshot(output_name) |
| 63 | + crop_image(output_name, output_name) |
| 64 | + |
| 65 | + |
| 66 | +def get_year() -> str: |
| 67 | + """Determine the year based on the parent folder name. |
| 68 | +
|
| 69 | + Returns: |
| 70 | + str: The year extracted from the folder name. |
| 71 | + """ |
| 72 | + folder_name = Path(__file__).parents[2].name |
| 73 | + return folder_name.split('aoc')[1] |
| 74 | + |
| 75 | + |
| 76 | +def main() -> None: |
| 77 | + """Main entry point for the script. |
| 78 | +
|
| 79 | + Sets up the environment, captures a screenshot, and ensures proper cleanup of resources. |
| 80 | + """ |
| 81 | + os.makedirs('screenshots', exist_ok=True) |
| 82 | + cookie = os.getenv('COOKIE') |
| 83 | + if not cookie: |
| 84 | + raise ValueError('COOKIE environment variable is not set.') |
| 85 | + |
| 86 | + driver = setup_driver(cookie) |
| 87 | + year = get_year() |
| 88 | + try: |
| 89 | + take_screenshot( |
| 90 | + driver, |
| 91 | + f'https://adventofcode.com/{year}', |
| 92 | + 'body > main > pre', |
| 93 | + 'screenshots/aoc-screenshot.png', |
| 94 | + ) |
| 95 | + finally: |
| 96 | + driver.quit() |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == '__main__': |
| 100 | + main() |
0 commit comments