Skip to content

Commit a5bdf89

Browse files
committed
tinkering with the actions
1 parent 6a0dcaf commit a5bdf89

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

.github/workflows/periodic_update.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
uses: astral-sh/setup-uv@v5
4646

4747
- name: Install the project
48-
run: uv sync --group actions --all-extras --dev
48+
run: uv pip install -r requirements.txt
4949

5050
- name: Install Chrome
5151
uses: browser-actions/setup-chrome@v1
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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': 'CW_COOKIE', 'value': cookie_value.lstrip('CW_COOKIE='), 'domain': 'codewars.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 main() -> None:
67+
"""Main entry point for the script.
68+
69+
Sets up the environment, captures a screenshot, and ensures proper cleanup of resources.
70+
"""
71+
os.makedirs('screenshots', exist_ok=True)
72+
cookie = os.getenv('CW_COOKIE')
73+
if not cookie:
74+
raise ValueError('COOKIE environment variable is not set.')
75+
76+
driver = setup_driver(cookie)
77+
78+
try:
79+
take_screenshot(
80+
driver,
81+
f'https://codewars.com',
82+
'body > main > pre',
83+
'screenshots/aoc-screenshot.png',
84+
)
85+
finally:
86+
driver.quit()
87+
88+
89+
if __name__ == '__main__':
90+
main()

requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ python-dotenv~=1.0.0
22
requests-html
33
lxml[html_clean]
44
requests
5-
tqdm
5+
tqdm
6+
pillow>=11.0.0
7+
selenium>=4.27.1

0 commit comments

Comments
 (0)