Merge pull request #231 from yongjer/master

minor improvement
master
Max 2024-04-26 14:57:22 +08:00 committed by GitHub
commit 1ac2c8aa7e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 137 additions and 205 deletions

View File

@ -142,9 +142,8 @@ 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:
if len(args.input) > 0: config_filepath = args.input
config_filepath = args.input
config_dict = None config_dict = None
if os.path.isfile(config_filepath): if os.path.isfile(config_filepath):
@ -152,43 +151,34 @@ 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: if args.headless is not None:
config_dict["advanced"]["headless"] = util.t_or_f(args.headless) config_dict["advanced"]["headless"] = util.t_or_f(args.headless)
if not args.homepage is None: if args.homepage:
if len(args.homepage) > 0: config_dict["homepage"] = args.homepage
config_dict["homepage"] = args.homepage
if not args.ticket_number is None: if args.ticket_number:
if args.ticket_number > 0: config_dict["ticket_number"] = args.ticket_number
config_dict["ticket_number"] = args.ticket_number
if not args.browser is None: if args.browser:
if len(args.browser) > 0: config_dict["browser"] = args.browser
config_dict["browser"] = args.browser
if not args.tixcraft_sid is None: if args.tixcraft_sid:
if len(args.tixcraft_sid) > 0: config_dict["advanced"]["tixcraft_sid"] = args.tixcraft_sid
config_dict["advanced"]["tixcraft_sid"] = args.tixcraft_sid
if not args.ibonqware is None: if args.ibonqware:
if len(args.ibonqware) > 0: config_dict["advanced"]["ibonqware"] = args.ibonqware
config_dict["advanced"]["ibonqware"] = args.ibonqware
if not args.kktix_account is None: if args.kktix_account:
if len(args.kktix_account) > 0: config_dict["advanced"]["kktix_account"] = args.kktix_account
config_dict["advanced"]["kktix_account"] = args.kktix_account if args.kktix_password:
if not args.kktix_password is None: config_dict["advanced"]["kktix_password_plaintext"] = args.kktix_password
if len(args.kktix_password) > 0:
config_dict["advanced"]["kktix_password_plaintext"] = args.kktix_password
if not args.proxy_server is None: if args.proxy_server:
if len(args.proxy_server) > 2: config_dict["advanced"]["proxy_server_port"] = args.proxy_server
config_dict["advanced"]["proxy_server_port"] = args.proxy_server
if not args.window_size is None: if args.window_size:
if len(args.window_size) > 2: config_dict["advanced"]["window_size"] = args.window_size
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
@ -325,11 +315,7 @@ def get_chrome_options(webdriver_path, config_dict):
return chrome_options return chrome_options
def load_chromdriver_normal(config_dict, driver_type): def load_chromdriver_normal(config_dict, driver_type):
show_debug_message = True # debug. show_debug_message = config_dict["advanced"]["verbose"]
show_debug_message = False # online
if config_dict["advanced"]["verbose"]:
show_debug_message = True
driver = None driver = None
@ -337,8 +323,7 @@ def load_chromdriver_normal(config_dict, driver_type):
webdriver_path = os.path.join(Root_Dir, "webdriver") webdriver_path = os.path.join(Root_Dir, "webdriver")
chromedriver_path = get_chromedriver_path(webdriver_path) chromedriver_path = get_chromedriver_path(webdriver_path)
if not os.path.exists(webdriver_path): os.makedirs(webdriver_path, exist_ok=True)
os.mkdir(webdriver_path)
if not os.path.exists(chromedriver_path): if not os.path.exists(chromedriver_path):
print("WebDriver not exist, try to download to:", webdriver_path) print("WebDriver not exist, try to download to:", webdriver_path)
@ -353,15 +338,11 @@ def load_chromdriver_normal(config_dict, driver_type):
chrome_options = get_chrome_options(webdriver_path, config_dict) chrome_options = get_chrome_options(webdriver_path, config_dict)
try: try:
driver = webdriver.Chrome(service=chrome_service, options=chrome_options) driver = webdriver.Chrome(service=chrome_service, options=chrome_options)
except Exception as exc: except WebDriverException as exc:
error_message = str(exc) error_message = str(exc)
if show_debug_message: if show_debug_message:
print(exc) print(exc)
left_part = None left_part = error_message.split("Stacktrace:")[0] if "Stacktrace:" in error_message else None
if "Stacktrace:" in error_message:
left_part = error_message.split("Stacktrace:")[0]
print(left_part)
if "This version of ChromeDriver only supports Chrome version" in error_message: 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_EN)
print(CONST_CHROME_VERSION_NOT_MATCH_TW) print(CONST_CHROME_VERSION_NOT_MATCH_TW)
@ -379,12 +360,12 @@ def load_chromdriver_normal(config_dict, driver_type):
try: try:
chrome_options = get_chrome_options(webdriver_path, config_dict) chrome_options = get_chrome_options(webdriver_path, config_dict)
driver = webdriver.Chrome(service=chrome_service, options=chrome_options) 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.") print("Selenium 4.11.0 Release with Chrome For Testing Browser.")
try: try:
chrome_options = get_chrome_options(webdriver_path, config_dict) chrome_options = get_chrome_options(webdriver_path, config_dict)
driver = webdriver.Chrome(service=Service(), options=chrome_options) driver = webdriver.Chrome(service=Service(), options=chrome_options)
except Exception as exc3: except WebDriverException as exc3:
print(exc3) print(exc3)
pass pass

