diff --git a/plugins/Sidebar/SidebarPlugin.py b/plugins/Sidebar/SidebarPlugin.py index c117de0e8..b8c5f0f3a 100644 --- a/plugins/Sidebar/SidebarPlugin.py +++ b/plugins/Sidebar/SidebarPlugin.py @@ -602,7 +602,7 @@ def actionSidebarGetHtmlTag(self, to): def downloadGeoLiteDb(self, db_path): import gzip import shutil - from util import helper + import requests if config.offline or config.tor == 'always': return False @@ -617,19 +617,18 @@ def downloadGeoLiteDb(self, db_path): downloadl_err = None try: # Download - response = helper.httpRequest(db_url) - data_size = response.getheader('content-length') + response = requests.get(db_url, stream=True) + data_size = response.headers.get('content-length') + if data_size is None: + data.write(response.content) + data_size = int(data_size) data_recv = 0 data = io.BytesIO() - while True: - buff = response.read(1024 * 512) - if not buff: - break + for buff in response.iter_content(chunk_size=1024 * 512): data.write(buff) data_recv += 1024 * 512 - if data_size: - progress = int(float(data_recv) / int(data_size) * 100) - self.cmd("progress", ["geolite-info", _["Downloading GeoLite2 City database (one time only, ~20MB)..."], progress]) + progress = int(float(data_recv) / data_size * 100) + self.cmd("progress", ["geolite-info", _["Downloading GeoLite2 City database (one time only, ~20MB)..."], progress]) self.log.info("GeoLite2 City database downloaded (%s bytes), unpacking..." % data.tell()) data.seek(0) diff --git a/src/util/helper.py b/src/util/helper.py index a0daa557f..af65f7279 100644 --- a/src/util/helper.py +++ b/src/util/helper.py @@ -195,44 +195,6 @@ def mergeDicts(dicts): return dict(back) -# Request https url using gevent SSL error workaround -def httpRequest(url, as_file=False): - if url.startswith("http://"): - import urllib.request - response = urllib.request.urlopen(url) - else: # Hack to avoid Python gevent ssl errors - import socket - import http.client - import ssl - - host, request = re.match("https://(.*?)(/.*?)$", url).groups() - - conn = http.client.HTTPSConnection(host) - sock = socket.create_connection((conn.host, conn.port), conn.timeout, conn.source_address) - - context = ssl.create_default_context() - context.minimum_version = ssl.TLSVersion.TLSv1_2 - - conn.sock = context.wrap_socket(sock, conn.key_file, conn.cert_file) - conn.request("GET", request) - response = conn.getresponse() - if response.status in [301, 302, 303, 307, 308]: - logging.info("Redirect to: %s" % response.getheader('Location')) - response = httpRequest(response.getheader('Location')) - - if as_file: - import io - data = io.BytesIO() - while True: - buff = response.read(1024 * 16) - if not buff: - break - data.write(buff) - return data - else: - return response - - def timerCaller(secs, func, *args, **kwargs): gevent.spawn_later(secs, timerCaller, secs, func, *args, **kwargs) func(*args, **kwargs)