Skip to content

Commit 48912e3

Browse files
committed
added screenshot action
1 parent 4e61695 commit 48912e3

File tree

6 files changed

+526
-0
lines changed

6 files changed

+526
-0
lines changed

.github/scripts/take_screenshot.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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()

.github/workflows/update_readme.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Update Screenshot in README
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
workflow_dispatch:
9+
10+
jobs:
11+
update-readme:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- name: Install uv
17+
uses: astral-sh/setup-uv@v5
18+
19+
- name: Install the project
20+
run: uv sync --group actions --all-extras --dev
21+
22+
- name: Install Chrome
23+
uses: browser-actions/setup-chrome@v1
24+
with:
25+
chrome-version: stable
26+
27+
- name: Install Chrome WebDriver
28+
uses: nanasess/setup-chromedriver@v2
29+
30+
- name: Start Chrome Driver
31+
run: |
32+
chromedriver --version
33+
chrome --version
34+
35+
- name: Take Screenshot
36+
env:
37+
COOKIE: ${{ secrets.COOKIE }}
38+
run: |
39+
uv run .github/scripts/take_screenshot.py
40+
41+
- name: Commit and Push Changes
42+
uses: stefanzweifel/git-auto-commit-action@v5
43+
with:
44+
commit_message: "Update screenshot in README"
45+
file_pattern: screenshots/*.png

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.13

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ advent of code 2023
33

44
https://adventofcode.com/2023
55

6+
![Advent of Code Screenshot](screenshots/aoc-screenshot.png)
7+
68
### about
79

810
https://adventofcode.com/2023/about

pyproject.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[project]
2+
name = "aoc2023"
3+
version = "0.1.0"
4+
description = "Add your description here"
5+
readme = "README.md"
6+
requires-python = ">=3.13"
7+
dependencies = [
8+
"networkx>=3.4.2",
9+
"pytest~=7.4.3",
10+
"shapely~=2.0.2",
11+
]
12+
13+
[tool.uv.sources]
14+
support = { path = "support" }
15+
16+
[dependency-groups]
17+
actions = [
18+
"pillow>=11.1.0",
19+
"selenium>=4.27.1",
20+
]

0 commit comments

Comments
 (0)