View File

@ -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/' URL_EDGE_DRIVER = 'https://developer.microsoft.com/zh-tw/microsoft-edge/tools/webdriver/'
def load_translate(): def load_translate():
translate = {} translate = {
en_us={} 'en_us': {
en_us["language"] = 'Language' "language": 'Language',
en_us["enable"] = 'Enable' "enable": 'Enable',
"config_list": 'Config List',
en_us["config_list"] = 'Config List' "advanced": 'Advanced',
en_us["advanced"] = 'Advanced' "about": 'About',
en_us["about"] = 'About' "run": 'Run',
"browse": 'Browse...',
en_us["run"] = 'Run' "save": 'Save',
en_us["browse"] = 'Browse...' "exit": 'Close',
en_us["save"] = 'Save' "copy": 'Copy',
en_us["exit"] = 'Close' "restore_defaults": 'Restore Defaults',
en_us["copy"] = 'Copy' "done": 'Done',
en_us["restore_defaults"] = 'Restore Defaults' "maxbot_slogan": 'MaxRegBot is a FREE and open source bot program. Wish you good luck.',
en_us["done"] = 'Done' "donate": 'Donate',
"help": 'Help',
en_us["maxbot_slogan"] = 'MaxRegBot is a FREE and open source bot program. Wish you good luck.' "release": 'Release'
en_us["donate"] = 'Donate' },
en_us["help"] = 'Help' 'zh_tw': {
en_us["release"] = 'Release' "language": '語言',
"enable": '啟用',
zh_tw={} "config_list": '設定檔管理',
zh_tw["language"] = '語言' "advanced": '進階設定',
zh_tw["enable"] = '啟用' "autofill": '自動填表單',
"about": '關於',
zh_tw["config_list"] = '設定檔管理' "run": '搶票',
zh_tw["advanced"] = '進階設定' "browse": '開啟...',
zh_tw["autofill"] = '自動填表單' "save": '存檔',
zh_tw["about"] = '關於' "exit": '關閉',
"copy": '複製',
zh_tw["run"] = '搶票' "restore_defaults": '恢復預設值',
zh_tw["browse"] = '開啟...' "done": '完成',
zh_tw["save"] = '存檔' "maxbot_slogan": 'MaxRegBot是一個免費、開放原始碼的搶票機器人。\n祝您掛號成功。',
zh_tw["exit"] = '關閉' "donate": '打賞',
zh_tw["copy"] = '複製' "release": '所有可用版本',
zh_tw["restore_defaults"] = '恢復預設值' "help": '使用教學'
zh_tw["done"] = '完成' },
'zh_cn': {
zh_tw["maxbot_slogan"] = 'MaxRegBot是一個免費、開放原始碼的搶票機器人。\n祝您掛號成功。' "language": '语言',
zh_tw["donate"] = '打賞' "enable": '启用',
zh_tw["release"] = '所有可用版本' "config_list": '设定档管理',
zh_tw["help"] = '使用教學' "advanced": '進階設定',
"autofill": '自动填表单',
zh_cn={} "about": '关于',
zh_cn["language"] = '语言' "run": '抢票',
zh_cn["enable"] = '启用' "browse": '开启...',
"save": '存档',
zh_cn["config_list"] = '设定档管理' "exit": '关闭',
zh_cn["advanced"] = '進階設定' "copy": '复制',
zh_cn["autofill"] = '自动填表单' "restore_defaults": '恢复默认值',
zh_cn["about"] = '关于' "done": '完成',
zh_cn["copy"] = '复制' "maxbot_slogan": 'MaxRegBot 是一个免费的开源机器人程序。\n祝您挂号成功。',
"donate": '打赏',
zh_cn["run"] = '抢票' "help": '使用教学',
zh_cn["browse"] = '开启...' "release": '所有可用版本'
zh_cn["save"] = '存档' },
zh_cn["exit"] = '关闭' 'ja_jp': {
zh_cn["copy"] = '复制' "language": '言語',
zh_cn["restore_defaults"] = '恢复默认值' "enable": '有効',
zh_cn["done"] = '完成' "config_list": 'Config List',
"advanced": '高度な設定',
zh_cn["maxbot_slogan"] = 'MaxRegBot 是一个免费的开源机器人程序。\n祝您挂号成功。' "autofill": 'オートフィル',
zh_cn["donate"] = '打赏' "about": '情報',
zh_cn["help"] = '使用教学' "run": 'チケットを取る',
zh_cn["release"] = '所有可用版本' "browse": '開ける...',
"save": '保存',
ja_jp={} "exit": '閉じる',
ja_jp["language"] = '言語' "copy": 'コピー',
ja_jp["enable"] = '有効' "restore_defaults": 'デフォルトに戻す',
"done": '終わり',
ja_jp["config_list"] = 'Config List' "maxbot_slogan": 'MaxRegBot は無料のオープン ソース ボット プログラムです。チケットの成功をお祈りします。',
ja_jp["advanced"] = '高度な設定' "donate": '寄付',
ja_jp["autofill"] = 'オートフィル' "help": '利用方法',
ja_jp["about"] = '情報' "release": 'リリース'
}
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
return translate return translate
def get_default_config(): def get_default_config():
@ -183,32 +167,18 @@ def btn_save_act(slience_mode=True):
config_dict = get_default_config() config_dict = get_default_config()
language_code = get_language_code_by_name(config_dict["advanced"]["language"]) language_code = get_language_code_by_name(config_dict["advanced"]["language"])
is_all_data_correct = True config_dict["advanced"]["language"] = combo_language.get().strip()
language_code = get_language_code_by_name(config_dict["advanced"]["language"])
global combo_language filelist = [txt_file_name[i].get().strip() for i in range(15)]
if is_all_data_correct: config_dict["list"] = filelist
config_dict["advanced"]["language"] = combo_language.get().strip()
# display as new language.
language_code = get_language_code_by_name(config_dict["advanced"]["language"])
util.save_json(config_dict, config_filepath)
if not slience_mode:
messagebox.showinfo(translate[language_code]["save"], translate[language_code]["done"])
global txt_file_name return True
filelist = []
for i in range(15):
filelist.append(txt_file_name[i].get().strip())
if is_all_data_correct:
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"])
return is_all_data_correct
def open_url(url): def open_url(url):
webbrowser.open_new(url) webbrowser.open_new(url)
@ -227,14 +197,12 @@ def callbackLanguageOnChange(event):
def get_language_code_by_name(new_language): def get_language_code_by_name(new_language):
language_code = "en_us" language_code = "en_us"
if u'繁體中文' in new_language: if '繁體中文' in new_language:
language_code = 'zh_tw' language_code = 'zh_tw'
if u'簡体中文' in new_language: if '簡体中文' in new_language:
language_code = 'zh_cn' language_code = 'zh_cn'
if u'日本語' in new_language: if '日本語' in new_language:
language_code = 'ja_jp' language_code = 'ja_jp'
#print("new language code:", language_code)
return language_code return language_code
def applyNewLanguage(): def applyNewLanguage():
@ -307,50 +275,33 @@ def btn_items_run_event(event):
threading.Thread(target=util.launch_maxbot, args=(script_name,filename,)).start() threading.Thread(target=util.launch_maxbot, args=(script_name,filename,)).start()
def ConfigListTab(root, config_dict, language_code, UI_PADDING_X): def ConfigListTab(root, config_dict, language_code, UI_PADDING_X):
# output to GUI.
row_count = 0
frame_group_header = Frame(root) 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 for i, filename in enumerate(config_dict["list"][:15]):
global txt_file_name widgets['lbl_file_name'][i] = Label(frame_group_header, text=str(i+1))
global txt_file_name_value widgets['lbl_file_name'][i].grid(column=0, row=i, sticky=E)
global btn_browse
global btn_run
lbl_file_name = {}
txt_file_name = {}
txt_file_name_value = {}
btn_browse = {}
btn_run = {}
print("config_dict[list]:",config_dict["list"]) widgets['txt_file_name_value'][i] = StringVar(frame_group_header, value=filename)
print("config_dict[list]:",len(config_dict["list"])) widgets['txt_file_name'][i] = Entry(frame_group_header, width=20, textvariable=widgets['txt_file_name_value'][i])
for i in range(15): widgets['txt_file_name'][i].grid(column=1, row=i, sticky=W)
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)
txt_file_name_value[i] = StringVar(frame_group_header, value=filename) widgets['btn_browse'][i] = ttk.Button(frame_group_header, text=translate[language_code]['browse'] + " " + str(i+1))
txt_file_name[i] = Entry(frame_group_header, width=20, textvariable = txt_file_name_value[i]) widgets['btn_browse'][i].grid(column=2, row=i, sticky=W)
txt_file_name[i].grid(column=1, row=group_row_count, sticky = W) widgets['btn_browse'][i].bind('<Button-1>', btn_items_browse_event)
btn_browse[i] = ttk.Button(frame_group_header, text=translate[language_code]['browse'] + " " + str(i+1)) widgets['btn_run'][i] = ttk.Button(frame_group_header, text=translate[language_code]['run'] + " " + str(i+1))
btn_browse[i].grid(column=2, row=group_row_count, sticky = W) widgets['btn_run'][i].grid(column=3, row=i, sticky=W)
btn_browse[i].bind('<Button-1>', btn_items_browse_event) widgets['btn_run'][i].bind('<Button-1>', btn_items_run_event)
btn_run[i] = ttk.Button(frame_group_header, text=translate[language_code]['run'] + " " + str(i+1)) frame_group_header.grid(column=0, row=0, sticky=W, padx=UI_PADDING_X)
btn_run[i].grid(column=3, row=group_row_count, sticky = W) return widgets
btn_run[i].bind('<Button-1>', 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)
def AboutTab(root, language_code): def AboutTab(root, language_code):