Skip to content

Commit

Permalink
Handle cases where response size cannot be calculated, closes #89
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Sep 13, 2022
1 parent bf34a76 commit 31bc975
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/screenshots.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ The output looks like this:
{"method": "GET", "url": "https://datasette.io/static/site.css", "size": 3952, "timing": {"startTime": 1663014972482.3188, "domainLookupStart": -1, "domainLookupEnd": -1, "connectStart": -1, "secureConnectionStart": -1, "connectEnd": -1, "requestStart": 0.433, "responseStart": 81.263, "responseEnd": 82.535}}
...

Note that the `size` field here will be the size of the response in bytes, but in some circumstances this will not be available and it will be returned as `"size": null`.

## Taking screenshots of local HTML files

You can pass the path to an HTML file on disk to take a screenshot of that rendered file:
Expand Down
10 changes: 9 additions & 1 deletion shot_scraper/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,12 +726,20 @@ def take_shot(
if log_requests:

def on_response(response):
try:
body = response.body()
size = len(body)
except Error as ex:
if "Network.getResponseBody" in ex.message:
size = None
else:
raise
log_requests.write(
json.dumps(
{
"method": response.request.method,
"url": response.url,
"size": len(response.body()),
"size": size,
"timing": response.request.timing,
}
)
Expand Down

0 comments on commit 31bc975

Please sign in to comment.