download to tmp file and rename once download is complete
This commit is contained in:
parent
0e801f82a3
commit
e850296f68
1 changed files with 4 additions and 3 deletions
|
@ -113,14 +113,15 @@ get_url = read_url
|
|||
def save_url(url, filename, overwrite=False):
|
||||
if not os.path.exists(filename) or overwrite:
|
||||
dirname = os.path.dirname(filename)
|
||||
if dirname and not os.path.exists(dirname):
|
||||
os.makedirs(dirname)
|
||||
os.makedirs(dirname, exist_ok=True)
|
||||
headers = DEFAULT_HEADERS.copy()
|
||||
r = requests.get(url, headers=headers, stream=True)
|
||||
with open(filename, 'wb') as f:
|
||||
filename_tmp = filename + '~'
|
||||
with open(filename_tmp, 'wb') as f:
|
||||
for chunk in r.iter_content(chunk_size=1024):
|
||||
if chunk: # filter out keep-alive new chunks
|
||||
f.write(chunk)
|
||||
os.rename(filename_tmp, filename)
|
||||
|
||||
def _get_size(url):
|
||||
req = urllib.request.Request(url, headers=DEFAULT_HEADERS.copy())
|
||||
|
|
Loading…
Reference in a new issue