From 5a21965e6131b1a0e48de12eb42ca28f2e4e79b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Sun, 25 Feb 2024 01:31:14 +0100 Subject: [PATCH] fix: Avoid undefined variable when the body is empty --- playwrightcapture/capture.py | 19 ++++++++----------- poetry.lock | 14 +++++++------- pyproject.toml | 4 ++-- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/playwrightcapture/capture.py b/playwrightcapture/capture.py index f67bda7..7299ee9 100644 --- a/playwrightcapture/capture.py +++ b/playwrightcapture/capture.py @@ -484,19 +484,16 @@ async def store_request(request: Request) -> None: if response := await request.response(): if response.ok: try: - body = await response.body() + if body := await response.body(): + try: + mimetype = from_string(body, mime=True) + if mimetype.startswith('image'): + self._requests[request.url] = body + except PureError: + # unable to identify the mimetype + self.logger.debug(f'Unable to identify the mimetype for {request.url}') except Exception as e: self.logger.debug(f'Unable to get body for {request.url}: {e}') - else: - try: - if body: - mimetype = from_string(body, mime=True) - except PureError: - # unable to identify the mimetype - self.logger.debug(f'Unable to identify the mimetype for {request.url}') - else: - if mimetype.startswith('image'): - self._requests[request.url] = body except Exception as e: self.logger.warning(f'Unable to store request: {e}') diff --git a/poetry.lock b/poetry.lock index 09712f5..f9a8293 100644 --- a/poetry.lock +++ b/poetry.lock @@ -518,13 +518,13 @@ testing = ["pytest", "pytest-benchmark"] [[package]] name = "puremagic" -version = "1.20" +version = "1.21" description = "Pure python implementation of magic file detection" optional = false python-versions = "*" files = [ - {file = "puremagic-1.20-py3-none-any.whl", hash = "sha256:14817470dc1e3339356088b58576820efd12544a676c20d7d5e738ea1e06f852"}, - {file = "puremagic-1.20.tar.gz", hash = "sha256:c55c57369bd957bfe3af4765a66784eaaae77d697a6f12477174280e0abcbd07"}, + {file = "puremagic-1.21-py3-none-any.whl", hash = "sha256:8fe85c05800fe1eacdd5aa943b9e7fdbee66bc41a17aacf80efd6c668c63df45"}, + {file = "puremagic-1.21.tar.gz", hash = "sha256:31ef09b37a6ad2f7f2b09b5bd6b8c4a07187a01af4025f5f1368889bdfc6d779"}, ] [[package]] @@ -569,13 +569,13 @@ files = [ [[package]] name = "pytest" -version = "8.0.1" +version = "8.0.2" description = "pytest: simple powerful testing with Python" optional = false python-versions = ">=3.8" files = [ - {file = "pytest-8.0.1-py3-none-any.whl", hash = "sha256:3e4f16fe1c0a9dc9d9389161c127c3edc5d810c38d6793042fb81d9f48a59fca"}, - {file = "pytest-8.0.1.tar.gz", hash = "sha256:267f6563751877d772019b13aacbe4e860d73fe8f651f28112e9ac37de7513ae"}, + {file = "pytest-8.0.2-py3-none-any.whl", hash = "sha256:edfaaef32ce5172d5466b5127b42e0d6d35ebbe4453f0e3505d96afd93f6b096"}, + {file = "pytest-8.0.2.tar.gz", hash = "sha256:d4051d623a2e0b7e51960ba963193b09ce6daeb9759a451844a21e4ddedfc1bd"}, ] [package.dependencies] @@ -940,4 +940,4 @@ recaptcha = ["SpeechRecognition", "pydub", "requests"] [metadata] lock-version = "2.0" python-versions = "^3.8" -content-hash = "07c0257c4930a607c8ad88093bdcc95cd28edbb3f00fdf9b639f5b3cf9dcb910" +content-hash = "663e54b9c90c066d6aa3dca7562fcc1cf26a12234517bd208a943c2b1288774a" diff --git a/pyproject.toml b/pyproject.toml index 48f367c..69f6fee 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,7 +30,7 @@ pytz = {"version" = "^2024.1", python = "<3.9"} tzdata = "^2024.1" playwright-stealth = "^1.0.6" setuptools = "^69.1.1" -puremagic = "^1.20" +puremagic = "^1.21" [tool.poetry.extras] recaptcha = ["requests", "pydub", "SpeechRecognition"] @@ -40,7 +40,7 @@ optional = true [tool.poetry.group.dev.dependencies] types-beautifulsoup4 = "^4.12.0.20240106" -pytest = "^8.0.1" +pytest = "^8.0.2" mypy = "^1.8.0" types-dateparser = "^1.1.4.20240106" types-requests = "^2.31.0.20240218"