From 6715769c9c82d77a4b74d6c46ff00e0f50af9bd9 Mon Sep 17 00:00:00 2001 From: Yong-Jer Chuang Date: Fri, 26 Apr 2024 14:28:11 +0800 Subject: [PATCH 1/8] Refactor config_launcher.py to simplify code and improve readability --- config_launcher.py | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/config_launcher.py b/config_launcher.py index 9644ef4..c933524 100644 --- a/config_launcher.py +++ b/config_launcher.py @@ -183,32 +183,22 @@ def btn_save_act(slience_mode=True): config_dict = get_default_config() language_code = get_language_code_by_name(config_dict["advanced"]["language"]) - is_all_data_correct = True - global combo_language - if is_all_data_correct: - config_dict["advanced"]["language"] = combo_language.get().strip() - # display as new language. - language_code = get_language_code_by_name(config_dict["advanced"]["language"]) - + config_dict["advanced"]["language"] = combo_language.get().strip() + # display as new language. + language_code = get_language_code_by_name(config_dict["advanced"]["language"]) global txt_file_name - filelist = [] - for i in range(15): - filelist.append(txt_file_name[i].get().strip()) - - if is_all_data_correct: - config_dict["list"]=filelist + filelist = [txt_file_name[i].get().strip() for i in range(15)] + config_dict["list"] = filelist # save config. - if is_all_data_correct: - # slience - util.save_json(config_dict, config_filepath) - - if not slience_mode: - messagebox.showinfo(translate[language_code]["save"], translate[language_code]["done"]) + util.save_json(config_dict, config_filepath) + + if not slience_mode: + messagebox.showinfo(translate[language_code]["save"], translate[language_code]["done"]) - return is_all_data_correct + return True def open_url(url): webbrowser.open_new(url) From d35c3bea323ab38859b2c1a34ab7a89d4955bc2f Mon Sep 17 00:00:00 2001 From: Yong-Jer Chuang Date: Fri, 26 Apr 2024 14:31:59 +0800 Subject: [PATCH 2/8] Refactor config_launcher.py to improve code organization and UI readability --- config_launcher.py | 59 +++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 38 deletions(-) diff --git a/config_launcher.py b/config_launcher.py index c933524..14a65c7 100644 --- a/config_launcher.py +++ b/config_launcher.py @@ -297,50 +297,33 @@ def btn_items_run_event(event): threading.Thread(target=util.launch_maxbot, args=(script_name,filename,)).start() def ConfigListTab(root, config_dict, language_code, UI_PADDING_X): - - # output to GUI. - row_count = 0 - frame_group_header = Frame(root) - group_row_count = 0 + widgets = { + 'lbl_file_name': {}, + 'txt_file_name': {}, + 'txt_file_name_value': {}, + 'btn_browse': {}, + 'btn_run': {}, + } - global lbl_file_name - global txt_file_name - global txt_file_name_value - global btn_browse - global btn_run - lbl_file_name = {} - txt_file_name = {} - txt_file_name_value = {} - btn_browse = {} - btn_run = {} + for i, filename in enumerate(config_dict["list"][:15]): + widgets['lbl_file_name'][i] = Label(frame_group_header, text=str(i+1)) + widgets['lbl_file_name'][i].grid(column=0, row=i, sticky=E) - print("config_dict[list]:",config_dict["list"]) - print("config_dict[list]:",len(config_dict["list"])) - for i in range(15): - filename = "" - if i <= len(config_dict["list"])-1: - filename = config_dict["list"][i] - lbl_file_name[i] = Label(frame_group_header, text=str(i+1)) - lbl_file_name[i].grid(column=0, row=group_row_count, sticky = E) + widgets['txt_file_name_value'][i] = StringVar(frame_group_header, value=filename) + widgets['txt_file_name'][i] = Entry(frame_group_header, width=20, textvariable=widgets['txt_file_name_value'][i]) + widgets['txt_file_name'][i].grid(column=1, row=i, sticky=W) - txt_file_name_value[i] = StringVar(frame_group_header, value=filename) - txt_file_name[i] = Entry(frame_group_header, width=20, textvariable = txt_file_name_value[i]) - txt_file_name[i].grid(column=1, row=group_row_count, sticky = W) + widgets['btn_browse'][i] = ttk.Button(frame_group_header, text=translate[language_code]['browse'] + " " + str(i+1)) + widgets['btn_browse'][i].grid(column=2, row=i, sticky=W) + widgets['btn_browse'][i].bind('', btn_items_browse_event) - btn_browse[i] = ttk.Button(frame_group_header, text=translate[language_code]['browse'] + " " + str(i+1)) - btn_browse[i].grid(column=2, row=group_row_count, sticky = W) - btn_browse[i].bind('', btn_items_browse_event) + widgets['btn_run'][i] = ttk.Button(frame_group_header, text=translate[language_code]['run'] + " " + str(i+1)) + widgets['btn_run'][i].grid(column=3, row=i, sticky=W) + widgets['btn_run'][i].bind('', btn_items_run_event) - btn_run[i] = ttk.Button(frame_group_header, text=translate[language_code]['run'] + " " + str(i+1)) - btn_run[i].grid(column=3, row=group_row_count, sticky = W) - btn_run[i].bind('', btn_items_run_event) - - group_row_count+=1 - - - # add first block to UI. - frame_group_header.grid(column=0, row=row_count, sticky = W, padx=UI_PADDING_X) + frame_group_header.grid(column=0, row=0, sticky=W, padx=UI_PADDING_X) + return widgets def AboutTab(root, language_code): From fe1918adfd0b3b3c1197d02c155c5fa6871cd778 Mon Sep 17 00:00:00 2001 From: Yong-Jer Chuang Date: Fri, 26 Apr 2024 14:33:50 +0800 Subject: [PATCH 3/8] Refactor language translations in config_launcher.py for improved readability --- config_launcher.py | 170 ++++++++++++++++++++------------------------- 1 file changed, 77 insertions(+), 93 deletions(-) diff --git a/config_launcher.py b/config_launcher.py index 14a65c7..f9b6f67 100644 --- a/config_launcher.py +++ b/config_launcher.py @@ -40,99 +40,83 @@ URL_FIREFOX_DRIVER = 'https://github.com/mozilla/geckodriver/releases' URL_EDGE_DRIVER = 'https://developer.microsoft.com/zh-tw/microsoft-edge/tools/webdriver/' def load_translate(): - translate = {} - en_us={} - en_us["language"] = 'Language' - en_us["enable"] = 'Enable' - - en_us["config_list"] = 'Config List' - en_us["advanced"] = 'Advanced' - en_us["about"] = 'About' - - en_us["run"] = 'Run' - en_us["browse"] = 'Browse...' - en_us["save"] = 'Save' - en_us["exit"] = 'Close' - en_us["copy"] = 'Copy' - en_us["restore_defaults"] = 'Restore Defaults' - en_us["done"] = 'Done' - - en_us["maxbot_slogan"] = 'MaxRegBot is a FREE and open source bot program. Wish you good luck.' - en_us["donate"] = 'Donate' - en_us["help"] = 'Help' - en_us["release"] = 'Release' - - zh_tw={} - zh_tw["language"] = '語言' - zh_tw["enable"] = '啟用' - - zh_tw["config_list"] = '設定檔管理' - zh_tw["advanced"] = '進階設定' - zh_tw["autofill"] = '自動填表單' - zh_tw["about"] = '關於' - - zh_tw["run"] = '搶票' - zh_tw["browse"] = '開啟...' - zh_tw["save"] = '存檔' - zh_tw["exit"] = '關閉' - zh_tw["copy"] = '複製' - zh_tw["restore_defaults"] = '恢復預設值' - zh_tw["done"] = '完成' - - zh_tw["maxbot_slogan"] = 'MaxRegBot是一個免費、開放原始碼的搶票機器人。\n祝您掛號成功。' - zh_tw["donate"] = '打賞' - zh_tw["release"] = '所有可用版本' - zh_tw["help"] = '使用教學' - - zh_cn={} - zh_cn["language"] = '语言' - zh_cn["enable"] = '启用' - - zh_cn["config_list"] = '设定档管理' - zh_cn["advanced"] = '進階設定' - zh_cn["autofill"] = '自动填表单' - zh_cn["about"] = '关于' - zh_cn["copy"] = '复制' - - zh_cn["run"] = '抢票' - zh_cn["browse"] = '开启...' - zh_cn["save"] = '存档' - zh_cn["exit"] = '关闭' - zh_cn["copy"] = '复制' - zh_cn["restore_defaults"] = '恢复默认值' - zh_cn["done"] = '完成' - - zh_cn["maxbot_slogan"] = 'MaxRegBot 是一个免费的开源机器人程序。\n祝您挂号成功。' - zh_cn["donate"] = '打赏' - zh_cn["help"] = '使用教学' - zh_cn["release"] = '所有可用版本' - - ja_jp={} - ja_jp["language"] = '言語' - ja_jp["enable"] = '有効' - - ja_jp["config_list"] = 'Config List' - ja_jp["advanced"] = '高度な設定' - ja_jp["autofill"] = 'オートフィル' - ja_jp["about"] = '情報' - - ja_jp["run"] = 'チケットを取る' - ja_jp["browse"] = '開ける...' - ja_jp["save"] = '保存' - ja_jp["exit"] = '閉じる' - ja_jp["copy"] = 'コピー' - ja_jp["restore_defaults"] = 'デフォルトに戻す' - ja_jp["done"] = '終わり' - - ja_jp["maxbot_slogan"] = 'MaxRegBot は無料のオープン ソース ボット プログラムです。チケットの成功をお祈りします。' - ja_jp["donate"] = '寄付' - ja_jp["help"] = '利用方法' - ja_jp["release"] = 'リリース' - - translate['en_us']=en_us - translate['zh_tw']=zh_tw - translate['zh_cn']=zh_cn - translate['ja_jp']=ja_jp + translate = { + 'en_us': { + "language": 'Language', + "enable": 'Enable', + "config_list": 'Config List', + "advanced": 'Advanced', + "about": 'About', + "run": 'Run', + "browse": 'Browse...', + "save": 'Save', + "exit": 'Close', + "copy": 'Copy', + "restore_defaults": 'Restore Defaults', + "done": 'Done', + "maxbot_slogan": 'MaxRegBot is a FREE and open source bot program. Wish you good luck.', + "donate": 'Donate', + "help": 'Help', + "release": 'Release' + }, + 'zh_tw': { + "language": '語言', + "enable": '啟用', + "config_list": '設定檔管理', + "advanced": '進階設定', + "autofill": '自動填表單', + "about": '關於', + "run": '搶票', + "browse": '開啟...', + "save": '存檔', + "exit": '關閉', + "copy": '複製', + "restore_defaults": '恢復預設值', + "done": '完成', + "maxbot_slogan": 'MaxRegBot是一個免費、開放原始碼的搶票機器人。\n祝您掛號成功。', + "donate": '打賞', + "release": '所有可用版本', + "help": '使用教學' + }, + 'zh_cn': { + "language": '语言', + "enable": '启用', + "config_list": '设定档管理', + "advanced": '進階設定', + "autofill": '自动填表单', + "about": '关于', + "run": '抢票', + "browse": '开启...', + "save": '存档', + "exit": '关闭', + "copy": '复制', + "restore_defaults": '恢复默认值', + "done": '完成', + "maxbot_slogan": 'MaxRegBot 是一个免费的开源机器人程序。\n祝您挂号成功。', + "donate": '打赏', + "help": '使用教学', + "release": '所有可用版本' + }, + 'ja_jp': { + "language": '言語', + "enable": '有効', + "config_list": 'Config List', + "advanced": '高度な設定', + "autofill": 'オートフィル', + "about": '情報', + "run": 'チケットを取る', + "browse": '開ける...', + "save": '保存', + "exit": '閉じる', + "copy": 'コピー', + "restore_defaults": 'デフォルトに戻す', + "done": '終わり', + "maxbot_slogan": 'MaxRegBot は無料のオープン ソース ボット プログラムです。チケットの成功をお祈りします。', + "donate": '寄付', + "help": '利用方法', + "release": 'リリース' + } + } return translate def get_default_config(): From 6503612e20e8770f36d5f97017361687df9bfc09 Mon Sep 17 00:00:00 2001 From: Yong-Jer Chuang Date: Fri, 26 Apr 2024 14:35:46 +0800 Subject: [PATCH 4/8] Refactor language translations in config_launcher.py for improved readability --- config_launcher.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/config_launcher.py b/config_launcher.py index f9b6f67..ed0fd10 100644 --- a/config_launcher.py +++ b/config_launcher.py @@ -167,16 +167,12 @@ def btn_save_act(slience_mode=True): config_dict = get_default_config() language_code = get_language_code_by_name(config_dict["advanced"]["language"]) - global combo_language config_dict["advanced"]["language"] = combo_language.get().strip() - # display as new language. language_code = get_language_code_by_name(config_dict["advanced"]["language"]) - global txt_file_name filelist = [txt_file_name[i].get().strip() for i in range(15)] config_dict["list"] = filelist - # save config. util.save_json(config_dict, config_filepath) if not slience_mode: From 163810e01ad2c54bc22f6181de307f41b11b9229 Mon Sep 17 00:00:00 2001 From: Yong-Jer Chuang Date: Fri, 26 Apr 2024 14:36:43 +0800 Subject: [PATCH 5/8] Refactor language translations in config_launcher.py for improved readability --- config_launcher.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/config_launcher.py b/config_launcher.py index ed0fd10..57a5190 100644 --- a/config_launcher.py +++ b/config_launcher.py @@ -197,14 +197,12 @@ def callbackLanguageOnChange(event): def get_language_code_by_name(new_language): language_code = "en_us" - if u'繁體中文' in new_language: + if '繁體中文' in new_language: language_code = 'zh_tw' - if u'簡体中文' in new_language: + if '簡体中文' in new_language: language_code = 'zh_cn' - if u'日本語' in new_language: + if '日本語' in new_language: language_code = 'ja_jp' - #print("new language code:", language_code) - return language_code def applyNewLanguage(): From 0c8a44c447d49d4d562bb848a52dcff5eaf4dbd4 Mon Sep 17 00:00:00 2001 From: Yong-Jer Chuang Date: Fri, 26 Apr 2024 14:39:10 +0800 Subject: [PATCH 6/8] Refactor get_config_dict function in chrome_tixcraft.py for improved readability and code organization --- chrome_tixcraft.py | 52 +++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/chrome_tixcraft.py b/chrome_tixcraft.py index a38939a..b2a748e 100644 --- a/chrome_tixcraft.py +++ b/chrome_tixcraft.py @@ -142,9 +142,8 @@ def get_config_dict(args): config_filepath = os.path.join(app_root, CONST_MAXBOT_CONFIG_FILE) # allow assign config by command line. - if not args.input is None: - if len(args.input) > 0: - config_filepath = args.input + if args.input: + config_filepath = args.input config_dict = None if os.path.isfile(config_filepath): @@ -152,43 +151,34 @@ def get_config_dict(args): with open(config_filepath) as json_data: config_dict = json.load(json_data) - if not args.headless is None: + if args.headless is not None: config_dict["advanced"]["headless"] = util.t_or_f(args.headless) - if not args.homepage is None: - if len(args.homepage) > 0: - config_dict["homepage"] = args.homepage + if args.homepage: + config_dict["homepage"] = args.homepage - if not args.ticket_number is None: - if args.ticket_number > 0: - config_dict["ticket_number"] = args.ticket_number + if args.ticket_number: + config_dict["ticket_number"] = args.ticket_number - if not args.browser is None: - if len(args.browser) > 0: - config_dict["browser"] = args.browser + if args.browser: + 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 args.tixcraft_sid: + 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 args.ibonqware: + 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 args.kktix_account: + config_dict["advanced"]["kktix_account"] = args.kktix_account + if args.kktix_password: + 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 args.proxy_server: + 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 + if args.window_size: + config_dict["advanced"]["window_size"] = args.window_size # special case for headless to enable away from keyboard mode. is_headless_enable_ocr = False From fa575ceea432ea91382fed466177b11f8a49b29d Mon Sep 17 00:00:00 2001 From: Yong-Jer Chuang Date: Fri, 26 Apr 2024 14:40:43 +0800 Subject: [PATCH 7/8] Refactor load_chromdriver_normal function in chrome_tixcraft.py for improved code organization and error handling --- chrome_tixcraft.py | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/chrome_tixcraft.py b/chrome_tixcraft.py index b2a748e..283b11d 100644 --- a/chrome_tixcraft.py +++ b/chrome_tixcraft.py @@ -315,11 +315,7 @@ def get_chrome_options(webdriver_path, config_dict): return chrome_options def load_chromdriver_normal(config_dict, driver_type): - show_debug_message = True # debug. - show_debug_message = False # online - - if config_dict["advanced"]["verbose"]: - show_debug_message = True + show_debug_message = config_dict["advanced"]["verbose"] driver = None @@ -327,8 +323,7 @@ def load_chromdriver_normal(config_dict, driver_type): webdriver_path = os.path.join(Root_Dir, "webdriver") chromedriver_path = get_chromedriver_path(webdriver_path) - if not os.path.exists(webdriver_path): - os.mkdir(webdriver_path) + os.makedirs(webdriver_path, exist_ok=True) if not os.path.exists(chromedriver_path): print("WebDriver not exist, try to download to:", webdriver_path) @@ -347,11 +342,7 @@ def load_chromdriver_normal(config_dict, driver_type): error_message = str(exc) if show_debug_message: print(exc) - left_part = None - if "Stacktrace:" in error_message: - left_part = error_message.split("Stacktrace:")[0] - print(left_part) - + left_part = error_message.split("Stacktrace:")[0] if "Stacktrace:" in error_message else None if "This version of ChromeDriver only supports Chrome version" in error_message: print(CONST_CHROME_VERSION_NOT_MATCH_EN) print(CONST_CHROME_VERSION_NOT_MATCH_TW) From 49723dd1e1e303e15f38b8e874967189959701d2 Mon Sep 17 00:00:00 2001 From: Yong-Jer Chuang Date: Fri, 26 Apr 2024 14:43:21 +0800 Subject: [PATCH 8/8] Refactor exception handling in load_chromdriver_normal function in chrome_tixcraft.py for improved error reporting --- chrome_tixcraft.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/chrome_tixcraft.py b/chrome_tixcraft.py index 283b11d..6944183 100644 --- a/chrome_tixcraft.py +++ b/chrome_tixcraft.py @@ -338,7 +338,7 @@ def load_chromdriver_normal(config_dict, driver_type): chrome_options = get_chrome_options(webdriver_path, config_dict) try: driver = webdriver.Chrome(service=chrome_service, options=chrome_options) - except Exception as exc: + except WebDriverException as exc: error_message = str(exc) if show_debug_message: print(exc) @@ -360,12 +360,12 @@ def load_chromdriver_normal(config_dict, driver_type): try: chrome_options = get_chrome_options(webdriver_path, config_dict) driver = webdriver.Chrome(service=chrome_service, options=chrome_options) - except Exception as exc2: + except WebDriverException as exc2: print("Selenium 4.11.0 Release with Chrome For Testing Browser.") try: chrome_options = get_chrome_options(webdriver_path, config_dict) driver = webdriver.Chrome(service=Service(), options=chrome_options) - except Exception as exc3: + except WebDriverException as exc3: print(exc3) pass