|
| 1 | +from abstract_api.bases import BaseService |
| 2 | +from abstract_api.exceptions import ResponseParseError |
| 3 | + |
| 4 | +from .website_screenshot_response import WebsiteScreenshotResponse |
| 5 | + |
| 6 | + |
| 7 | +class WebsiteScreenshot(BaseService): |
| 8 | + """AbstractAPI website screenshot service. |
| 9 | +
|
| 10 | + Used to request a screenshot of a webpage in a given URL. |
| 11 | +
|
| 12 | + Attributes: |
| 13 | + _subdomain: Website screenshot service subdomain. |
| 14 | + """ |
| 15 | + _subdomain: str = "screenshot" |
| 16 | + |
| 17 | + def capture( |
| 18 | + self, |
| 19 | + url: str, |
| 20 | + capture_full_page: bool | None = None, |
| 21 | + width: int | None = None, |
| 22 | + height: int | None = None, |
| 23 | + delay: int | None = None, |
| 24 | + css_injection: str | None = None, |
| 25 | + user_agent: str | None = None, |
| 26 | + export_format: str | None = None |
| 27 | + ) -> WebsiteScreenshotResponse: |
| 28 | + """Captures a screenshot of a webpage in the given URL. |
| 29 | +
|
| 30 | + Args: |
| 31 | + url: The URL to get the screenshot of. Note that this parameter |
| 32 | + should include the full HTTP Protocol (http:// or https://). |
| 33 | + capture_full_page: If true the request will capture the entire |
| 34 | + height and width of the page. Defaults to True. |
| 35 | + width: The width in pixels of the view port to use to capture |
| 36 | + the image. |
| 37 | + height: The height in pixels of the view port to use to capture |
| 38 | + the image. |
| 39 | + delay: The time in seconds to wait between loading the page and |
| 40 | + taking the screenshot. |
| 41 | + css_injection: A CSS string to inject into the website before |
| 42 | + capturing the image. |
| 43 | + user_agent: The User Agent to use when capturing the screenshot. |
| 44 | + export_format: The image format to use for the screenshot. |
| 45 | + Can be jpeg or png, and defaults to jpeg. |
| 46 | +
|
| 47 | + Returns: |
| 48 | + WebsiteScreenshotResponse representing API call response. |
| 49 | + """ |
| 50 | + response = self._service_request( |
| 51 | + url=url, |
| 52 | + capture_full_page=capture_full_page, |
| 53 | + width=width, |
| 54 | + height=height, |
| 55 | + delay=delay, |
| 56 | + css_injection=css_injection, |
| 57 | + user_agent=user_agent, |
| 58 | + export_format=export_format |
| 59 | + ) |
| 60 | + |
| 61 | + try: |
| 62 | + website_screenshot_response = WebsiteScreenshotResponse( |
| 63 | + response=response |
| 64 | + ) |
| 65 | + except Exception as e: |
| 66 | + raise ResponseParseError( |
| 67 | + "Failed to parse response as WebsiteScreenshotResponse" |
| 68 | + ) from e |
| 69 | + |
| 70 | + return website_screenshot_response |
0 commit comments