Set default timeout in `download.stream_file` to 10 seconds, and allow to override value by benoit74 · Pull Request #222 · openzim/python-scraperlib · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
4 changes: 4 additions & 0 deletions src/zimscraperlib/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@
ILLUSTRATIONS_METADATA_RE = re.compile(
r"^Illustration_(?P<height>\d+)x(?P<width>\d+)@(?P<scale>\d+)$"
)

# default timeout to get responses from upstream when doing web requests ; this is not
# the total time it gets to download the whole resource
DEFAULT_WEB_REQUESTS_TIMEOUT = 10
3 changes: 3 additions & 0 deletions src/zimscraperlib/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import yt_dlp as youtube_dl

from zimscraperlib import logger
from zimscraperlib.constants import DEFAULT_WEB_REQUESTS_TIMEOUT


class YoutubeDownloader:
Expand Down Expand Up @@ -181,6 +182,7 @@ def stream_file(
max_retries: int | None = 5,
headers: dict[str, str] | None = None,
session: requests.Session | None = None,
timeout: int | None = DEFAULT_WEB_REQUESTS_TIMEOUT,
*,
only_first_block: bool | None = False,
) -> tuple[int, requests.structures.CaseInsensitiveDict[str]]:
Expand Down Expand Up @@ -208,6 +210,7 @@ def stream_file(
stream=True,
proxies=proxies,
headers=headers,
timeout=timeout,
)
resp.raise_for_status()

Expand Down
13 changes: 8 additions & 5 deletions tests/download/test_download.py