Update util.py

master
melia19791012 2024-04-23 21:37:46 +08:00 committed by GitHub
parent 41dbb749a2
commit 0f50dd8bcc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 43 additions and 16 deletions

33
util.py
View File

@ -25,19 +25,25 @@ def get_ip_address():
try:
gethostname = socket.gethostname()
except Exception as exc:
print(exc)
print("gethostname", exc)
gethostname = None
default_ip = "127.0.0.1"
ip = default_ip
if not gethostname is None:
check_public_ip = True
if "macos" in platform.platform().lower():
if "arm64" in platform.platform().lower():
check_public_ip = False
if check_public_ip and not gethostname is None:
try:
ip = [l for l in ([ip for ip in socket.gethostbyname_ex(gethostname)[2]
if not ip.startswith("127.")][:1], [[(s.connect(('8.8.8.8', 53)),
s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET,
socket.SOCK_DGRAM)]][0][1]]) if l][0][0]
except Exception as exc:
print(exc)
print("gethostbyname_ex", exc)
ip = gethostname
#print("get_ip_address:", ip)
@ -162,8 +168,11 @@ def is_text_match_keyword(keyword_string, text):
def save_json(config_dict, target_path):
json_str = json.dumps(config_dict, indent=4)
try:
with open(target_path, 'w') as outfile:
outfile.write(json_str)
except Exception as e:
pass
def write_string_to_file(filename, data):
outfile = None
@ -448,8 +457,12 @@ def dump_settings_to_maxbot_plus_extension(ext, config_dict, CONST_MAXBOT_CONFIG
os.unlink(target_path)
except Exception as exc:
pass
try:
with open(target_path, 'w') as outfile:
json.dump(config_dict, outfile)
except Exception as e:
pass
# add host_permissions
target_path = ext
@ -457,8 +470,11 @@ def dump_settings_to_maxbot_plus_extension(ext, config_dict, CONST_MAXBOT_CONFIG
manifest_dict = None
if os.path.isfile(target_path):
try:
with open(target_path) as json_data:
manifest_dict = json.load(json_data)
except Exception as e:
pass
local_remote_url_array = []
local_remote_url = config_dict["advanced"]["remote_url"]
@ -482,8 +498,11 @@ def dump_settings_to_maxbot_plus_extension(ext, config_dict, CONST_MAXBOT_CONFIG
if is_manifest_changed:
json_str = json.dumps(manifest_dict, indent=4)
try:
with open(target_path, 'w') as outfile:
outfile.write(json_str)
except Exception as e:
pass
def dump_settings_to_maxblock_plus_extension(ext, config_dict, CONST_MAXBOT_CONFIG_FILE, CONST_MAXBLOCK_EXTENSION_FILTER):
@ -501,9 +520,13 @@ def dump_settings_to_maxblock_plus_extension(ext, config_dict, CONST_MAXBOT_CONF
os.unlink(target_path)
except Exception as exc:
pass
try:
with open(target_path, 'w') as outfile:
config_dict["domain_filter"]=CONST_MAXBLOCK_EXTENSION_FILTER
json.dump(config_dict, outfile)
except Exception as e:
pass
# convert web string to reg pattern
def convert_string_to_pattern(my_str, dynamic_length=True):
@ -1375,8 +1398,12 @@ def get_answer_list_from_user_guess_string(config_dict, CONST_MAXBOT_ANSWER_ONLI
# load from internet.
user_guess_string = ""
if os.path.exists(CONST_MAXBOT_ANSWER_ONLINE_FILE):
try:
with open(CONST_MAXBOT_ANSWER_ONLINE_FILE, "r") as text_file:
user_guess_string = text_file.readline()
except Exception as e:
pass
if len(user_guess_string) > 0:
user_guess_string = format_config_keyword_for_json(user_guess_string)
try: