@@ -109,30 +109,34 @@ def simple_download(url: str, destination: Union[Path, str], headers: Optional[d
109
109
temporary .write_bytes (b'' ) # clearing temporary file when total_length is inconsistent
110
110
111
111
with temporary .open ('ab' ) as f :
112
- done = False
113
112
downloaded = f .tell ()
114
113
if downloaded != 0 :
115
114
log .warning (f'Found a partial download { temporary } ' )
116
115
with tqdm (initial = downloaded , total = total_length , unit = 'B' , unit_scale = True ) as pbar :
117
- while not done :
116
+ while True :
118
117
if downloaded != 0 :
119
118
log .warning (f'Download stopped abruptly, trying to resume from { downloaded } '
120
119
f'to reach { total_length } ' )
121
120
headers ['Range' ] = f'bytes={ downloaded } -'
122
121
r = requests .get (url , headers = headers , stream = True )
123
122
if 'content-length' not in r .headers or \
124
123
total_length - downloaded != int (r .headers ['content-length' ]):
125
- raise RuntimeError (f'It looks like the server does not support resuming '
126
- f'downloads.' )
127
- for chunk in r .iter_content (chunk_size = chunk_size ):
128
- if chunk : # filter out keep-alive new chunks
129
- downloaded += len (chunk )
130
- pbar .update (len (chunk ))
131
- f .write (chunk )
124
+ raise RuntimeError ('It looks like the server does not support resuming downloads.' )
125
+
126
+ try :
127
+ for chunk in r .iter_content (chunk_size = chunk_size ):
128
+ if chunk : # filter out keep-alive new chunks
129
+ downloaded += len (chunk )
130
+ pbar .update (len (chunk ))
131
+ f .write (chunk )
132
+ except requests .exceptions .ChunkedEncodingError :
133
+ if downloaded == 0 :
134
+ r = requests .get (url , stream = True , headers = headers )
135
+
132
136
if downloaded >= total_length :
133
137
# Note that total_length is 0 if the server didn't return the content length,
134
138
# in this case we perform just one iteration and assume that we are done.
135
- done = True
139
+ break
136
140
137
141
temporary .rename (destination )
138
142
0 commit comments