Add error handling and resume to dl func.
parent
920ca3267f
commit
37d64aeadc
|
|
@ -2,6 +2,7 @@
|
||||||
import sys
|
import sys
|
||||||
import requests
|
import requests
|
||||||
import os
|
import os
|
||||||
|
import time
|
||||||
from . import util
|
from . import util
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -93,21 +94,37 @@ def dl(url, folder, filename, filepath):
|
||||||
|
|
||||||
# write to file
|
# write to file
|
||||||
with open(dl_file_path, "ab") as f:
|
with open(dl_file_path, "ab") as f:
|
||||||
for chunk in r.iter_content(chunk_size=1024):
|
resumecount = 3
|
||||||
if chunk:
|
while(resumecount > 0):
|
||||||
downloaded_size += len(chunk)
|
for chunk in r.iter_content(chunk_size=1024):
|
||||||
f.write(chunk)
|
if chunk:
|
||||||
# force to write to disk
|
downloaded_size += len(chunk)
|
||||||
f.flush()
|
f.write(chunk)
|
||||||
|
# force to write to disk
|
||||||
|
f.flush()
|
||||||
|
|
||||||
# progress
|
# progress
|
||||||
progress = int(50 * downloaded_size / total_size)
|
progress = int(50 * downloaded_size / total_size)
|
||||||
sys.stdout.reconfigure(encoding='utf-8')
|
sys.stdout.reconfigure(encoding='utf-8')
|
||||||
sys.stdout.write("\r[%s%s] %d%%" % ('-' * progress, ' ' * (50 - progress), 100 * downloaded_size / total_size))
|
sys.stdout.write("\r[%s%s] %d%%" % ('-' * progress, ' ' * (50 - progress), 100 * downloaded_size / total_size))
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
|
if(downloaded_size == total_size):
|
||||||
|
break
|
||||||
|
resumecount -= 1
|
||||||
|
if(resumecount == 0):
|
||||||
|
break
|
||||||
|
print()
|
||||||
|
util.printD(f"Download failed, Try to resume. Downloaded size: {downloaded_size}")
|
||||||
|
time.sleep(3)
|
||||||
|
headers['Range']= 'bytes=%d-' % downloaded_size
|
||||||
|
r = requests.get(url, stream=True, verify=False, headers=headers, proxies=util.proxies)
|
||||||
|
|
||||||
print()
|
print()
|
||||||
|
|
||||||
|
if(downloaded_size != total_size):
|
||||||
|
util.printD(f"Download failed due to insufficient file size. {url}")
|
||||||
|
return
|
||||||
|
|
||||||
# rename file
|
# rename file
|
||||||
os.rename(dl_file_path, file_path)
|
os.rename(dl_file_path, file_path)
|
||||||
util.printD(f"File Downloaded to: {file_path}")
|
util.printD(f"File Downloaded to: {file_path}")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue