Skip to content

Commit baf00fe

Browse files
AppStore app: improve download_url() function
1 parent 581d6a6 commit baf00fe

File tree

1 file changed

+45
-37
lines changed
  • internal_filesystem/builtin/apps/com.micropythonos.appstore/assets

1 file changed

+45
-37
lines changed

internal_filesystem/builtin/apps/com.micropythonos.appstore/assets/appstore.py

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -183,45 +183,53 @@ async def download_url(self, url, outfile=None):
183183
try:
184184
async with self.aiohttp_session.get(url) as response:
185185
if response.status < 200 or response.status >= 400:
186-
return None
187-
if not outfile:
188-
return await response.read()
189-
else:
190-
# Would be good to check free available space first
191-
chunk_size = 1024
192-
print("headers:") ; print(response.headers)
193-
total_size = response.headers.get('Content-Length') # some servers don't send this
194-
print(f"download_url writing to {outfile} of {total_size} bytes in chunks of size {chunk_size}")
195-
with open(outfile, 'wb') as fd:
196-
print("opened file...")
197-
print(dir(response.content))
198-
while True:
199-
#print("Downloading next chunk...")
200-
tries_left=3
201-
chunk = None
202-
while tries_left > 0:
203-
try:
204-
chunk = await TaskManager.wait_for(response.content.read(chunk_size), 10)
205-
break
206-
except Exception as e:
207-
print(f"Waiting for response.content.read of next chunk got error: {e}")
208-
tries_left -= 1
209-
if tries_left == 0:
210-
print("ERROR: failed to download chunk, even with retries!")
211-
return False
212-
else:
213-
#print(f"Downloaded chunk: {chunk}")
214-
if chunk:
215-
#print("writing chunk...")
216-
fd.write(chunk)
217-
#print("wrote chunk")
218-
else:
219-
print("chunk is None while there was no error so this was the last one")
220-
print(f"Done downloading {url}")
221-
return True
186+
return False if outfile else None
187+
188+
# Always use chunked downloading
189+
chunk_size = 1024
190+
print("headers:") ; print(response.headers)
191+
total_size = response.headers.get('Content-Length') # some servers don't send this
192+
print(f"download_url {'writing to ' + outfile if outfile else 'reading'} {total_size} bytes in chunks of size {chunk_size}")
193+
194+
fd = open(outfile, 'wb') if outfile else None
195+
chunks = [] if not outfile else None
196+
197+
if fd:
198+
print("opened file...")
199+
200+
while True:
201+
tries_left = 3
202+
chunk = None
203+
while tries_left > 0:
204+
try:
205+
chunk = await TaskManager.wait_for(response.content.read(chunk_size), 10)
206+
break
207+
except Exception as e:
208+
print(f"Waiting for response.content.read of next chunk got error: {e}")
209+
tries_left -= 1
210+
211+
if tries_left == 0:
212+
print("ERROR: failed to download chunk, even with retries!")
213+
if fd:
214+
fd.close()
215+
return False if outfile else None
216+
217+
if chunk:
218+
if fd:
219+
fd.write(chunk)
220+
else:
221+
chunks.append(chunk)
222+
else:
223+
print("chunk is None while there was no error so this was the last one")
224+
print(f"Done downloading {url}")
225+
if fd:
226+
fd.close()
227+
return True
228+
else:
229+
return b''.join(chunks)
222230
except Exception as e:
223231
print(f"download_url got exception {e}")
224-
return False
232+
return False if outfile else None
225233

226234
@staticmethod
227235
def badgehub_app_to_mpos_app(bhapp):

0 commit comments

Comments
 (0)