Add internet connection check and rewrite some parts

This commit is contained in:
Hoang Hong Quan
2024-12-10 12:17:24 +07:00
parent a469db2520
commit e75282052d

69
Scripts/resource_fetcher.py Executable file → Normal file
View File

@@ -1,8 +1,8 @@
import sys
import ssl
import os
import plistlib
import json
import plistlib
import socket
import sys
if sys.version_info >= (3, 0):
from urllib.request import urlopen, Request
@@ -11,26 +11,17 @@ else:
from urllib2 import urlopen, Request
class ResourceFetcher:
def __init__(self, headers=None):
def __init__(self, headers={}):
self.request_headers = headers
self.buffer_size = 16 * 1024
self.ssl_context = self.create_ssl_context()
def create_ssl_context(self):
try:
cafile = ssl.get_default_verify_paths().openssl_cafile
if not os.path.exists(cafile):
import certifi
cafile = certifi.where()
ssl_context = ssl.create_default_context(cafile=cafile)
except Exception as e:
print("SSL Context Creation Error: {}".format(e))
ssl_context = ssl._create_unverified_context()
return ssl_context
def is_connected(self, timeout=5):
socket.create_connection(("github.com", 443), timeout=timeout)
def fetch_and_parse_content(self, resource_url, content_type=None):
request = Request(resource_url, headers=self.request_headers or {})
with urlopen(request, context=self.ssl_context) as response:
self.is_connected()
with urlopen(resource_url) as response:
content = response.read()
if content_type == 'json':
return json.loads(content)
@@ -39,19 +30,31 @@ class ResourceFetcher:
else:
return content.decode('utf-8')
def download_and_save_file(self, resource_url, destination_path):
request = Request(resource_url, headers=self.request_headers or {})
with urlopen(request, context=self.ssl_context) as response:
total_size = response.length
bytes_downloaded = 0
print("Download from {}".format(resource_url))
def _download_with_progress(self, response, local_file):
total_size = response.getheader('Content-Length')
if total_size:
total_size = int(total_size)
bytes_downloaded = 0
with open(destination_path, 'wb') as file_writer:
while True:
chunk = response.read(self.buffer_size)
if not chunk:
break
file_writer.write(chunk)
bytes_downloaded += len(chunk)
if total_size:
print("Downloaded {:.2f} MB of {:.2f} MB".format(bytes_downloaded / (1024 * 1024), total_size / (1024 * 1024)), end='\r')
while True:
chunk = response.read(self.buffer_size)
if not chunk:
break
local_file.write(chunk)
bytes_downloaded += len(chunk)
if total_size:
percent = int(bytes_downloaded / total_size * 100)
progress = f"[{'=' * (percent // 2):50s}] {percent}% {bytes_downloaded / (1024 * 1024):.2f}/{total_size / (1024 * 1024):.2f} MB"
else:
progress = f"Downloaded {bytes_downloaded / (1024 * 1024):.2f} MB"
print(progress, end='\r')
print()
def download_and_save_file(self, resource_url, destination_path):
self.is_connected()
with urlopen(resource_url) as response, open(destination_path, 'wb') as local_file:
print(f"Downloading from {resource_url}")
self._download_with_progress(response, local_file)