Fixes for py2 on Sierra

This commit is contained in:
CorpNewt 2018-11-09 09:30:39 -06:00 committed by GitHub
parent 9dfb846041
commit 340ba88150
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,8 +1,10 @@
import sys, os, time import sys, os, time, ssl
# Python-aware urllib stuff # Python-aware urllib stuff
if sys.version_info >= (3, 0): if sys.version_info >= (3, 0):
from urllib.request import urlopen from urllib.request import urlopen
else: else:
# Import urllib2 to catch errors
import urllib2
from urllib2 import urlopen from urllib2 import urlopen
class Downloader: class Downloader:
@ -10,6 +12,23 @@ class Downloader:
def __init__(self): def __init__(self):
return return
def open_url(self, url):
# Wrap up the try/except block so we don't have to do this for each function
try:
response = urlopen(url)
except Exception as e:
if sys.version_info >= (3, 0) or not (isinstance(e, urllib2.URLError) and "CERTIFICATE_VERIFY_FAILED" in str(e)):
# Either py3, or not the right error for this "fix"
return None
# Py2 and a Cert verify error - let's set the unverified context
context = ssl._create_unverified_context()
try:
response = urlopen(url, context=context)
except:
# No fixing this - bail
return None
return response
def get_size(self, size, suff=None): def get_size(self, size, suff=None):
if size == -1: if size == -1:
return "Unknown" return "Unknown"
@ -45,37 +64,58 @@ class Downloader:
sys.stdout.write("Downloaded {}\r".format(b_s)) sys.stdout.write("Downloaded {}\r".format(b_s))
def get_string(self, url, progress = True): def get_string(self, url, progress = True):
try: response = self.open_url(url)
response = urlopen(url) if not response:
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")
except:
return None return None
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): def get_bytes(self, url, progress = True):
response = self.open_url(url)
if not response:
return None
CHUNK = 16 * 1024
bytes_so_far = 0
try: try:
response = urlopen(url) total_size = int(response.headers['Content-Length'])
CHUNK = 16 * 1024 except:
bytes_so_far = 0 total_size = -1
try: chunk_so_far = "".encode("utf-8")
total_size = int(response.headers['Content-Length']) while True:
except: chunk = response.read(CHUNK)
total_size = -1 bytes_so_far += len(chunk)
chunk_so_far = "".encode("utf-8") 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 = self.open_url(url)
if not response:
return None
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: while True:
chunk = response.read(CHUNK) chunk = response.read(CHUNK)
bytes_so_far += len(chunk) bytes_so_far += len(chunk)
@ -83,32 +123,8 @@ class Downloader:
self._progress_hook(response, bytes_so_far, total_size) self._progress_hook(response, bytes_so_far, total_size)
if not chunk: if not chunk:
break break
chunk_so_far += chunk f.write(chunk)
return chunk_so_far if os.path.exists(file):
except: return file
return None else:
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 return None