Browshot 1.32.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,125 @@
1
+ Metadata-Version: 2.4
2
+ Name: Browshot
3
+ Version: 1.32.0
4
+ Summary: Client to interact with Browshot
5
+ Home-page: https://github.com/juliensobrier/browshot-python
6
+ Author: Julien Sobrier
7
+ Author-email: julien@sobrier.net
8
+ License: Apache-2.0
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: Environment :: Console
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Intended Audience :: Information Technology
13
+ Classifier: License :: OSI Approved :: Apache Software License
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3 :: Only
17
+ Requires-Python: >=3.9
18
+ Description-Content-Type: text/plain
19
+ Requires-Dist: requests
20
+ Dynamic: author
21
+ Dynamic: author-email
22
+ Dynamic: classifier
23
+ Dynamic: description
24
+ Dynamic: description-content-type
25
+ Dynamic: home-page
26
+ Dynamic: license
27
+ Dynamic: requires-dist
28
+ Dynamic: requires-python
29
+ Dynamic: summary
30
+
31
+ Browshot version 1.32.0
32
+ ================================
33
+
34
+ Browshot (https://browshot.com/) is a web service to easily make screenshots of web pages in any screen size, as any device: iPhone, iPad, Android, PC, etc. Browshot has full Flash, JavaScript, CSS, & HTML5 support.
35
+
36
+ The latest API version is detailed at https://browshot.com/api/documentation. browshot follows the API documentation very closely: the function names are similar to the URLs used (screenshot/create becomes screenshot_create(), instance/list becomes instance_list(), etc.), the request arguments are exactly the same, etc.
37
+
38
+ The library version matches closely the API version it handles: browshot 1.0.0 is the first release for the API 1.0, browshot 1.1.1 is the second release for the API 1.1, etc.
39
+
40
+ browshot can handle most the API updates within the same major version, e.g. browshot 1.0.0 should be compatible with the API 1.1 or 1.2.
41
+
42
+ You can install the library from PyPI: https://pypi.python.org/pypi/Browshot
43
+
44
+ The source code can be found on github at https://github.com/juliensobrier/browshot-python
45
+
46
+
47
+
48
+ INSTALLATION
49
+
50
+ python3 -m pip install .
51
+
52
+
53
+ REQUIREMENTS
54
+
55
+ Python 3.9 or newer
56
+ requests (installed automatically by pip)
57
+
58
+ TESTS
59
+
60
+ python3 -m unittest discover -s tests -p '*_test.py'
61
+
62
+ LIVE TESTS
63
+
64
+ Set BROWSHOT_API_KEY to enable read-only checks with your account.
65
+ Set BROWSHOT_SCREENSHOT_ID to a finished capture and BROWSHOT_RETINA_ID to
66
+ a finished capture with retina and ad blocking. For artifact checks, set
67
+ BROWSHOT_PDF_ID, BROWSHOT_WEBM_ID, BROWSHOT_MP4_ID, and BROWSHOT_GIF_ID to
68
+ owned, finished captures of each format.
69
+
70
+ API 1.32
71
+
72
+ from browshot import BrowshotClient
73
+ import os
74
+
75
+ client = BrowshotClient(os.environ['BROWSHOT_API_KEY'])
76
+ capture = client.screenshot_create('https://example.com/', {
77
+ 'instance_id': 373,
78
+ 'pdf': 1,
79
+ 'pdf_format': 'a4',
80
+ 'pdf_margin': '10mm',
81
+ 'device_scale': 2,
82
+ 'hide_ads': 1,
83
+ })
84
+
85
+ Poll screenshot_info(capture['id']) until status is 'finished'. Download with
86
+ screenshot_pdf(capture['id']) or save with
87
+ screenshot_pdf_file(capture['id'], 'example.pdf').
88
+
89
+ PDF settings include pdf_format (letter, a4, or legal), pdf_background,
90
+ pdf_landscape, pdf_one_page, pdf_margin, and the per-side pdf_margin_top,
91
+ pdf_margin_bottom, pdf_margin_left, and pdf_margin_right.
92
+
93
+ Request a short video or animated GIF with the same capture method.
94
+
95
+ recording = client.screenshot_create('https://example.com/', {
96
+ 'instance_id': 373,
97
+ 'video': 1,
98
+ 'video_format': 'gif',
99
+ 'video_duration': 5,
100
+ 'video_scale': 0.5,
101
+ })
102
+
103
+ After the capture finishes, use screenshot_video(recording['id']) to get the
104
+ bytes or screenshot_video_file(recording['id'], 'example.gif') to save them.
105
+ Accepted formats are webm (default), mp4, and gif. Set video_duration to 1–30
106
+ seconds, video_quality to 0–63, video_fps to 1–30, and video_scale to 0.1–1.
107
+ GIF uses 20 fps. An MP4 request can produce WebM where MP4 encoding is unavailable.
108
+
109
+ The download methods return bytes or b'' on failure. File helpers use the
110
+ supplied path in an existing directory and return that path, or '' on failure.
111
+ Downloads preserve the returned bytes.
112
+
113
+ Choose a capture country through instance_id. The IDs below are in order
114
+ Chrome, Firefox, iPhone 12, and Samsung S20.
115
+
116
+ France 373 374 375 376
117
+ India 377 378 379 380
118
+ Finland 381 382 383 384
119
+ United Kingdom 385 386 387 388
120
+
121
+ Each new instance costs one credit before options. PDF adds one credit per
122
+ capture. Recording adds one credit per started five seconds of requested
123
+ duration. Both require a paid Chrome-based instance, and video requires
124
+ device_scale=1. Retina (device_scale=2), ad blocking (hide_ads=1), and downloads
125
+ add no credits.
@@ -0,0 +1,5 @@
1
+ browshot.py,sha256=NYts4e_Q3tfWZ9nh-KOxjVYa27eD7_kOa-rhLxY7tWA,17291
2
+ browshot-1.32.0.dist-info/METADATA,sha256=_t-dP1vV2n4UERAuzHlzwR3TBHLWMqmD8M9u9aa_2u8,4765
3
+ browshot-1.32.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
4
+ browshot-1.32.0.dist-info/top_level.txt,sha256=BdOjkfAD21neLb9e9Al9mJTJ4p6vZSzasZf2b0hZ4NY,9
5
+ browshot-1.32.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ browshot
browshot.py ADDED
@@ -0,0 +1,396 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Copyright 2012 Julien Sobrier
3
+ # All Rights Reserved.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License. You may obtain
7
+ # a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations
15
+ # under the License.
16
+
17
+ """ Version 1.32.0
18
+
19
+ Python module for Browshot (http://www.browshot.com/), a web service to create website screenshots.
20
+
21
+ Browshot (http://www.browshot.com/) is a web service to easily make screenshots of web pages in any screen size, as any device: iPhone, iPad, Android, Nook, PC, etc. Browshot has full Flash, JavaScript, CSS, & HTML5 support.
22
+
23
+ The latest API version is detailed at http://browshot.com/api/documentation. browshot.py follows the API documentation very closely: the function names are similar to the URLs used (screenshot/create becomes screenshot_create(), instance/list becomes instance_list(), etc.), the request arguments are exactly the same, etc.
24
+
25
+ The library version matches closely the API version it handles: browshot.py 1.0.0 is the first release for the API 1.0, browshot.py 1.1.1 is the second release for the API 1.1, etc.
26
+
27
+ browshot.py can handle most the API updates within the same major version, e.g. browshot.py 1.0.0 should be compatible with the API 1.1 or 1.2. """
28
+
29
+
30
+ from http.client import HTTPException
31
+ import json
32
+ from os import PathLike
33
+ import re
34
+ from urllib.error import HTTPError, URLError
35
+ from urllib.parse import quote_plus
36
+ from urllib.request import urlopen
37
+
38
+ import requests
39
+
40
+
41
+ class BrowshotClient(object):
42
+ def __init__(self, key='', debug=0, base='https://api.browshot.com/api/v1/'):
43
+ """ Create a new BrowshotClient object. You must pass your API key (go to you Dashboard to find your API key, https://browshot.com/dashboard).
44
+
45
+ Arguments:
46
+ key: API key.
47
+ debug: Set to 1 to print debug output to the standard output. 0 (disabled) by default.
48
+ base: Base URL for all API requests. You should use the default base provided by the library. Be careful if you decide to use HTTP instead of HTTPS as your API key could be sniffed and your account could be used without your consent.
49
+ """
50
+ self.key = key
51
+ self.base = base
52
+ self.debug = debug
53
+
54
+ def api_version(self):
55
+ """ Return the API version handled by the library. Note that this library can usually handle new arguments in requests without requiring an update. """
56
+ return '1.32'
57
+
58
+ def simple(self, url='', parameters={}):
59
+ """ Retrieve a screenshot in one function.
60
+
61
+ Note: by default, screenshots are cached for 24 hours. You can tune this valu with the cache=X parameter.
62
+
63
+ Arguments:
64
+ See https://browshot.com/api/documentation#simple for the full list of possible arguments.
65
+ url (Required): URL of the screenshot
66
+
67
+ Returns {'code': <code>, 'png': <content>}
68
+ <code>: 200 if successful
69
+ <content>: PNG file
70
+ """
71
+ parameters = dict(parameters, url=url)
72
+ uri = self.make_url('simple', parameters)
73
+ try:
74
+ with urlopen(uri) as response:
75
+ return {'code': 200, 'png': response.read()}
76
+ except HTTPError as error:
77
+ with error:
78
+ return {'code': error.code, 'png': ''}
79
+ except Exception:
80
+ return {'code': 400, 'png': ''}
81
+
82
+
83
+
84
+ def simple_file(self, url='', file='', parameters={}):
85
+ """ Retrieve a screenshot, or a thumbnail, and save it to a fil in one functione.
86
+
87
+ Note: by default, screenshots are cached for 24 hours. You can tune this valu with the cache=X parameter.
88
+
89
+ Returns {'code': <code>, 'file': <file path}
90
+ <code>: 200 if successful
91
+ <file path>: local file where the screenshot was saved, empty string it if failed
92
+
93
+ Arguments:
94
+ See https://browshot.com/api/documentation#simple for the full list of possible arguments.
95
+ url (Required): URL of the screenshot
96
+ file (Required): local file to store the screenshot
97
+ """
98
+ data = self.simple(url, parameters)
99
+ if data['png']:
100
+ with open(file, 'wb') as image:
101
+ image.write(data['png'])
102
+ return {'code': data['code'], 'file': file}
103
+
104
+ return {'code': data['code'], 'file': ''}
105
+
106
+
107
+ def instance_list(self):
108
+ """ Return the list of instances as a dictionary. See https://browshot.com/api/documentation#instance_list for the response format. """
109
+ return self.return_reply('instance/list')
110
+
111
+
112
+ def instance_info(self, id=0):
113
+ """ Return the details of an instance. See https://browshot.com/api/documentation#instance_info for the response format.
114
+
115
+ Arguments:
116
+ id (Required): Instance ID
117
+ """
118
+ return self.return_reply('instance/info', { 'id': id })
119
+
120
+
121
+ def browser_list(self):
122
+ """ Return the list of browsers as a hash reference. See https://browshot.com/api/documentation#browser_list for the response format. """
123
+ return self.return_reply('browser/list')
124
+
125
+ def browser_info(self, id=0):
126
+ """ Return the details of a browser. See https://browshot.com/api/documentation#browser_info for the response format.
127
+
128
+ Arguments:
129
+ id (Required: Browser ID
130
+ """
131
+ return self.return_reply('browser/info', { 'id': id })
132
+
133
+
134
+ def screenshot_create(self, url='', parameters={}):
135
+ """ Request a screenshot. See https://browshot.com/api/documentation#screenshot_create for the response format.
136
+
137
+ Note: by default, screenshots are cached for 24 hours. You can tune this valu with the cache=X parameter.
138
+
139
+ Arguments:
140
+ See https://browshot.com/api/documentation#screenshot_create for the full list of possible arguments.
141
+ url(Required): URL of the website to create a screenshot of.
142
+ """
143
+ parameters = dict(parameters, url=url)
144
+ return self.return_reply('screenshot/create', parameters)
145
+
146
+ def screenshot_info(self, id=0, parameters={}):
147
+ """ Get information about a screenshot requested previously. See https://browshot.com/api/documentation#screenshot_info for the response format.
148
+
149
+ Arguments:
150
+ id (Required): Screenshot ID.
151
+ """
152
+ parameters = dict(parameters, id=id)
153
+ return self.return_reply('screenshot/info', parameters)
154
+
155
+ def screenshot_list(self, parameters={}):
156
+ """ Get details about screenshot requested. See https://browshot.com/api/documentation#screenshot_list for the response format. """
157
+ return self.return_reply('screenshot/list', parameters)
158
+
159
+ def screenshot_host(self, id=0, parameters={}):
160
+ """ Host a screenshot or a thumbnail. See https://browshot.com/api/documentation#screenshot_host for the response format.
161
+
162
+ Arguments:
163
+ See https://browshot.com/api/documentation#screenshot_host for the full list of possible arguments.
164
+ id (Required): screenshot ID
165
+ """
166
+ parameters = dict(parameters, id=id)
167
+ return self.return_reply('screenshot/host', parameters)
168
+
169
+ def screenshot_share(self, id=0, parameters={}):
170
+ """ Share a screenshot. See L<http://browshot.com/api/documentation#screenshot_share> for the response format.
171
+
172
+ Arguments:
173
+ See https://browshot.com/api/documentation#screenshot_share for the full list of possible arguments.
174
+ id (Required): screenshot ID
175
+ """
176
+ parameters = dict(parameters, id=id)
177
+ return self.return_reply('screenshot/share', parameters)
178
+
179
+ def screenshot_search(self, url='', parameters={}):
180
+ """ Get details about screenshots requested. See L<http://browshot.com/api/documentation#screenshot_search> for the response format.
181
+
182
+ Arguments:
183
+ See https://browshot.com/api/documentation#screenshot_search for the full list of possible arguments.
184
+ url (Required): URL string to match
185
+ """
186
+ parameters = dict(parameters, url=url)
187
+ return self.return_reply('screenshot/search', parameters)
188
+
189
+ def screenshot_delete(self, id=0, parameters={}):
190
+ """ Delete details of a screenshot. See L<http://browshot.com/api/documentation#screenshot_delete> for the response format.
191
+
192
+ Arguments:
193
+ See https://browshot.com/api/documentation#screenshot_delete for the full list of possible arguments.
194
+ id (Required): screenshot ID
195
+ """
196
+ parameters = dict(parameters, id=id)
197
+ return self.return_reply('screenshot/delete', parameters)
198
+
199
+ def screenshot_thumbnail(self, id=0, parameters={}):
200
+ """ Retrieve the screenshot, or a thumbnail. See L<http://browshot.com/api/documentation#screenshot_thumbnail> for the response format.
201
+
202
+ Arguments:
203
+ See https://browshot.com/api/documentation#screenshot_thumbnail for the full list of possible arguments.
204
+ id (Required): screenshot ID. You will get the full image if no other argument is specified.
205
+ """
206
+ parameters = dict(parameters, id=id)
207
+ return self.return_reply_string('screenshot/thumbnail', parameters)
208
+
209
+ def screenshot_thumbnail_file(self, id=0, file='', parameters={}):
210
+ """ Retrieve the screenshot, or a thumbnail, and save it to a file. See http://browshot.com/api/documentation#screenshot_thumbnail for the response format.
211
+
212
+ Returns the file name if successful.
213
+
214
+ Arguments:
215
+ See https://browshot.com/api/documentation#screenshot_thumbnail for the full list of possible arguments.
216
+ id (Required): screenshot ID. You will get the full image if no other argument is specified.
217
+ file (Required): local file to store the screenshot
218
+ """
219
+ content = self.screenshot_thumbnail(id, parameters)
220
+ with open(file, 'wb') as image:
221
+ image.write(content)
222
+
223
+ return file
224
+
225
+ def screenshot_html(self, id=0, parameters={}):
226
+ """ Get the HTML code of the rendered page. See L<http://browshot.com/api/documentation#screenshot_html> for the response format.
227
+
228
+ Arguments:
229
+ See https://browshot.com/api/documentation#screenshot_html for the full list of possible arguments.
230
+ id (Required): screenshot ID
231
+ """
232
+ parameters = dict(parameters, id=id)
233
+ return self.return_reply_string('screenshot/html', parameters)
234
+
235
+
236
+ def screenshot_pdf(self, id=0, parameters=None):
237
+ """Return the PDF bytes for a finished capture, or b'' on failure."""
238
+ return self._screenshot_artifact('pdf', id, parameters, ('application/pdf',))
239
+
240
+ def screenshot_pdf_file(self, id=0, file='', parameters=None):
241
+ """Save a PDF and return its file path, or '' on failure."""
242
+ return self._save_artifact(self.screenshot_pdf, id, file, parameters)
243
+
244
+ def screenshot_video(self, id=0, parameters=None):
245
+ """Return a finished capture's WebM, MP4 or GIF bytes, or b'' on failure."""
246
+ return self._screenshot_artifact(
247
+ 'video', id, parameters, ('video/webm', 'video/mp4', 'image/gif')
248
+ )
249
+
250
+ def screenshot_video_file(self, id=0, file='', parameters=None):
251
+ """Save a recording using the supplied path, or return '' on failure.
252
+
253
+ An MP4 request can produce WebM. The file name stays as supplied.
254
+ """
255
+ return self._save_artifact(self.screenshot_video, id, file, parameters)
256
+
257
+
258
+ def screenshot_multiple(self, parameters={}):
259
+ """ Request multiple screenshots. See https://browshot.com/api/documentation#screenshot_multiple for the response format.
260
+
261
+ Arguments:
262
+ See https://browshot.com/api/documentation#screenshot_multiple for the full list of possible arguments.
263
+ """
264
+ return self.return_reply('screenshot/multiple', parameters)
265
+
266
+
267
+ def batch_create(self, file='', parameters={}):
268
+ """ Request multiple screenshots from a file. See https://browshot.com/api/documentation#batch_create for the response format.
269
+
270
+ Arguments:
271
+ See https://browshot.com/api/documentation#batch_create for the full list of possible arguments.
272
+ file (Required): file with hhe list of URLs to capture
273
+ """
274
+ return self.return_post_reply('batch/create', file, parameters)
275
+
276
+
277
+ def batch_info(self, id=0, parameters={}):
278
+ """ Check the status of a batch. See https://browshot.com/api/documentation#batch_info for the response format.
279
+
280
+ Arguments:
281
+ See https://browshot.com/api/documentation#batch_info for the full list of possible arguments.
282
+ id (Required): batch ID
283
+ """
284
+ parameters = dict(parameters, id=id)
285
+ return self.return_reply('batch/info', parameters)
286
+
287
+
288
+ def crawl_create(self, domain='', url='', parameters={}):
289
+ """ Crawl a domain. See https://browshot.com/api/documentation#crawl_create for the response format.
290
+
291
+ Arguments:
292
+ See https://browshot.com/api/documentation#crawl_create for the full list of possible arguments.
293
+ domain (Required): domain to crawl
294
+ url (Required): URL to start with
295
+ """
296
+ parameters = dict(parameters, url=url, domain=domain)
297
+ return self.return_reply('crawl/create', parameters)
298
+
299
+
300
+ def crawl_info(self, id=0, parameters={}):
301
+ """ Request multiple screenshots. See https://browshot.com/api/documentation#crawl_info for the response format.
302
+
303
+ Arguments:
304
+ See https://browshot.com/api/documentation#crawl_info for the full list of possible arguments.
305
+ id (Required): crawl ID
306
+ """
307
+ parameters = dict(parameters, id=id)
308
+ return self.return_reply('crawl/info', parameters)
309
+
310
+
311
+ def account_info(self, parameters={}):
312
+ """ Get details about the user account. See https://browshot.com/api/documentation#account_info for the response format. """
313
+ return self.return_reply('account/info', parameters)
314
+
315
+
316
+ def _screenshot_artifact(self, kind, id, parameters, content_types):
317
+ if not re.fullmatch(r'[1-9][0-9]{0,18}', str(id)):
318
+ return b''
319
+
320
+ query = dict(parameters or {}, id=id)
321
+ url = self.make_url('screenshot/' + kind, query)
322
+ try:
323
+ with urlopen(url, timeout=60) as response:
324
+ if response.status != 200 or response.headers.get_content_type() not in content_types:
325
+ return b''
326
+ return response.read()
327
+ except HTTPError as error:
328
+ error.close()
329
+ return b''
330
+ except (URLError, HTTPException, OSError):
331
+ return b''
332
+
333
+ def _save_artifact(self, download, id, file, parameters):
334
+ if not file or not isinstance(file, (str, bytes, PathLike)):
335
+ return ''
336
+
337
+ content = download(id, parameters)
338
+ if not content:
339
+ return ''
340
+
341
+ try:
342
+ with open(file, 'wb') as artifact:
343
+ artifact.write(content)
344
+ except (OSError, ValueError):
345
+ return ''
346
+ return file
347
+
348
+
349
+ def make_url(self, action='', parameters={}):
350
+ url = self.base + action + '?key=' + quote_plus(self.key)
351
+
352
+ for key, value in parameters.items():
353
+ if key == 'urls':
354
+ for uri in value:
355
+ url += '&url=' + quote_plus(str(uri))
356
+ elif key == 'instances':
357
+ for instance_id in value:
358
+ url += '&instance_id=' + quote_plus(str(instance_id))
359
+ else:
360
+ url += '&' + quote_plus(key) + '=' + quote_plus(str(value))
361
+
362
+ if self.debug:
363
+ print(self.base + action)
364
+
365
+ return url
366
+
367
+
368
+ def return_reply(self, action='', parameters={}):
369
+ return json.loads(self.return_reply_string(action, parameters))
370
+
371
+ def return_reply_string(self, action='', parameters={}):
372
+ url = self.make_url(action, parameters)
373
+ try:
374
+ with urlopen(url) as response:
375
+ return response.read()
376
+ except HTTPError as error:
377
+ # urllib.urlopen on Python 2 returned the response body for these
378
+ # statuses. Keep Browshot's JSON errors available to callers.
379
+ with error:
380
+ return error.read()
381
+
382
+ def return_post_reply(self, action='', file='', parameters={}):
383
+ return json.loads(self.return_reply_post_string(action, file, parameters))
384
+
385
+ def return_reply_post_string(self, action='', file='', parameters={}):
386
+ if not file:
387
+ return self.return_reply_string(action, parameters)
388
+
389
+ url = self.make_url(action, parameters)
390
+ with open(file, 'rb') as upload:
391
+ with requests.post(url, files={'file': upload}) as response:
392
+ return response.content
393
+
394
+
395
+ if __name__ == "__main__":
396
+ client = BrowshotClient()