2022-10-19, fix kktix captcha text guess.

master
CHUN YU YAO 2022-10-19 02:15:05 +08:00
parent 43d0d73cd5
commit 3e520fa4bf
2 changed files with 209 additions and 159 deletions

View File

@ -66,7 +66,7 @@ ssl._create_default_https_context = ssl._create_unverified_context
#附註1沒有寫的很好很多地方應該可以模組化。 #附註1沒有寫的很好很多地方應該可以模組化。
#附註2 #附註2
CONST_APP_VERSION = u"MaxBot (2022.09.19)" CONST_APP_VERSION = u"MaxBot (2022.10.19)"
CONST_FROM_TOP_TO_BOTTOM = u"from top to bottom" CONST_FROM_TOP_TO_BOTTOM = u"from top to bottom"
CONST_FROM_BOTTOM_TO_TOP = u"from bottom to top" CONST_FROM_BOTTOM_TO_TOP = u"from bottom to top"
@ -368,12 +368,12 @@ def load_config_from_local(driver):
chromedriver_path =Root_Dir+ "webdriver/chromedriver.exe" chromedriver_path =Root_Dir+ "webdriver/chromedriver.exe"
# method 5: uc # method 5: uc
if driver_type=="undetected_chromedriver": if driver_type == "undetected_chromedriver":
import undetected_chromedriver as uc import undetected_chromedriver as uc
#import seleniumwire.undetected_chromedriver as uc #import seleniumwire.undetected_chromedriver as uc
# method 6: Selenium Stealth # method 6: Selenium Stealth
if driver_type=="stealth": if driver_type != "undetected_chromedriver":
from selenium_stealth import stealth from selenium_stealth import stealth
chrome_options = webdriver.ChromeOptions() chrome_options = webdriver.ChromeOptions()
@ -403,15 +403,18 @@ def load_config_from_local(driver):
# method 6: Selenium Stealth # method 6: Selenium Stealth
driver = webdriver.Chrome(service=chrome_service, options=chrome_options) driver = webdriver.Chrome(service=chrome_service, options=chrome_options)
# Selenium Stealth settings if driver_type=="stealth":
stealth(driver, from selenium_stealth import stealth
languages=["zh-TW", "zh"],
vendor="Google Inc.", # Selenium Stealth settings
platform="Win32", stealth(driver,
webgl_vendor="Intel Inc.", languages=["zh-TW", "zh"],
renderer="Intel Iris OpenGL Engine", vendor="Google Inc.",
fix_hairline=True, platform="Win32",
) webgl_vendor="Intel Inc.",
renderer="Intel Iris OpenGL Engine",
fix_hairline=True,
)
#print("caps:", caps) #print("caps:", caps)
@ -488,14 +491,17 @@ def load_config_from_local(driver):
except Exception as excSwithFail: except Exception as excSwithFail:
pass pass
try: if driver is None:
print("goto url:", homepage) print("create web driver object fail @_@;")
driver.get(homepage) else:
except WebDriverException: try:
print('oh no not again !') print("goto url:", homepage)
except Exception as exec1: driver.get(homepage)
print('get() raise Exception:', exec1) except WebDriverException:
pass print('oh no not again !')
except Exception as exec1:
print('get() raise Exception:', exec1)
pass
else: else:
print("Config error!") print("Config error!")
@ -1524,7 +1530,6 @@ def kktix_events_press_next_button(driver):
if next_step_button.is_enabled(): if next_step_button.is_enabled():
next_step_button.click() next_step_button.click()
ret = True ret = True
except Exception as exc: except Exception as exc:
print("wait form-actions div wait to be clickable Exception:") print("wait form-actions div wait to be clickable Exception:")
#print(exc) #print(exc)
@ -1599,32 +1604,36 @@ def kktix_captcha_text_value(captcha_inner_div):
return ret return ret
def kktix_input_captcha_text(captcha_inner_div, captcha_password_string, force_overwrite = False): def kktix_input_captcha_text(captcha_inner_div, captcha_password_string, force_overwrite = False):
show_debug_message = True # debug.
#show_debug_message = False # online
ret = False ret = False
try: if captcha_inner_div is not None and captcha_password_string is not None:
if captcha_inner_div is not None: try:
#print("found captcha div") if show_debug_message:
if captcha_password_string is not None: print("found captcha div")
captcha_password_text = captcha_inner_div.find_element(By.TAG_NAME, "input") captcha_password_text = captcha_inner_div.find_element(By.TAG_NAME, "input")
if not captcha_password_text is None: if not captcha_password_text is None:
#print("found input field") if show_debug_message:
inputed_captcha_text = captcha_password_text.get_attribute('value') print("found input field")
if force_overwrite:
inputed_captcha_text = captcha_password_text.get_attribute('value')
if force_overwrite:
captcha_password_text.send_keys(captcha_password_string)
print("send captcha keys:" + captcha_password_string)
ret = True
else:
# not force overwrite:
if len(inputed_captcha_text) == 0:
captcha_password_text.send_keys(captcha_password_string) captcha_password_text.send_keys(captcha_password_string)
print("send captcha keys:" + captcha_password_string) print("send captcha keys:" + captcha_password_string)
ret = True ret = True
else: except Exception as exc:
# not force overwrite: if show_debug_message:
if len(inputed_captcha_text) == 0: print("find kktix_input_captcha_text Exception:")
captcha_password_text.send_keys(captcha_password_string) print(exc)
print("send captcha keys:" + captcha_password_string) pass
ret = True
else:
print("find captcha input field fail")
except Exception as exc:
print("find kktix_input_captcha_text Exception:")
#print(exc)
return ret return ret
@ -1763,42 +1772,53 @@ def kktix_assign_ticket_number(driver, ticket_number, kktix_area_keyword):
return ret return ret
def kktix_get_web_datetime(url, registrationsNewApp_div): def kktix_get_web_datetime(url, registrationsNewApp_div):
show_debug_message = True # debug.
#show_debug_message = False # online
web_datetime = None web_datetime = None
el_web_datetime = None
is_found_web_datetime = False is_found_web_datetime = False
try:
if not registrationsNewApp_div is None: el_web_datetime_list = None
if not registrationsNewApp_div is None:
try:
el_web_datetime_list = registrationsNewApp_div.find_elements(By.TAG_NAME, 'td') el_web_datetime_list = registrationsNewApp_div.find_elements(By.TAG_NAME, 'td')
if el_web_datetime_list is not None: except Exception as exc:
el_web_datetime_list_count = len(el_web_datetime_list) if show_debug_message:
if el_web_datetime_list_count > 0: print("find td.ng-binding Exception")
for el_web_datetime in el_web_datetime_list: print(exc)
try: pass
el_web_datetime_text = el_web_datetime.text #print("is_found_web_datetime", is_found_web_datetime)
#print("el_web_datetime_text:", el_web_datetime_text)
now = datetime.now() if el_web_datetime_list is not None:
#print("now:", now) el_web_datetime_list_count = len(el_web_datetime_list)
for guess_year in range(now.year,now.year+3): if el_web_datetime_list_count > 0:
current_year = str(guess_year) el_web_datetime = None
if current_year in el_web_datetime_text: for el_web_datetime in el_web_datetime_list:
if u'/' in el_web_datetime_text: try:
web_datetime = el_web_datetime_text el_web_datetime_text = el_web_datetime.text
is_found_web_datetime = True if show_debug_message:
break print("el_web_datetime_text:", el_web_datetime_text)
if is_found_web_datetime: now = datetime.now()
#print("now:", now)
for guess_year in range(now.year,now.year+3):
current_year = str(guess_year)
if current_year in el_web_datetime_text:
if u'/' in el_web_datetime_text:
web_datetime = el_web_datetime_text
is_found_web_datetime = True
break break
except Exception as exc:
#print(exc) if is_found_web_datetime:
pass break
else: except Exception as exc:
print("find td.ng-binding fail") if show_debug_message:
except Exception as exc: print('parse web datetime fail:')
#print("find td.ng-binding Exception") print(exc)
pass pass
#print("is_found_web_datetime", is_found_web_datetime) else:
print("find td.ng-binding fail")
return web_datetime return web_datetime
@ -1884,6 +1904,9 @@ def kktix_check_register_status(url):
return registerStatus return registerStatus
def kktix_reg_new_main(url, answer_index, registrationsNewApp_div, is_finish_checkbox_click, auto_fill_ticket_number, ticket_number, kktix_area_keyword): def kktix_reg_new_main(url, answer_index, registrationsNewApp_div, is_finish_checkbox_click, auto_fill_ticket_number, ticket_number, kktix_area_keyword):
show_debug_message = True # debug.
#show_debug_message = False # online
#--------------------------- #---------------------------
# part 2: ticket number # part 2: ticket number
#--------------------------- #---------------------------
@ -1916,6 +1939,9 @@ def kktix_reg_new_main(url, answer_index, registrationsNewApp_div, is_finish_che
pass pass
if captcha_inner_div is not None: if captcha_inner_div is not None:
if show_debug_message:
print("found captcha_inner_div layor.")
captcha_text_div = None captcha_text_div = None
try: try:
captcha_text_div = captcha_inner_div.find_element(By.TAG_NAME, "p") captcha_text_div = captcha_inner_div.find_element(By.TAG_NAME, "p")
@ -1934,6 +1960,9 @@ def kktix_reg_new_main(url, answer_index, registrationsNewApp_div, is_finish_che
except Exception as exc: except Exception as exc:
pass pass
if show_debug_message:
print("captcha_text_div_text:", captcha_text_div_text)
#captcha_text_div_text = u"請回答下列問題,請在下方空格輸入DELIGHT請以半形輸入法作答大小寫需要一模一樣" #captcha_text_div_text = u"請回答下列問題,請在下方空格輸入DELIGHT請以半形輸入法作答大小寫需要一模一樣"
#captcha_text_div_text = u"請在下方空白處輸入引號內文字「abc」" #captcha_text_div_text = u"請在下方空白處輸入引號內文字「abc」"
#captcha_text_div_text = u"請在下方空白處輸入引號內文字「0118eveconcert」請以半形小寫作答。" #captcha_text_div_text = u"請在下方空白處輸入引號內文字「0118eveconcert」請以半形小寫作答。"
@ -2019,7 +2048,8 @@ def kktix_reg_new_main(url, answer_index, registrationsNewApp_div, is_finish_che
# parse '演出日期' # parse '演出日期'
is_need_parse_web_datetime = False is_need_parse_web_datetime = False
if u'半形數字' in captcha_text_div_text: # '半形阿拉伯數字' & '半形數字'
if u'半形' in captcha_text_div_text and u'' in captcha_text_div_text:
if u'演出日期' in captcha_text_div_text: if u'演出日期' in captcha_text_div_text:
is_need_parse_web_datetime = True is_need_parse_web_datetime = True
if u'活動日期' in captcha_text_div_text: if u'活動日期' in captcha_text_div_text:
@ -2034,41 +2064,48 @@ def kktix_reg_new_main(url, answer_index, registrationsNewApp_div, is_finish_che
is_need_parse_web_datetime = True is_need_parse_web_datetime = True
if u'音樂會日期' in captcha_text_div_text: if u'音樂會日期' in captcha_text_div_text:
is_need_parse_web_datetime = True is_need_parse_web_datetime = True
if u'the date of the show you purchased' in captcha_text_div_text: if u'the date of the show you purchased' in captcha_text_div_text:
is_need_parse_web_datetime = True is_need_parse_web_datetime = True
if show_debug_message:
print("is_need_parse_web_datetime:", is_need_parse_web_datetime)
#print("is_need_parse_web_datetime:", is_need_parse_web_datetime)
if is_need_parse_web_datetime: if is_need_parse_web_datetime:
captcha_password_string = None captcha_password_string = None
web_datetime = kktix_get_web_datetime(url, registrationsNewApp_div) web_datetime = kktix_get_web_datetime(url, registrationsNewApp_div)
if not web_datetime is None: if not web_datetime is None:
tmp_text = captcha_text_div_text if show_debug_message:
# replace ex. print("web_datetime:", web_datetime)
tmp_text = tmp_text.replace(u'例如',u'範例')
tmp_text = tmp_text.replace(u'如:',u'範例:')
tmp_text = tmp_text.replace(u'舉例',u'範例')
if not u'範例' in tmp_text:
tmp_text = tmp_text.replace(u'',u'範例')
# important, maybe 例 & ex occurs at same time.
tmp_text = tmp_text.replace(u'ex:',u'範例:')
tmp_text = tmp_text.replace(u'輸入:',u'範例') captcha_text_formatted = captcha_text_div_text
tmp_text = tmp_text.replace(u'輸入',u'範例') # replace ex.
#print("tmp_text", tmp_text) captcha_text_formatted = captcha_text_formatted.replace(u'例如',u'範例')
captcha_text_formatted = captcha_text_formatted.replace(u'如:',u'範例:')
captcha_text_formatted = captcha_text_formatted.replace(u'舉例',u'範例')
if not u'範例' in captcha_text_formatted:
captcha_text_formatted = captcha_text_formatted.replace(u'',u'範例')
# important, maybe 例 & ex occurs at same time.
captcha_text_formatted = captcha_text_formatted.replace(u'ex:',u'範例:')
captcha_text_formatted = captcha_text_formatted.replace(u'輸入:',u'範例')
captcha_text_formatted = captcha_text_formatted.replace(u'輸入',u'範例')
if show_debug_message:
print("captcha_text_formatted", captcha_text_formatted)
my_datetime_foramted = None my_datetime_foramted = None
if my_datetime_foramted is None: if my_datetime_foramted is None:
if u'4位半形數字' in tmp_text: if u'4位半形' in captcha_text_formatted:
my_datetime_foramted = "%m%d" my_datetime_foramted = "%m%d"
if my_datetime_foramted is None: if my_datetime_foramted is None:
now = datetime.now() now = datetime.now()
for guess_year in range(now.year-1,now.year+3): for guess_year in range(now.year-4,now.year+2):
current_year = str(guess_year) current_year = str(guess_year)
if current_year in tmp_text: if current_year in captcha_text_formatted:
my_hint_index = tmp_text.find(current_year) my_hint_index = captcha_text_formatted.find(current_year)
my_hint_anwser = tmp_text[my_hint_index:] my_hint_anwser = captcha_text_formatted[my_hint_index:]
#print("my_hint_anwser:", my_hint_anwser) #print("my_hint_anwser:", my_hint_anwser)
# get after. # get after.
my_delimitor_symbol = u'範例' my_delimitor_symbol = u'範例'
@ -2090,13 +2127,22 @@ def kktix_reg_new_main(url, answer_index, registrationsNewApp_div, is_finish_che
if my_delimitor_symbol in my_hint_anwser: if my_delimitor_symbol in my_hint_anwser:
my_delimitor_index = my_hint_anwser.find(my_delimitor_symbol) my_delimitor_index = my_hint_anwser.find(my_delimitor_symbol)
my_hint_anwser = my_hint_anwser[:my_delimitor_index] my_hint_anwser = my_hint_anwser[:my_delimitor_index]
#remove last char.
remove_last_char_list = [')','(','.','','','','[',']']
for check_char in remove_last_char_list:
if my_hint_anwser[-1:]==check_char:
my_hint_anwser = my_hint_anwser[:-1]
my_anwser_formated = convert_string_to_pattern(my_hint_anwser, dynamic_length=False) my_anwser_formated = convert_string_to_pattern(my_hint_anwser, dynamic_length=False)
#print("my_hint_anwser:", my_hint_anwser)
#print(u"my_anwser_formated:", my_anwser_formated)
if my_anwser_formated == u"[\\d][\\d][\\d][\\d][\\d][\\d][\\d][\\d]": if my_anwser_formated == u"[\\d][\\d][\\d][\\d][\\d][\\d][\\d][\\d]":
my_datetime_foramted = "%Y%m%d" my_datetime_foramted = "%Y%m%d"
if my_anwser_formated == u"[\\d][\\d][\\d][\\d]/[\\d][\\d]/[\\d][\\d]": if my_anwser_formated == u"[\\d][\\d][\\d][\\d]/[\\d][\\d]/[\\d][\\d]":
my_datetime_foramted = "%Y/%m/%d" my_datetime_foramted = "%Y/%m/%d"
if show_debug_message:
print("my_hint_anwser:", my_hint_anwser)
print("my_anwser_formated:", my_anwser_formated)
print("my_datetime_foramted:", my_datetime_foramted)
break break
if not my_datetime_foramted is None: if not my_datetime_foramted is None:
@ -2104,14 +2150,16 @@ def kktix_reg_new_main(url, answer_index, registrationsNewApp_div, is_finish_che
if my_delimitor_symbol in web_datetime: if my_delimitor_symbol in web_datetime:
web_datetime = web_datetime[:web_datetime.find(my_delimitor_symbol)] web_datetime = web_datetime[:web_datetime.find(my_delimitor_symbol)]
date_time = datetime.strptime(web_datetime,u"%Y/%m/%d") date_time = datetime.strptime(web_datetime,u"%Y/%m/%d")
#print("date_time:", date_time) if show_debug_message:
print("date_time:", date_time)
ans = None ans = None
try: try:
ans = date_time.strftime(my_datetime_foramted) ans = date_time.strftime(my_datetime_foramted)
except Exception as exc: except Exception as exc:
pass pass
captcha_password_string = ans captcha_password_string = ans
#print(u"my_anwser:", ans) if show_debug_message:
print("my_anwser:", ans)
# parse '演出時間' # parse '演出時間'
is_need_parse_web_time = False is_need_parse_web_time = False
@ -2179,12 +2227,12 @@ def kktix_reg_new_main(url, answer_index, registrationsNewApp_div, is_finish_che
#print(u"my_anwser_formated:", my_anwser_formated) #print(u"my_anwser_formated:", my_anwser_formated)
if my_anwser_formated == u"[\\d][\\d][\\d][\\d]": if my_anwser_formated == u"[\\d][\\d][\\d][\\d]":
my_datetime_foramted = "%H%M" my_datetime_foramted = "%H%M"
if u'12小時' in tmp_text: if u'12小時' in tmp_text:
my_datetime_foramted = "%I%M" my_datetime_foramted = "%I%M"
if my_anwser_formated == u"[\\d][\\d]:[\\d][\\d]": if my_anwser_formated == u"[\\d][\\d]:[\\d][\\d]":
my_datetime_foramted = "%H:%M" my_datetime_foramted = "%H:%M"
if u'12小時' in tmp_text: if u'12小時' in tmp_text:
my_datetime_foramted = "%I:%M" my_datetime_foramted = "%I:%M"
if not my_datetime_foramted is None: if not my_datetime_foramted is None:
@ -2239,7 +2287,9 @@ def kktix_reg_new_main(url, answer_index, registrationsNewApp_div, is_finish_che
captcha_password_string = None captcha_password_string = None
#print("is_combine_two_question:", is_combine_two_question) #print("is_combine_two_question:", is_combine_two_question)
#print("captcha_password_string:", captcha_password_string) if show_debug_message:
print("captcha_password_string:", captcha_password_string)
# ask question. # ask question.
if auto_guess_options: if auto_guess_options:
if not is_combine_two_question: if not is_combine_two_question:
@ -2448,7 +2498,7 @@ def kktix_reg_new(driver, url, answer_index, kktix_register_status_last):
# PS: it seems use date_auto_select_mode instead of area_auto_select_mode # PS: it seems use date_auto_select_mode instead of area_auto_select_mode
def get_fami_target_area(date_keyword, area_keyword_1, area_keyword_2, area_auto_select_mode): def get_fami_target_area(date_keyword, area_keyword_1, area_keyword_2, area_auto_select_mode):
show_debug_message = True # debug. show_debug_message = True # debug.
show_debug_message = False # online #show_debug_message = False # online
areas = None areas = None

View File

@ -17,7 +17,7 @@ import sys
import platform import platform
import json import json
CONST_APP_VERSION = u"MaxBot (2022.09.19)" CONST_APP_VERSION = u"MaxBot (2022.10.19)"
CONST_FROM_TOP_TO_BOTTOM = u"from top to bottom" CONST_FROM_TOP_TO_BOTTOM = u"from top to bottom"
CONST_FROM_BOTTOM_TO_TOP = u"from bottom to top" CONST_FROM_BOTTOM_TO_TOP = u"from bottom to top"