Add checksum control od db download and switch to urllib3
This commit is contained in:
@@ -2,13 +2,15 @@ from io import BytesIO
|
||||
from os import mkdir
|
||||
import os.path
|
||||
from shutil import rmtree
|
||||
import urllib
|
||||
import urllib3
|
||||
from zipfile import ZipFile
|
||||
from PyQt5.QtCore import QThread, pyqtSignal
|
||||
import utilities
|
||||
|
||||
class DownloadThread(QThread):
|
||||
no_connection_error = pyqtSignal()
|
||||
bad_db_download_error = pyqtSignal()
|
||||
bad_file_error = pyqtSignal()
|
||||
|
||||
def __init__(self, db_location, path):
|
||||
super().__init__()
|
||||
@@ -22,12 +24,11 @@ class DownloadThread(QThread):
|
||||
self.wait()
|
||||
|
||||
def run(self):
|
||||
if os.path.exists(self.__path):
|
||||
rmtree(self.__path)
|
||||
try:
|
||||
db = urllib.request.urlopen(self.__db_location)
|
||||
db = urllib3.PoolManager().request('GET', self.__db_location)
|
||||
# db = urllib.request.urlopen(self.__db_location)
|
||||
# raise urllib.error.URLError('Test')
|
||||
except urllib.error.URLError: # No internet connection.
|
||||
except urllib3.exceptions.MaxRetryError: # No internet connection.
|
||||
self.regular_execution = False
|
||||
self.no_connection_error.emit()
|
||||
return
|
||||
@@ -36,8 +37,18 @@ class DownloadThread(QThread):
|
||||
self.reason = db.reason
|
||||
self.bad_db_download_error.emit()
|
||||
return
|
||||
if not utilities.checksum_ok(db.data, "folder"):
|
||||
regular_execution = False
|
||||
self.bad_db_download_error.emit()
|
||||
return
|
||||
if os.path.exists(self.__path):
|
||||
rmtree(self.__path)
|
||||
try:
|
||||
with ZipFile(BytesIO(db.read())) as zipped:
|
||||
# data_folder = db.read()
|
||||
# data_folder = db.data
|
||||
with ZipFile(BytesIO(db.data)) as zipped:
|
||||
zipped.extractall()
|
||||
except:
|
||||
pass
|
||||
self.regular_execution = False
|
||||
self.bad_file_error.emit()
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user