Skip to content

Commit 95f4822

Browse files
author
Benjamin Moody
committed
dl_pn_file: use openurl in place of requests.get.
Note that this will handle errors (e.g. a file that does not exist) and raise an exception, rather than writing the error document to the output file. It will also correctly handle remote files that do not support random access.
1 parent ac3a0b5 commit 95f4822

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

wfdb/io/download.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,6 @@ def dl_pn_file(inputs):
422422
# Full url of file
423423
url = posixpath.join(config.db_index_url, db, subdir, basefile)
424424

425-
# Supposed size of the file
426-
remote_file_size = _remote_file_size(url)
427-
428425
# Figure out where the file should be locally
429426
if keep_subdirs:
430427
dldir = os.path.join(dl_dir, subdir)
@@ -441,20 +438,19 @@ def dl_pn_file(inputs):
441438
# Process accordingly.
442439
else:
443440
local_file_size = os.path.getsize(local_file)
444-
# Local file is smaller than it should be. Append it.
445-
if local_file_size < remote_file_size:
446-
print('Detected partially downloaded file: %s Appending file...' % local_file)
447-
headers = {"Range": "bytes="+str(local_file_size)+"-", 'Accept-Encoding': '*'}
448-
r = requests.get(url, headers=headers, stream=True)
449-
print('headers: ', headers)
450-
print('r content length: ', len(r.content))
451-
with open(local_file, 'ba') as writefile:
452-
writefile.write(r.content)
453-
print('Done appending.')
454-
# Local file is larger than it should be. Redownload.
455-
elif local_file_size > remote_file_size:
456-
dl_full_file(url, local_file)
457-
# If they're the same size, do nothing.
441+
with _url.openurl(url, 'rb') as f:
442+
remote_file_size = f.seek(0, os.SEEK_END)
443+
# Local file is smaller than it should be. Append it.
444+
if local_file_size < remote_file_size:
445+
print('Detected partially downloaded file: %s Appending file...' % local_file)
446+
f.seek(local_file_size, os.SEEK_SET)
447+
with open(local_file, 'ba') as writefile:
448+
writefile.write(f.read())
449+
print('Done appending.')
450+
# Local file is larger than it should be. Redownload.
451+
elif local_file_size > remote_file_size:
452+
dl_full_file(url, local_file)
453+
# If they're the same size, do nothing.
458454

459455
# The file doesn't exist. Download it.
460456
else:

0 commit comments

Comments
 (0)