Refactor get_config_dict function to simplify argument handling in nodriver_tixcraft.py

master
Yong-Jer Chuang 2024-04-24 13:14:07 +08:00
parent 9de59ae498
commit f078895d3d
1 changed files with 28 additions and 46 deletions

View File

@ -111,8 +111,7 @@ def get_config_dict(args):
config_filepath = os.path.join(app_root, CONST_MAXBOT_CONFIG_FILE) config_filepath = os.path.join(app_root, CONST_MAXBOT_CONFIG_FILE)
# allow assign config by command line. # allow assign config by command line.
if not args.input is None: if args.input and len(args.input) > 0:
if len(args.input) > 0:
config_filepath = args.input config_filepath = args.input
config_dict = None config_dict = None
@ -121,48 +120,33 @@ def get_config_dict(args):
with open(config_filepath) as json_data: with open(config_filepath) as json_data:
config_dict = json.load(json_data) config_dict = json.load(json_data)
if not args.headless is None: # Define a dictionary to map argument names to their paths in the config_dict
config_dict["advanced"]["headless"] = util.t_or_f(args.headless) arg_to_path = {
"headless": ["advanced", "headless"],
"homepage": ["homepage"],
"ticket_number": ["ticket_number"],
"browser": ["browser"],
"tixcraft_sid": ["advanced", "tixcraft_sid"],
"ibonqware": ["advanced", "ibonqware"],
"kktix_account": ["advanced", "kktix_account"],
"kktix_password": ["advanced", "kktix_password_plaintext"],
"proxy_server": ["advanced", "proxy_server_port"],
"window_size": ["advanced", "window_size"]
}
if not args.homepage is None: # Update the config_dict based on the arguments
if len(args.homepage) > 0: for arg, path in arg_to_path.items():
config_dict["homepage"] = args.homepage value = getattr(args, arg)
if value and len(str(value)) > 0:
if not args.ticket_number is None: d = config_dict
if args.ticket_number > 0: for key in path[:-1]:
config_dict["ticket_number"] = args.ticket_number d = d[key]
d[path[-1]] = value
if not args.browser is None:
if len(args.browser) > 0:
config_dict["browser"] = args.browser
if not args.tixcraft_sid is None:
if len(args.tixcraft_sid) > 0:
config_dict["advanced"]["tixcraft_sid"] = args.tixcraft_sid
if not args.ibonqware is None:
if len(args.ibonqware) > 0:
config_dict["advanced"]["ibonqware"] = args.ibonqware
if not args.kktix_account is None:
if len(args.kktix_account) > 0:
config_dict["advanced"]["kktix_account"] = args.kktix_account
if not args.kktix_password is None:
if len(args.kktix_password) > 0:
config_dict["advanced"]["kktix_password_plaintext"] = args.kktix_password
if not args.proxy_server is None:
if len(args.proxy_server) > 2:
config_dict["advanced"]["proxy_server_port"] = args.proxy_server
if not args.window_size is None:
if len(args.window_size) > 2:
config_dict["advanced"]["window_size"] = args.window_size
# special case for headless to enable away from keyboard mode. # special case for headless to enable away from keyboard mode.
is_headless_enable_ocr = False is_headless_enable_ocr = False
if config_dict["advanced"]["headless"]: if config_dict["advanced"]["headless"]:
# for tixcraft headless. # for tixcraft headless.
#print("If you are runnig headless mode on tixcraft, you need input your cookie SID.")
if len(config_dict["advanced"]["tixcraft_sid"]) > 1: if len(config_dict["advanced"]["tixcraft_sid"]) > 1:
is_headless_enable_ocr = True is_headless_enable_ocr = True
@ -207,20 +191,18 @@ async def nodriver_press_button(tab, select_query):
print(e) print(e)
pass pass
async def nodriver_check_checkbox(tab, select_query, value='true'): from typing import Optional
is_checkbox_checked = False
async def nodriver_check_checkbox(tab: Optional[object], select_query: str, value: str = 'true') -> bool:
if tab: if tab:
try: try:
element = await tab.query_selector(select_query) element = await tab.query_selector(select_query)
if element: if element:
#await element.apply('function (element) { element.checked='+ value +'; } ')
await element.click() await element.click()
is_checkbox_checked = True return True
except Exception as exc: except Exception as exc:
#print("check checkbox fail for selector:", select_query)
print(exc) print(exc)
pass return False
return is_checkbox_checked
async def nodriver_facebook_login(tab, facebook_account, facebook_password): async def nodriver_facebook_login(tab, facebook_account, facebook_password):
if tab: if tab: