FetchScreenNode captures screenshots from a given URL and stores the image data as bytes.
| 10 | |
| 11 | |
| 12 | class FetchScreenNode(BaseNode): |
| 13 | """ |
| 14 | FetchScreenNode captures screenshots from a given URL and stores the image data as bytes. |
| 15 | """ |
| 16 | |
| 17 | def __init__( |
| 18 | self, |
| 19 | input: str, |
| 20 | output: List[str], |
| 21 | node_config: Optional[dict] = None, |
| 22 | node_name: str = "FetchScreen", |
| 23 | ): |
| 24 | super().__init__(node_name, "node", input, output, 2, node_config) |
| 25 | self.url = node_config.get("link") |
| 26 | |
| 27 | def execute(self, state: dict) -> dict: |
| 28 | """ |
| 29 | Captures screenshots from the input URL and stores them in the state dictionary as bytes. |
| 30 | """ |
| 31 | self.logger.info(f"--- Executing {self.node_name} Node ---") |
| 32 | |
| 33 | with sync_playwright() as p: |
| 34 | browser = p.chromium.launch() |
| 35 | page = browser.new_page() |
| 36 | page.goto(self.url) |
| 37 | |
| 38 | viewport_height = page.viewport_size["height"] |
| 39 | |
| 40 | screenshot_counter = 1 |
| 41 | |
| 42 | screenshot_data_list = [] |
| 43 | |
| 44 | def capture_screenshot(scroll_position, counter): |
| 45 | page.evaluate(f"window.scrollTo(0, {scroll_position});") |
| 46 | screenshot_data = page.screenshot() |
| 47 | screenshot_data_list.append(screenshot_data) |
| 48 | |
| 49 | capture_screenshot(0, screenshot_counter) |
| 50 | screenshot_counter += 1 |
| 51 | capture_screenshot(viewport_height, screenshot_counter) |
| 52 | |
| 53 | browser.close() |
| 54 | |
| 55 | state["link"] = self.url |
| 56 | state["screenshots"] = screenshot_data_list |
| 57 | |
| 58 | return state |