Skip to content

Commit

Permalink
Merge pull request #621 from rohis06/develop
Browse files Browse the repository at this point in the history
Added support for handling 206 response in `eta.core.web.download_file()` - Fix for Issue #620
  • Loading branch information
brimoor authored Mar 1, 2024
2 parents ede22d9 + d56be35 commit 2b36830
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions eta/core/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
# pragma pylint: enable=unused-wildcard-import
# pragma pylint: enable=wildcard-import

# pylint: disable=E1121

import io
import logging
import re
Expand Down Expand Up @@ -150,6 +152,7 @@ def write(self, path, url, params=None):
self._do_download(r, f)

def _get_streaming_response(self, url, headers=None, params=None):

r = self.sess.get(
url,
headers=headers,
Expand All @@ -165,14 +168,40 @@ def _get_streaming_response(self, url, headers=None, params=None):

def _do_download(self, r, f):
size_bytes = _get_content_length(r)
total_downloaded_bytes = 0
size_bits = 8 * size_bytes if size_bytes is not None else None

with etau.ProgressBar(
size_bits, use_bits=True, quiet=self.quiet
) as pb:
for chunk in r.iter_content(chunk_size=self.chunk_size):
f.write(chunk)
total_downloaded_bytes += len(chunk)
pb.update(8 * len(chunk))

while size_bytes is not None and (
"Accept-Ranges" in r.headers
and r.headers["Accept-Ranges"] is not None
):
remaining_bytes = size_bytes - total_downloaded_bytes
if remaining_bytes > 0:
logger.debug(
"Continuing download...Total downloaded bytes: %d, Remaining bytes: %d"
% (total_downloaded_bytes, remaining_bytes)
)
r = self._get_streaming_response(
r.url,
headers={
"Range": "bytes=%d-" % total_downloaded_bytes
},
)
for chunk in r.iter_content(chunk_size=self.chunk_size):
f.write(chunk)
total_downloaded_bytes += len(chunk)
pb.update(8 * len(chunk))
else:
break


class WebSessionError(Exception):
"""Exception raised when there is a problem with a web session."""
Expand Down

0 comments on commit 2b36830

Please sign in to comment.