From be5f277ddc6ffa4103928e9845e76f1168618671 Mon Sep 17 00:00:00 2001 From: CorpNewt Date: Sun, 7 Oct 2018 18:08:45 -0500 Subject: [PATCH] Wrap in try/except --- Scripts/downloader.py | 107 +++++++++++++++++++++++------------------- 1 file changed, 58 insertions(+), 49 deletions(-) diff --git a/Scripts/downloader.py b/Scripts/downloader.py index bd2fb88..80a25dd 100644 --- a/Scripts/downloader.py +++ b/Scripts/downloader.py @@ -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