Skip to content

Commit 864b017

Browse files
committed
added action to update readme with calendar screenshot
1 parent 093391b commit 864b017

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

.github/scripts/take_screenshot.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import os
2+
3+
from PIL import Image
4+
from selenium import webdriver
5+
from selenium.webdriver.chrome.options import Options
6+
from selenium.webdriver.common.by import By
7+
from selenium.webdriver.support import expected_conditions as EC
8+
from selenium.webdriver.support.ui import WebDriverWait
9+
10+
11+
def setup_driver(cookie_value):
12+
options = Options()
13+
options.add_argument('--headless=new')
14+
options.add_argument('--window-size=1920,1080')
15+
driver = webdriver.Chrome(options=options)
16+
driver.get('https://adventofcode.com')
17+
driver.add_cookie(
18+
{'name': 'session', 'value': cookie_value.lstrip('session='), 'domain': 'adventofcode.com'}
19+
)
20+
return driver
21+
22+
23+
def crop_image(input_path, output_path, crop_box=(0, 0, 640, 621)):
24+
with Image.open(input_path) as img:
25+
cropped = img.crop(crop_box)
26+
cropped.save(output_path)
27+
28+
29+
def take_screenshot(driver, url, selector, output_name):
30+
driver.get(url)
31+
element = WebDriverWait(driver, 10).until(
32+
EC.presence_of_element_located((By.CSS_SELECTOR, selector))
33+
)
34+
element.screenshot(output_name)
35+
crop_image(output_name, output_name)
36+
37+
38+
def main():
39+
os.makedirs('screenshots', exist_ok=True)
40+
cookie = os.getenv('COOKIE')
41+
driver = setup_driver(cookie)
42+
try:
43+
take_screenshot(
44+
driver,
45+
'https://adventofcode.com/2024',
46+
'body > main > pre',
47+
'screenshots/aoc-screenshot.png',
48+
)
49+
finally:
50+
driver.quit()
51+
52+
53+
if __name__ == '__main__':
54+
main()

.github/workflows/update_readme.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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: Upload screenshot artifact
42+
uses: actions/upload-artifact@v4
43+
with:
44+
name: webpage-screenshots
45+
path: screenshots/
46+
47+
- name: Commit and Push Changes
48+
uses: stefanzweifel/git-auto-commit-action@v5
49+
with:
50+
commit_message: "Update screenshot in README"
51+
file_pattern: screenshots/*.png

README.md

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

44
https://adventofcode.com/2024
55

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

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

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ dev = [
2020
"pre-commit>=4.0.1",
2121
"ruff>=0.8.1",
2222
]
23+
actions = [
24+
"pillow>=11.0.0",
25+
"selenium>=4.27.1",
26+
]
2327

2428
# --- tools configuration ---
2529

0 commit comments

Comments
 (0)