Skip to content

Commit ec807a2

Browse files
authored
Use Download.path directly (#246)
1 parent 42a1080 commit ec807a2

File tree

2 files changed

+2
-21
lines changed

2 files changed

+2
-21
lines changed

scrapy_playwright/handler.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from contextlib import suppress
44
from dataclasses import dataclass
55
from ipaddress import ip_address
6-
from tempfile import NamedTemporaryFile
76
from time import time
87
from typing import Awaitable, Callable, Dict, Optional, Tuple, Type, TypeVar, Union
98

@@ -429,10 +428,8 @@ async def _handle_download(dwnld: Download) -> None:
429428
try:
430429
if failure := await dwnld.failure():
431430
raise RuntimeError(f"Failed to download {dwnld.url}: {failure}")
432-
with NamedTemporaryFile() as temp_file:
433-
await dwnld.save_as(temp_file.name)
434-
temp_file.seek(0)
435-
download["bytes"] = temp_file.read()
431+
download_path = await dwnld.path()
432+
download["bytes"] = download_path.read_bytes()
436433
download["url"] = dwnld.url
437434
download["suggested_filename"] = dwnld.suggested_filename
438435
except Exception as ex:

tests/tests_asyncio/test_playwright_requests.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -475,22 +475,6 @@ async def test_download_file_delay_error(self):
475475
f" exc_type={type(excinfo.value)} exc_msg={str(excinfo.value)}",
476476
) in self._caplog.record_tuples
477477

478-
@patch("scrapy_playwright.handler.NamedTemporaryFile")
479-
@pytest.mark.asyncio
480-
async def test_download_file_exception(self, NamedTemporaryFile):
481-
NamedTemporaryFile.side_effect = ZeroDivisionError("asdf")
482-
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
483-
with MockServer() as server:
484-
request = Request(url=server.urljoin("/mancha.pdf"), meta={"playwright": True})
485-
with pytest.raises(ZeroDivisionError) as excinfo:
486-
await handler._download_request(request, Spider("foo"))
487-
assert (
488-
"scrapy-playwright",
489-
logging.WARNING,
490-
f"Closing page due to failed request: {request}"
491-
f" exc_type={type(excinfo.value)} exc_msg={str(excinfo.value)}",
492-
) in self._caplog.record_tuples
493-
494478
@pytest.mark.asyncio
495479
async def test_download_file_failure(self):
496480
if self.browser_type != "chromium":

0 commit comments

Comments
 (0)