Skip to content

Commit c533b27

Browse files
authored
Add website screenshot service (#17)
1 parent 900e97c commit c533b27

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

src/abstract_api/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .phone_validation import PhoneValidation
1111
from .timezone import Timezone
1212
from .vat import VAT
13+
from .website_screenshot import WebsiteScreenshot
1314

1415
__all__: Final[list[str]] = [
1516
"Avatars",
@@ -21,5 +22,6 @@
2122
"IPGeolocation",
2223
"PhoneValidation",
2324
"Timezone",
24-
"VAT"
25+
"VAT",
26+
"WebsiteScreenshot"
2527
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from typing import Final
2+
3+
from .website_screenshot import WebsiteScreenshot
4+
from .website_screenshot_response import WebsiteScreenshotResponse
5+
6+
__all__: Final[list[str]] = [
7+
"WebsiteScreenshot",
8+
"WebsiteScreenshotResponse"
9+
]
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from abstract_api.bases import FileResponse
2+
3+
4+
class WebsiteScreenshotResponse(FileResponse):
5+
"""Website screenshot service response."""

0 commit comments

Comments
 (0)