mirror of
https://github.com/corpnewt/gibMacOS.git
synced 2024-12-04 13:33:26 +01:00
Wrap in try/except
This commit is contained in:
parent
e73da3cf30
commit
be5f277ddc
@ -45,52 +45,15 @@ class Downloader:
|
||||
sys.stdout.write("Downloaded {}\r".format(b_s))
|
||||
|
||||
def get_string(self, url, progress = True):
|
||||
response = urlopen(url)
|
||||
CHUNK = 16 * 1024
|
||||
bytes_so_far = 0
|
||||
try:
|
||||
total_size = int(response.headers['Content-Length'])
|
||||
except:
|
||||
total_size = -1
|
||||
chunk_so_far = "".encode("utf-8")
|
||||
while True:
|
||||
chunk = response.read(CHUNK)
|
||||
bytes_so_far += len(chunk)
|
||||
if progress:
|
||||
self._progress_hook(response, bytes_so_far, total_size)
|
||||
if not chunk:
|
||||
break
|
||||
chunk_so_far += chunk
|
||||
return chunk_so_far.decode("utf-8")
|
||||
|
||||
def get_bytes(self, url, progress = True):
|
||||
response = urlopen(url)
|
||||
CHUNK = 16 * 1024
|
||||
bytes_so_far = 0
|
||||
try:
|
||||
total_size = int(response.headers['Content-Length'])
|
||||
except:
|
||||
total_size = -1
|
||||
chunk_so_far = "".encode("utf-8")
|
||||
while True:
|
||||
chunk = response.read(CHUNK)
|
||||
bytes_so_far += len(chunk)
|
||||
if progress:
|
||||
self._progress_hook(response, bytes_so_far, total_size)
|
||||
if not chunk:
|
||||
break
|
||||
chunk_so_far += chunk
|
||||
return chunk_so_far
|
||||
|
||||
def stream_to_file(self, url, file, progress = True):
|
||||
response = urlopen(url)
|
||||
CHUNK = 16 * 1024
|
||||
bytes_so_far = 0
|
||||
try:
|
||||
total_size = int(response.headers['Content-Length'])
|
||||
except:
|
||||
total_size = -1
|
||||
with open(file, 'wb') as f:
|
||||
response = urlopen(url)
|
||||
CHUNK = 16 * 1024
|
||||
bytes_so_far = 0
|
||||
try:
|
||||
total_size = int(response.headers['Content-Length'])
|
||||
except:
|
||||
total_size = -1
|
||||
chunk_so_far = "".encode("utf-8")
|
||||
while True:
|
||||
chunk = response.read(CHUNK)
|
||||
bytes_so_far += len(chunk)
|
||||
@ -98,8 +61,54 @@ class Downloader:
|
||||
self._progress_hook(response, bytes_so_far, total_size)
|
||||
if not chunk:
|
||||
break
|
||||
f.write(chunk)
|
||||
if os.path.exists(file):
|
||||
return file
|
||||
else:
|
||||
chunk_so_far += chunk
|
||||
return chunk_so_far.decode("utf-8")
|
||||
except:
|
||||
return None
|
||||
|
||||
def get_bytes(self, url, progress = True):
|
||||
try:
|
||||
response = urlopen(url)
|
||||
CHUNK = 16 * 1024
|
||||
bytes_so_far = 0
|
||||
try:
|
||||
total_size = int(response.headers['Content-Length'])
|
||||
except:
|
||||
total_size = -1
|
||||
chunk_so_far = "".encode("utf-8")
|
||||
while True:
|
||||
chunk = response.read(CHUNK)
|
||||
bytes_so_far += len(chunk)
|
||||
if progress:
|
||||
self._progress_hook(response, bytes_so_far, total_size)
|
||||
if not chunk:
|
||||
break
|
||||
chunk_so_far += chunk
|
||||
return chunk_so_far
|
||||
except:
|
||||
return None
|
||||
|
||||
def stream_to_file(self, url, file, progress = True):
|
||||
try:
|
||||
response = urlopen(url)
|
||||
CHUNK = 16 * 1024
|
||||
bytes_so_far = 0
|
||||
try:
|
||||
total_size = int(response.headers['Content-Length'])
|
||||
except:
|
||||
total_size = -1
|
||||
with open(file, 'wb') as f:
|
||||
while True:
|
||||
chunk = response.read(CHUNK)
|
||||
bytes_so_far += len(chunk)
|
||||
if progress:
|
||||
self._progress_hook(response, bytes_so_far, total_size)
|
||||
if not chunk:
|
||||
break
|
||||
f.write(chunk)
|
||||
if os.path.exists(file):
|
||||
return file
|
||||
else:
|
||||
return None
|
||||
except:
|
||||
return None
|
||||
|
Loading…
Reference in New Issue
Block a user