mirror of
https://github.com/corpnewt/gibMacOS.git
synced 2025-02-15 00:51:28 +01:00
Fixes for py2 on Sierra
This commit is contained in:
parent
9dfb846041
commit
340ba88150
@ -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,8 +64,9 @@ 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:
|
||||||
|
return None
|
||||||
CHUNK = 16 * 1024
|
CHUNK = 16 * 1024
|
||||||
bytes_so_far = 0
|
bytes_so_far = 0
|
||||||
try:
|
try:
|
||||||
@ -63,12 +83,11 @@ class Downloader:
|
|||||||
break
|
break
|
||||||
chunk_so_far += chunk
|
chunk_so_far += chunk
|
||||||
return chunk_so_far.decode("utf-8")
|
return chunk_so_far.decode("utf-8")
|
||||||
except:
|
|
||||||
return None
|
|
||||||
|
|
||||||
def get_bytes(self, url, progress = True):
|
def get_bytes(self, url, progress = True):
|
||||||
try:
|
response = self.open_url(url)
|
||||||
response = urlopen(url)
|
if not response:
|
||||||
|
return None
|
||||||
CHUNK = 16 * 1024
|
CHUNK = 16 * 1024
|
||||||
bytes_so_far = 0
|
bytes_so_far = 0
|
||||||
try:
|
try:
|
||||||
@ -85,12 +104,11 @@ class Downloader:
|
|||||||
break
|
break
|
||||||
chunk_so_far += chunk
|
chunk_so_far += chunk
|
||||||
return chunk_so_far
|
return chunk_so_far
|
||||||
except:
|
|
||||||
return None
|
|
||||||
|
|
||||||
def stream_to_file(self, url, file, progress = True):
|
def stream_to_file(self, url, file, progress = True):
|
||||||
try:
|
response = self.open_url(url)
|
||||||
response = urlopen(url)
|
if not response:
|
||||||
|
return None
|
||||||
CHUNK = 16 * 1024
|
CHUNK = 16 * 1024
|
||||||
bytes_so_far = 0
|
bytes_so_far = 0
|
||||||
try:
|
try:
|
||||||
@ -110,5 +128,3 @@ class Downloader:
|
|||||||
return file
|
return file
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
except:
|
|
||||||
return None
|
|
||||||
|
Loading…
Reference in New Issue
Block a user