|
1 |
| -import os |
2 |
| -import time |
3 |
| - |
4 | 1 | from selenium import webdriver
|
5 | 2 | from selenium.webdriver.chrome.options import Options
|
6 |
| -from selenium.webdriver.chrome.service import Service |
7 |
| -from selenium.webdriver.support.wait import WebDriverWait |
8 |
| - |
9 | 3 | from selenium.webdriver.common.by import By
|
| 4 | +from selenium.webdriver.support.ui import WebDriverWait |
10 | 5 | from selenium.webdriver.support import expected_conditions as EC
|
| 6 | +from PIL import Image |
| 7 | +import os |
11 | 8 |
|
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...') |
30 | 9 |
|
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 |
35 | 18 |
|
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 |
| - }) |
44 | 19 |
|
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) |
50 | 24 |
|
51 | 25 |
|
52 | 26 | 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) |
78 | 34 |
|
79 | 35 |
|
80 | 36 | def main():
|
81 |
| - print('Starting screenshot process...') |
82 |
| - driver = None |
83 |
| - |
84 |
| - # Get cookie from environment variable |
85 | 37 | cookie = os.getenv('COOKIE')
|
86 |
| - if not cookie: |
87 |
| - raise ValueError('COOKIE environment variable not set') |
88 |
| - |
| 38 | + driver = setup_driver(cookie) |
89 | 39 | try:
|
90 |
| - driver = setup_driver(cookie) |
91 | 40 | 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', |
96 | 45 | )
|
97 |
| - |
98 |
| - except Exception as e: |
99 |
| - print(f'Fatal error in main: {e!s}') |
100 |
| - raise |
101 | 46 | finally:
|
102 |
| - if driver: |
103 |
| - print('Cleaning up driver...') |
104 |
| - driver.quit() |
| 47 | + driver.quit() |
105 | 48 |
|
106 | 49 |
|
107 | 50 | if __name__ == '__main__':
|
|
0 commit comments