1
1
import os
2
-
2
+ from pathlib import Path
3
+ from typing import Tuple
3
4
from PIL import Image
4
5
from selenium import webdriver
5
6
from selenium .webdriver .chrome .options import Options
8
9
from selenium .webdriver .support .ui import WebDriverWait
9
10
10
11
11
- def setup_driver (cookie_value ):
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
+ """
12
21
options = Options ()
13
22
options .add_argument ('--headless=new' )
14
23
options .add_argument ('--window-size=1920,1080' )
@@ -20,13 +29,32 @@ def setup_driver(cookie_value):
20
29
return driver
21
30
22
31
23
- def crop_image (input_path , output_path , crop_box = (0 , 0 , 640 , 621 )):
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
+ """
24
44
with Image .open (input_path ) as img :
25
45
cropped = img .crop (crop_box )
26
46
cropped .save (output_path )
27
47
28
48
29
- def take_screenshot (driver , url , selector , output_name ):
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
+ """
30
58
driver .get (url )
31
59
element = WebDriverWait (driver , 10 ).until (
32
60
EC .presence_of_element_located ((By .CSS_SELECTOR , selector ))
@@ -35,14 +63,32 @@ def take_screenshot(driver, url, selector, output_name):
35
63
crop_image (output_name , output_name )
36
64
37
65
38
- def main ():
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
+ """
39
81
os .makedirs ('screenshots' , exist_ok = True )
40
82
cookie = os .getenv ('COOKIE' )
83
+ if not cookie :
84
+ raise ValueError ('COOKIE environment variable is not set.' )
85
+
41
86
driver = setup_driver (cookie )
87
+ year = get_year ()
42
88
try :
43
89
take_screenshot (
44
90
driver ,
45
- 'https://adventofcode.com/2024 ' ,
91
+ f 'https://adventofcode.com/{ year } ' ,
46
92
'body > main > pre' ,
47
93
'screenshots/aoc-screenshot.png' ,
48
94
)
0 commit comments