Skip to content
Closed
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
30 changes: 28 additions & 2 deletions src/kaggle/api/kaggle_api_extended.py
Original file line number Diff line number Diff line change
Expand Up @@ -3154,6 +3154,31 @@ def dataset_create_new_cli(self, folder=None, public=False, quiet=False, convert
else:
print("Dataset creation error: " + result.error)


@staticmethod
def _extract_content_length(response):
"""Extract the content length and determine if the content is chunked from a response.

Args:
response: The response for which to look up the content length.
"""
content_length = response.headers.get("Content-Length")
transfer_encoding = response.headers.get("Transfer-Encoding")
# default
is_chunked = False
size = 0

if content_length is not None:
size = int(content_length)

# Per RFC 7230, chunked encoding implies unknown content length.
# We return 0 here to allow the stream to proceed.
if transfer_encoding == 'chunked':
size = 0
is_chunked = True

return size, is_chunked

def download_file(
self, response, outfile, http_client, quiet=True, resume=False, chunk_size=1048576, max_retries=5, timeout=300
):
Expand All @@ -3175,7 +3200,8 @@ def download_file(
os.makedirs(outpath)

# Get file metadata
size = int(response.headers["Content-Length"])
size, is_chunked = self._extract_content_length(response)

last_modified = response.headers.get("Last-Modified")
if last_modified is None:
remote_date = datetime.now()
Expand Down Expand Up @@ -3268,7 +3294,7 @@ def download_file(

# Verify file size
final_size = os.path.getsize(outfile)
if final_size != size:
if final_size != size and not is_chunked:
error_msg = f"Downloaded file size ({final_size}) does not match expected size ({size})"
if not quiet:
print(f"\n{error_msg}")
Expand Down
Loading