Skip to content

Commit 6e45e0c

Browse files
committed
testing actions
1 parent ba7eeab commit 6e45e0c

File tree

4 files changed

+35
-100
lines changed

4 files changed

+35
-100
lines changed

.github/scripts/take_screenshot.py

Lines changed: 28 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,50 @@
1-
import os
2-
import time
3-
41
from selenium import webdriver
52
from selenium.webdriver.chrome.options import Options
6-
from selenium.webdriver.chrome.service import Service
7-
from selenium.webdriver.support.wait import WebDriverWait
8-
93
from selenium.webdriver.common.by import By
4+
from selenium.webdriver.support.ui import WebDriverWait
105
from selenium.webdriver.support import expected_conditions as EC
6+
from PIL import Image
7+
import os
118

12-
def setup_driver(cookie_value):
13-
print('Setting up Chrome driver...')
14-
chrome_options = Options()
15-
16-
# Basic Chrome options
17-
chrome_options.add_argument('--headless=new')
18-
chrome_options.add_argument('--no-sandbox')
19-
chrome_options.add_argument('--disable-dev-shm-usage')
20-
chrome_options.add_argument('--window-size=1920,1080')
21-
22-
# Add headers
23-
chrome_options.add_argument('--accept-encoding=gzip, deflate, br, zstd')
24-
chrome_options.add_argument('--accept-language=en,cs;q=0.9')
25-
chrome_options.add_argument(
26-
'--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
27-
)
28-
29-
print('Chrome options configured, initializing driver...')
309

31-
try:
32-
service = Service()
33-
driver = webdriver.Chrome(service=service, options=chrome_options)
34-
driver.set_page_load_timeout(30)
10+
def setup_driver(cookie_value):
11+
options = Options()
12+
options.add_argument('--headless=new')
13+
options.add_argument('--window-size=1920,1080')
14+
driver = webdriver.Chrome(options=options)
15+
driver.get('https://adventofcode.com')
16+
driver.add_cookie({'name': 'session', 'value': cookie_value, 'domain': 'adventofcode.com'})
17+
return driver
3518

36-
# Set up cookie before any navigation
37-
print('Setting up cookie...')
38-
driver.get("https://adventofcode.com")
39-
driver.add_cookie({
40-
'name': 'session',
41-
'value': cookie_value.lstrip('session='),
42-
'domain': 'adventofcode.com',
43-
})
4419

45-
print('Chrome driver initialized successfully')
46-
return driver
47-
except Exception as e:
48-
print(f'Failed to initialize Chrome driver: {e!s}')
49-
raise
20+
def crop_image(input_path, output_path, crop_box=(0, 0, 640, 621)):
21+
with Image.open(input_path) as img:
22+
cropped = img.crop(crop_box)
23+
cropped.save(output_path)
5024

5125

5226
def take_screenshot(driver, url, selector, output_name):
53-
print(f'Attempting to take screenshot of {selector} at {url}')
54-
os.makedirs('screenshots', exist_ok=True)
55-
56-
try:
57-
print(f'Navigating to target URL: {url}')
58-
driver.get(url)
59-
time.sleep(3) # Give the page time to load
60-
61-
print('Waiting for element...')
62-
element = WebDriverWait(driver, 15).until(
63-
EC.presence_of_element_located((By.CSS_SELECTOR, selector))
64-
)
65-
66-
print('Element found, taking screenshot...')
67-
driver.execute_script('arguments[0].scrollIntoView();', element)
68-
time.sleep(2)
69-
element.screenshot(f'screenshots/{output_name}.png')
70-
print(f'Screenshot saved as screenshots/{output_name}.png')
71-
72-
except Exception as e:
73-
print(f'Error during screenshot process: {e!s}')
74-
print(f'Current URL: {driver.current_url}')
75-
print('Page source:')
76-
print(driver.page_source[:1000]) # Print first 1000 characters of source
77-
raise
27+
driver.get(url)
28+
element = WebDriverWait(driver, 10).until(
29+
EC.presence_of_element_located((By.CSS_SELECTOR, selector))
30+
)
31+
temp_output = output_name # Directly save the temp screenshot as the final output
32+
element.screenshot(temp_output)
33+
crop_image(temp_output, output_name)
7834

7935

8036
def main():
81-
print('Starting screenshot process...')
82-
driver = None
83-
84-
# Get cookie from environment variable
8537
cookie = os.getenv('COOKIE')
86-
if not cookie:
87-
raise ValueError('COOKIE environment variable not set')
88-
38+
driver = setup_driver(cookie)
8939
try:
90-
driver = setup_driver(cookie)
9140
take_screenshot(
92-
driver=driver,
93-
url='https://adventofcode.com/2024',
94-
selector='body > main > pre',
95-
output_name='aoc-content',
41+
driver,
42+
'https://adventofcode.com/2024',
43+
'body > main > pre',
44+
'aoc-screenshot.png',
9645
)
97-
98-
except Exception as e:
99-
print(f'Fatal error in main: {e!s}')
100-
raise
10146
finally:
102-
if driver:
103-
print('Cleaning up driver...')
104-
driver.quit()
47+
driver.quit()
10548

10649

10750
if __name__ == '__main__':

.github/scripts/update_readme.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

.github/workflows/update_readme.yml

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,15 @@ jobs:
3535
COOKIE: ${{ secrets.COOKIE }}
3636
run: |
3737
uv run .github/scripts/take_screenshot.py
38-
tree
3938
4039
- name: Upload screenshot artifact
4140
uses: actions/upload-artifact@v4
4241
with:
4342
name: webpage-screenshots
4443
path: screenshots/
45-
if-no-files-found: error # This will help us know if screenshots weren't created
46-
47-
- name: Update README
48-
run: |
49-
uv run .github/scripts/update_readme.py
50-
#
51-
# - name: Commit and Push Changes
52-
# run: |
53-
# git config user.name "GitHub Actions"
54-
# git config user.email "actions@github.com"
55-
# git add README.md screenshot.png
56-
# git commit -m "Update screenshot in README"
57-
# git push
5844

45+
- name: Commit and Push Changes
46+
uses: stefanzweifel/git-auto-commit-action@v4
47+
with:
48+
commit_message: "Update screenshot in README"
49+
file_pattern: screenshots/aoc-screenshot.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

0 commit comments

Comments
 (0)