Skip to content

Commit

Permalink
Added management of resumption of downloads stopped by a crash or a s…
Browse files Browse the repository at this point in the history
…top by the user
  • Loading branch information
Asthowen committed Oct 15, 2022
1 parent 76bcbb2 commit fefa04b
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions alphacoders_downloader/alphacoders_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,17 @@ async def download(self, element: list):
path = os.path.join(self.path, element[1])
if os.path.isfile(path) is False:
temp_path = os.path.join(self.temp_path, element[1])
headers = {}
temp_file_exist = os.path.isfile(temp_path)
if temp_file_exist:
file_size = os.stat(temp_path).st_size
headers['Range'] = f'bytes={file_size}-'
file_downloaded = 0
async with self.client_session.get(element[0]) as r:

async with self.client_session.get(element[0], headers=headers) as r:
try:
async with aiofiles.open(temp_path, 'wb') as f:
write_mode = 'ab' if temp_file_exist else 'wb'
async with aiofiles.open(temp_path, write_mode) as f:
try:
async for data in r.content.iter_chunked(1024):
await f.write(data)
Expand Down Expand Up @@ -190,7 +197,11 @@ async def main():

def start():
try:
os.get_terminal_size(0)
asyncio.get_event_loop().run_until_complete(main())
except OSError:
print_error('Your terminal does not support all the features needed for AlphacodersDownloader, please use another one.')
show()
except KeyboardInterrupt:
clear_line()
print('Stop the script...')
Expand Down

0 comments on commit fefa04b

Please sign in to comment.