Skip to content

Commit 1a41f3c

Browse files
committed
year dynamically from folder, no artifact needed
1 parent 30edfbd commit 1a41f3c

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

.github/scripts/take_screenshot.py

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
2-
2+
from pathlib import Path
3+
from typing import Tuple
34
from PIL import Image
45
from selenium import webdriver
56
from selenium.webdriver.chrome.options import Options
@@ -8,7 +9,15 @@
89
from selenium.webdriver.support.ui import WebDriverWait
910

1011

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+
"""
1221
options = Options()
1322
options.add_argument('--headless=new')
1423
options.add_argument('--window-size=1920,1080')
@@ -20,13 +29,32 @@ def setup_driver(cookie_value):
2029
return driver
2130

2231

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+
"""
2444
with Image.open(input_path) as img:
2545
cropped = img.crop(crop_box)
2646
cropped.save(output_path)
2747

2848

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+
"""
3058
driver.get(url)
3159
element = WebDriverWait(driver, 10).until(
3260
EC.presence_of_element_located((By.CSS_SELECTOR, selector))
@@ -35,14 +63,32 @@ def take_screenshot(driver, url, selector, output_name):
3563
crop_image(output_name, output_name)
3664

3765

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+
"""
3981
os.makedirs('screenshots', exist_ok=True)
4082
cookie = os.getenv('COOKIE')
83+
if not cookie:
84+
raise ValueError('COOKIE environment variable is not set.')
85+
4186
driver = setup_driver(cookie)
87+
year = get_year()
4288
try:
4389
take_screenshot(
4490
driver,
45-
'https://adventofcode.com/2024',
91+
f'https://adventofcode.com/{year}',
4692
'body > main > pre',
4793
'screenshots/aoc-screenshot.png',
4894
)

.github/workflows/update_readme.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@ jobs:
3838
run: |
3939
uv run .github/scripts/take_screenshot.py
4040
41-
- name: Upload screenshot artifact
42-
uses: actions/upload-artifact@v4
43-
with:
44-
name: webpage-screenshots
45-
path: screenshots/
46-
4741
- name: Commit and Push Changes
4842
uses: stefanzweifel/git-auto-commit-action@v5
4943
with:

0 commit comments

Comments
 (0)