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
if sys.version_info >= (3, 0):
from urllib.request import urlopen
else:
# Import urllib2 to catch errors
import urllib2
from urllib2 import urlopen
class Downloader:
@ -10,6 +12,23 @@ class Downloader:
def __init__(self):
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):
if size == -1:
return "Unknown"
@ -45,8 +64,9 @@ class Downloader:
sys.stdout.write("Downloaded {}\r".format(b_s))
def get_string(self, url, progress = True):
try:
response = urlopen(url)
response = self.open_url(url)
if not response:
return None
CHUNK = 16 * 1024
bytes_so_far = 0
try:
@ -63,12 +83,11 @@ class Downloader:
break
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)
response = self.open_url(url)
if not response:
return None
CHUNK = 16 * 1024
bytes_so_far = 0
try:
@ -85,12 +104,11 @@ class Downloader:
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)
response = self.open_url(url)
if not response:
return None
CHUNK = 16 * 1024
bytes_so_far = 0
try:
@ -110,5 +128,3 @@ class Downloader:
return file
else:
return None
except:
return None