don't delete downloaded files in certain edge cases

pull/13/head
Mike Fährmann 8 years ago
parent 2b2bdce366
commit 3c1daef839
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -15,6 +15,9 @@ class BasicDownloader():
max_tries = 5 max_tries = 5
def __init__(self):
self.downloading = False
def download(self, url, pathfmt): def download(self, url, pathfmt):
"""Download the resource at 'url' and write it to a file-like object""" """Download the resource at 'url' and write it to a file-like object"""
try: try:
@ -22,6 +25,7 @@ class BasicDownloader():
except: except:
# remove file if download failed # remove file if download failed
try: try:
if self.downloading:
os.unlink(pathfmt.realpath) os.unlink(pathfmt.realpath)
except (AttributeError, FileNotFoundError): except (AttributeError, FileNotFoundError):
pass pass

@ -61,9 +61,11 @@ class Downloader(BasicDownloader):
return return
self.out.start(pathfmt.path) self.out.start(pathfmt.path)
self.downloading = True
with pathfmt.open() as file: with pathfmt.open() as file:
for data in response.iter_content(16384): for data in response.iter_content(16384):
file.write(data) file.write(data)
self.downloading = False
self.out.success(pathfmt.path, tries) self.out.success(pathfmt.path, tries)
def set_headers(self, headers): def set_headers(self, headers):

@ -24,6 +24,8 @@ class Downloader(BasicDownloader):
return return
self.out.start(pathfmt.path) self.out.start(pathfmt.path)
self.downloading = True
with pathfmt.open() as file: with pathfmt.open() as file:
file.write(bytes(url[7:], "utf-8")) file.write(bytes(url[7:], "utf-8"))
self.downloading = False
self.out.success(pathfmt.path, 0) self.out.success(pathfmt.path, 0)

Loading…
Cancel
Save