From 4423b28139ccb051a53a3e77976946ef8a3cc349 Mon Sep 17 00:00:00 2001 From: Max Yao Date: Sun, 27 Oct 2019 09:31:22 +0800 Subject: [PATCH] add area keyword #2 --- chrome_tixcraft.py | 223 ++++++++++++++++++++++++++++----------------- settings.json | 2 +- settings.py | 96 ++++++++++++------- 3 files changed, 204 insertions(+), 117 deletions(-) diff --git a/chrome_tixcraft.py b/chrome_tixcraft.py index 7348160..4dc5710 100644 --- a/chrome_tixcraft.py +++ b/chrome_tixcraft.py @@ -39,7 +39,7 @@ logger = logging.getLogger('logger') #附註1:沒有寫的很好,很多地方應該可以模組化。 #附註2: -CONST_APP_VERSION = u"MaxBot (2019.10.23)" +CONST_APP_VERSION = u"MaxBot (2019.10.26)" CONST_FROM_TOP_TO_BOTTOM = u"from top to bottom" CONST_FROM_BOTTOM_TO_TOP = u"from bottom to top" @@ -83,6 +83,9 @@ area_auto_select_enable = None area_auto_select_mode = None area_keyword = None +area_keyword_1 = None +area_keyword_2 = None + kktix_area_auto_select_mode = None kktix_area_keyword = None @@ -153,9 +156,13 @@ if not config_dict is None: if not area_auto_select_mode in CONST_SELECT_OPTIONS_ARRAY: area_auto_select_mode = CONST_SELECT_ORDER_DEFAULT - if 'area_keyword' in config_dict["tixcraft"]["area_auto_select"]: - area_keyword = config_dict["tixcraft"]["area_auto_select"]["area_keyword"] - area_keyword = area_keyword.strip() + if 'area_keyword_1' in config_dict["tixcraft"]["area_auto_select"]: + area_keyword_1 = config_dict["tixcraft"]["area_auto_select"]["area_keyword_1"] + area_keyword_1 = area_keyword_1.strip() + + if 'area_keyword_2' in config_dict["tixcraft"]["area_auto_select"]: + area_keyword_2 = config_dict["tixcraft"]["area_auto_select"]["area_keyword_2"] + area_keyword_2 = area_keyword_2.strip() # output config: print("version", CONST_APP_VERSION) @@ -179,7 +186,8 @@ if not config_dict is None: print("area_auto_select_enable", area_auto_select_enable) print("area_auto_select_mode", area_auto_select_mode) - print("area_keyword", area_keyword) + print("area_keyword_1", area_keyword_1) + print("area_keyword_2", area_keyword_2) # entry point # 說明:自動開啟第一個的網頁 @@ -691,6 +699,72 @@ def date_auto_select(url): pass #print("find .btn-next fail:", exc) +# PURPOSE: get target area list. +def get_tixcraft_target_area(el, area_keyword): + areas = None + is_need_refresh = False + + if el is not None: + if len(area_keyword) == 0: + try: + areas = el.find_elements(By.TAG_NAME, "a") + except Exception as exc: + pass + + if areas is not None: + if len(areas) == 0: + print("list is empty, do refresh!") + is_need_refresh = True + else: + print("list is None, do refresh!") + is_need_refresh = True + else: + # match keyword. + area_list = None + try: + area_list = el.find_elements(By.TAG_NAME, 'a') + except Exception as exc: + #print("find area list a tag fail") + pass + + if area_list is not None: + if len(area_list) == 0: + print("(with keyword) list is empty, do refresh!") + is_need_refresh = True + else: + print("(with keyword) list is None, do refresh!") + is_need_refresh = True + + if area_list is not None: + areas = [] + for row in area_list: + row_is_enabled=False + try: + row_is_enabled = row.is_enabled() + except Exception as exc: + pass + + row_text = "" + if row_is_enabled: + try: + row_text = row.text + except Exception as exc: + print("get text fail") + break + + if len(row_text) > 0: + if area_keyword in row_text: + areas.append(row) + + if area_auto_select_mode == CONST_FROM_TOP_TO_BOTTOM: + print("only need first item, break area list loop.") + break + #print("row_text:" + row_text) + #print("match:" + area_keyword) + if len(areas) == 0: + areas = None + + return is_need_refresh, areas # PS: auto refresh condition 1: no keyword + no hyperlink. # PS: auto refresh condition 2: with keyword + no hyperlink. @@ -705,66 +779,11 @@ def area_auto_select(url): print("find .zone fail, do nothing.") if el is not None: - areas = None - - is_need_refresh = False - - if len(area_keyword) == 0: - try: - areas = el.find_elements(By.TAG_NAME, "a") - except Exception as exc: - pass - - if areas is not None: - if len(areas) == 0: - print("list is empty, do refresh!") - is_need_refresh = True - else: - print("list is None, do refresh!") - is_need_refresh = True - else: - # match keyword. - area_list = None - try: - area_list = el.find_elements(By.TAG_NAME, 'a') - except Exception as exc: - #print("find area list a tag fail") - pass - - if area_list is not None: - if len(area_list) == 0: - print("(with keyword) list is empty, do refresh!") - is_need_refresh = True - else: - print("(with keyword) list is None, do refresh!") - is_need_refresh = True - - if area_list is not None: - areas = [] - for row in area_list: - row_is_enabled=False - try: - row_is_enabled = row.is_enabled() - except Exception as exc: - pass - - row_text = "" - if row_is_enabled: - try: - row_text = row.text - except Exception as exc: - print("get text fail") - break - - if len(row_text) > 0: - if area_keyword in row_text: - areas.append(row) - - if area_auto_select_mode == CONST_FROM_TOP_TO_BOTTOM: - print("only need first item, break area list loop.") - break - #print("row_text:" + row_text) - #print("match:" + area_keyword) + is_need_refresh, areas = get_tixcraft_target_area(el, area_keyword_1) + if not is_need_refresh: + if areas is None: + print("use area keyword #2", area_keyword_2) + is_need_refresh, areas = get_tixcraft_target_area(el, area_keyword_2) area_target = None if areas is not None: @@ -799,7 +818,6 @@ def area_auto_select(url): print(exc) pass - # auto refresh for area list page. if is_need_refresh: try: @@ -996,22 +1014,45 @@ def tixcraft_verify(url): print("find verify fail") pass - if ret: - # retry - for i in range(5): + if ret: + # retry + for i in range(3): + form_input = None + try: + form_input = driver.find_element(By.CSS_SELECTOR, '#submitButton') + if form_input is not None: + if form_input.is_enabled(): + form_input.click() + break + else: + print("find submit button none") + + except Exception as exc: + print("find submit button fail") + print(exc) + pass + else: + is_auto_focus_enable = False + if is_auto_focus_enable: form_input = None try: - form_input = driver.find_element(By.CSS_SELECTOR, '#submitButton') + form_input = driver.find_element(By.CSS_SELECTOR, '#checkCode') if form_input is not None: - if form_input.is_enabled(): - form_input.click() - break + default_value = form_input.get_attribute('value') + is_need_focus = False + if default_value is None: + is_need_focus = True + else: + if len(default_value) == 0: + is_need_focus = True + if is_need_focus: + if form_input.is_enabled(): + form_input.click() + time.sleep(0.2) else: - print("find submit button none") - + print("find captcha input field fail") except Exception as exc: - print("find submit button fail") - print(exc) + print("find verify fail") pass return ret @@ -1810,13 +1851,9 @@ def fami_date_auto_select(url): if match_keyword_row: break -# purpose: area auto select -# return: -# True: area block appear. -# False: area block not appear. -# ps: return value for date auto select. -def fami_area_auto_select(url): - ret = False + +# PURPOSE: get target area list. +def get_fami_target_area(area_keyword): areas = None area_list = None @@ -1856,11 +1893,29 @@ def fami_area_auto_select(url): #print("area row_text:", row_index, row_text) if area_keyword in row_text: areas.append(row) + if len(areas)==0: + areas = None except Exception as exc: print("find #game_page date list fail") #print(exc) + return areas + +# purpose: area auto select +# return: +# True: area block appear. +# False: area block not appear. +# ps: return value for date auto select. +def fami_area_auto_select(url): + ret = False + + areas = get_fami_target_area(area_keyword_1) + if areas is None: + print("use area keyword #2", area_keyword_2) + areas = get_fami_target_area(area_keyword_2) + + area = None if areas is not None: #print("area_auto_select_mode", area_auto_select_mode) diff --git a/settings.json b/settings.json index e9bd6be..b9074de 100644 --- a/settings.json +++ b/settings.json @@ -1 +1 @@ -{"tixcraft": {"area_auto_select": {"enable": true, "mode": "random", "area_keyword": ""}, "date_auto_select": {"enable": true, "date_keyword": "", "mode": "from top to bottom"}}, "homepage1": "https://kktix.com", "homepage2": "https://tixcraft.com", "facebook_account": "", "browser1": "chrome", "browser2": "firefox", "kktix": {"auto_fill_ticket_price": "$1,500", "answer_dictionary": "", "area_keyword": "", "auto_press_next_step_button": true, "auto_fill_ticket_number": true, "area_mode": "random"}, "ticket_number": 2, "homepage": "https://tixcraft.com", "browser": "chrome"} \ No newline at end of file +{"tixcraft": {"area_auto_select": {"enable": true, "area_keyword": "", "mode": "from top to bottom", "area_keyword_2": "", "area_keyword_1": ""}, "date_auto_select": {"enable": true, "date_keyword": "", "mode": "from top to bottom"}}, "homepage1": "https://kktix.com", "homepage2": "https://tixcraft.com", "facebook_account": "", "browser1": "chrome", "browser2": "firefox", "kktix": {"auto_fill_ticket_price": "$1,500", "answer_dictionary": "", "area_keyword": "", "auto_press_next_step_button": true, "auto_fill_ticket_number": true, "area_mode": "random"}, "ticket_number": 2, "homepage": "https://tixcraft.com", "browser": "chrome"} \ No newline at end of file diff --git a/settings.py b/settings.py index 13116b9..50d493f 100644 --- a/settings.py +++ b/settings.py @@ -16,7 +16,7 @@ import os import sys import json -CONST_APP_VERSION = u"MaxBot (2019.10.23)" +CONST_APP_VERSION = u"MaxBot (2019.10.26)" CONST_FROM_TOP_TO_BOTTOM = u"from top to bottom" CONST_FROM_BOTTOM_TO_TOP = u"from bottom to top" @@ -77,7 +77,8 @@ def btn_save_act(slience_mode=False): global chk_state_date_auto_select global txt_date_keyword global chk_state_area_auto_select - global txt_area_keyword + global txt_area_keyword_1 + global txt_area_keyword_2 global combo_date_auto_select_mode global combo_area_auto_select_mode @@ -118,7 +119,8 @@ def btn_save_act(slience_mode=False): config_dict["tixcraft"]["date_auto_select"]["date_keyword"] = txt_date_keyword.get().strip() config_dict["tixcraft"]["area_auto_select"]["enable"] = bool(chk_state_area_auto_select.get()) - config_dict["tixcraft"]["area_auto_select"]["area_keyword"] = txt_area_keyword.get().strip() + config_dict["tixcraft"]["area_auto_select"]["area_keyword_1"] = txt_area_keyword_1.get().strip() + config_dict["tixcraft"]["area_auto_select"]["area_keyword_2"] = txt_area_keyword_2.get().strip() config_dict["tixcraft"]["date_auto_select"]["mode"] = combo_date_auto_select_mode.get().strip() config_dict["tixcraft"]["area_auto_select"]["mode"] = combo_area_auto_select_mode.get().strip() @@ -250,16 +252,19 @@ def showHideTixcraftBlocks(): global lbl_area_auto_select_mode global combo_area_auto_select_mode - global area_keyword_index - global lbl_area_keyword - global txt_area_keyword + global area_keyword_1_index + global area_keyword_2_index + global lbl_area_keyword_1 + global lbl_area_keyword_2 + global txt_area_keyword_1 + global txt_area_keyword_2 - new_date_enable = bool(chk_state_date_auto_select.get()) - new_area_enable = bool(chk_state_area_auto_select.get()) - #print("new new_date_enable value:", new_date_enable) - #print("new new_area_enable value:", new_area_enable) + is_date_set_to_enable = bool(chk_state_date_auto_select.get()) + is_area_set_to_enable = bool(chk_state_area_auto_select.get()) + #print("now is_date_set_to_enable value:", is_date_set_to_enable) + #print("now is_area_set_to_enable value:", is_area_set_to_enable) - if new_date_enable: + if is_date_set_to_enable: # show lbl_date_auto_select_mode.grid(column=0, row=date_auto_select_mode_index, sticky = E) combo_date_auto_select_mode.grid(column=1, row=date_auto_select_mode_index, sticky = W) @@ -274,20 +279,26 @@ def showHideTixcraftBlocks(): lbl_date_keyword.grid_forget() txt_date_keyword.grid_forget() - if new_area_enable: + if is_area_set_to_enable: # show lbl_area_auto_select_mode.grid(column=0, row=area_auto_select_index, sticky = E) combo_area_auto_select_mode.grid(column=1, row=area_auto_select_index, sticky = W) - lbl_area_keyword.grid(column=0, row=area_keyword_index, sticky = E) - txt_area_keyword.grid(column=1, row=area_keyword_index, sticky = W) + lbl_area_keyword_1.grid(column=0, row=area_keyword_1_index, sticky = E) + txt_area_keyword_1.grid(column=1, row=area_keyword_1_index, sticky = W) + + lbl_area_keyword_2.grid(column=0, row=area_keyword_2_index, sticky = E) + txt_area_keyword_2.grid(column=1, row=area_keyword_2_index, sticky = W) else: # hide lbl_area_auto_select_mode.grid_forget() combo_area_auto_select_mode.grid_forget() - lbl_area_keyword.grid_forget() - txt_area_keyword.grid_forget() + lbl_area_keyword_1.grid_forget() + txt_area_keyword_1.grid_forget() + + lbl_area_keyword_2.grid_forget() + txt_area_keyword_2.grid_forget() def MainMenu(root): @@ -319,7 +330,8 @@ def MainMenu(root): area_auto_select_enable = None area_auto_select_mode = "" - area_keyword = "" + area_keyword_1 = "" + area_keyword_2 = "" global config_dict if not config_dict is None: @@ -381,9 +393,13 @@ def MainMenu(root): if not area_auto_select_mode in CONST_SELECT_OPTIONS_ARRAY: area_auto_select_mode = CONST_SELECT_ORDER_DEFAULT - if 'area_keyword' in config_dict["tixcraft"]["area_auto_select"]: - area_keyword = config_dict["tixcraft"]["area_auto_select"]["area_keyword"] - area_keyword = area_keyword.strip() + if 'area_keyword_1' in config_dict["tixcraft"]["area_auto_select"]: + area_keyword_1 = config_dict["tixcraft"]["area_auto_select"]["area_keyword_1"] + area_keyword_1 = area_keyword_1.strip() + + if 'area_keyword_2' in config_dict["tixcraft"]["area_auto_select"]: + area_keyword_2 = config_dict["tixcraft"]["area_auto_select"]["area_keyword_2"] + area_keyword_2 = area_keyword_2.strip() # output config: print("homepage", homepage) @@ -407,7 +423,8 @@ def MainMenu(root): print("area_auto_select_enable", area_auto_select_enable) print("area_auto_select_mode", area_auto_select_mode) - print("area_keyword", area_keyword) + print("area_keyword_1", area_keyword_1) + print("area_keyword_2", area_keyword_2) else: print('config is none') @@ -638,18 +655,33 @@ def MainMenu(root): group_row_count+=1 - global area_keyword_index - area_keyword_index = group_row_count + global area_keyword_1_index + area_keyword_1_index = group_row_count - global lbl_area_keyword - lbl_area_keyword = Label(frame_group_tixcraft, text="Area Keyword") - lbl_area_keyword.grid(column=0, row=area_keyword_index, sticky = E) + global lbl_area_keyword_1 + lbl_area_keyword_1 = Label(frame_group_tixcraft, text="Area Keyword #1") + lbl_area_keyword_1.grid(column=0, row=area_keyword_1_index, sticky = E) - global txt_area_keyword - global txt_ares_keyword_value - txt_ares_keyword_value = StringVar(frame_group_tixcraft, value=area_keyword) - txt_area_keyword = Entry(frame_group_tixcraft, width=20, textvariable = txt_ares_keyword_value) - txt_area_keyword.grid(column=1, row=area_keyword_index, sticky = W) + global txt_area_keyword_1 + global txt_area_keyword_1_value + txt_area_keyword_1_value = StringVar(frame_group_tixcraft, value=area_keyword_1) + txt_area_keyword_1 = Entry(frame_group_tixcraft, width=20, textvariable = txt_area_keyword_1_value) + txt_area_keyword_1.grid(column=1, row=area_keyword_1_index, sticky = W) + + group_row_count+=1 + + global area_keyword_2_index + area_keyword_2_index = group_row_count + + global lbl_area_keyword_2 + lbl_area_keyword_2 = Label(frame_group_tixcraft, text="Area Keyword #2") + lbl_area_keyword_2.grid(column=0, row=area_keyword_2_index, sticky = E) + + global txt_area_keyword_2 + global txt_area_keyword_2_value + txt_area_keyword_2_value = StringVar(frame_group_tixcraft, value=area_keyword_2) + txt_area_keyword_2 = Entry(frame_group_tixcraft, width=20, textvariable = txt_area_keyword_2_value) + txt_area_keyword_2.grid(column=1, row=area_keyword_2_index, sticky = W) global frame_group_tixcraft_index frame_group_tixcraft_index = row_count @@ -697,7 +729,7 @@ def main(): GUI = MainMenu(root) GUI_SIZE_WIDTH = 420 - GUI_SIZE_HEIGHT = 340 + GUI_SIZE_HEIGHT = 370 GUI_SIZE_MACOS = str(GUI_SIZE_WIDTH) + 'x' + str(GUI_SIZE_HEIGHT) GUI_SIZE_WINDOWS=str(GUI_SIZE_WIDTH-60) + 'x' + str(GUI_SIZE_HEIGHT-20)