Merge branch 'chunked-resume' of https://github.com/chrisww/you-get into chrisww-chunked-resume

This commit is contained in:
Mort Yao 2020-07-18 22:50:30 +02:00
commit f1f2164e54
No known key found for this signature in database
GPG Key ID: 07DA00CB78203251

View File

@ -629,10 +629,12 @@ def url_save(
if refer is not None: if refer is not None:
tmp_headers['Referer'] = refer tmp_headers['Referer'] = refer
if type(url) is list: if type(url) is list:
file_size = urls_size(url, faker=faker, headers=tmp_headers) chunk_sizes = [url_size(url, faker=faker, headers=tmp_headers) for url in url]
file_size = sum(chunk_sizes)
is_chunked, urls = True, url is_chunked, urls = True, url
else: else:
file_size = url_size(url, faker=faker, headers=tmp_headers) file_size = url_size(url, faker=faker, headers=tmp_headers)
chunk_sizes = [file_size]
is_chunked, urls = False, [url] is_chunked, urls = False, [url]
continue_renameing = True continue_renameing = True
@ -696,9 +698,13 @@ def url_save(
else: else:
open_mode = 'wb' open_mode = 'wb'
for url in urls: chunk_start = 0
chunk_end = 0
for i, url in enumerate(urls):
received_chunk = 0 received_chunk = 0
if received < file_size: chunk_start += 0 if i == 0 else chunk_sizes[i - 1]
chunk_end += chunk_sizes[i]
if received < file_size and received < chunk_end:
if faker: if faker:
tmp_headers = fake_headers tmp_headers = fake_headers
''' '''
@ -708,8 +714,9 @@ def url_save(
else: else:
headers = {} headers = {}
''' '''
if received and not is_chunked: # only request a range when not chunked if received:
tmp_headers['Range'] = 'bytes=' + str(received) + '-' # chunk_start will always be 0 if not chunked
tmp_headers['Range'] = 'bytes=' + str(received - chunk_start) + '-'
if refer: if refer:
tmp_headers['Referer'] = refer tmp_headers['Referer'] = refer
@ -757,8 +764,7 @@ def url_save(
elif not is_chunked and received == file_size: # Download finished elif not is_chunked and received == file_size: # Download finished
break break
# Unexpected termination. Retry request # Unexpected termination. Retry request
if not is_chunked: # when tmp_headers['Range'] = 'bytes=' + str(received - chunk_start) + '-'
tmp_headers['Range'] = 'bytes=' + str(received) + '-'
response = urlopen_with_retry( response = urlopen_with_retry(
request.Request(url, headers=tmp_headers) request.Request(url, headers=tmp_headers)
) )