From 7f3524170d50ec2a81c067aa78a600101ad7530d Mon Sep 17 00:00:00 2001 From: lovefirst02 Date: Thu, 12 Jan 2023 23:30:34 +0800 Subject: [PATCH 1/3] Modify: auto ocr feauture --- NonBrowser.py | 33 ++++++++++++++++++++++++++++ chrome_tixcraft.py | 55 +++++++++++++++++++++++++++++----------------- settings.json | 2 +- 3 files changed, 69 insertions(+), 21 deletions(-) create mode 100644 NonBrowser.py diff --git a/NonBrowser.py b/NonBrowser.py new file mode 100644 index 0000000..dbb57a5 --- /dev/null +++ b/NonBrowser.py @@ -0,0 +1,33 @@ +import requests +import json +import base64 +from PIL import Image +from io import BytesIO + +class NonBrowser(): + def __init__(self) -> None: + self.Session = requests.session() + self.url = "https://tixcraft.com/ticket/captcha" + self.refresh_url = "https://tixcraft.com/ticket/captcha?refresh=1" + + def Set_cookies(self,cookies:dict): + for cookie in cookies: + self.Session.cookies.set(cookie["name"],cookie["value"]) + return True + + def set_headers(self,header:str): + self.Session.headers = header + + def Request_Captcha(self): + img = Image.open(BytesIO(self.Session.get(self.url, stream = True).content)) + output_buffer = BytesIO() + img.save(output_buffer, format='JPEG') + binary_data = output_buffer.getvalue() + base64_data = base64.b64encode(binary_data) + return base64_data + + def Request_Refresh_Captcha(self) -> str: + result = self.Session.get(self.refresh_url, stream = True) + if result.status_code == 200: + json_data = json.loads(result.text) + return json_data.get("url","") \ No newline at end of file diff --git a/chrome_tixcraft.py b/chrome_tixcraft.py index 7385440..3fcc3f0 100644 --- a/chrome_tixcraft.py +++ b/chrome_tixcraft.py @@ -1607,6 +1607,12 @@ def tixcraft_verify(driver, presale_code): return ret +def tixcraft_change_captcha(driver,url): + try: + driver.execute_script(f"document.querySelector('.verify-img').children[0].setAttribute('src','{url}');") + except Exception as exc: + print("edit captcha element fail") + def tixcraft_toast(driver, message): toast_element = None try: @@ -1663,27 +1669,36 @@ def tixcraft_manully_keyin_verify_code(driver, answer = "", auto_submit = False) #PS: credit to LinShihJhang's share def tixcraft_auto_ocr(driver, ocr, ocr_captcha_with_submit): print("start to ddddocr") + from NonBrowser import NonBrowser + Non_Browser = NonBrowser() + Non_Browser.Set_cookies(driver.get_cookies()) + ocr_count = 0 #ocr 次數 + ocr_total_count = 0 #ocr 總次數 orc_answer = None - if not ocr is None: - try: - form_verifyCode_base64 = driver.execute_async_script(""" - var canvas = document.createElement('canvas'); - var context = canvas.getContext('2d'); - var img = document.getElementById('yw0'); - canvas.height = img.naturalHeight; - canvas.width = img.naturalWidth; - context.drawImage(img, 0, 0); - - callback = arguments[arguments.length - 1]; - callback(canvas.toDataURL()); - """) - img_base64 = base64.b64decode(form_verifyCode_base64.split(',')[1]) - orc_answer = ocr.classification(img_base64) - except Exception as exc: - pass - else: - print("ddddocr is None") - + while True: + ocr_count += 1 + ocr_total_count += 1 + if ocr_total_count > 6: #如果總次數大於6次, 直接手動輸入 + orc_answer = None + break + if ocr_count > 3: #如果次數大於3次, 取得新的Captcha + new_captcha_url = Non_Browser.Request_Refresh_Captcha() #取得新的CAPTCHA + if new_captcha_url != "": + ocr_count = 0 + tixcraft_change_captcha(driver,new_captcha_url) #更改CAPTCHA圖 + continue + img_base64 = base64.b64decode(Non_Browser.Request_Captcha()) + if not ocr is None: + try: + orc_answer = ocr.classification(img_base64) + if not orc_answer is None: + if len(orc_answer)==4: + break + except Exception as exc: + pass + else: + print("ddddocr is None") + print("ocr_total_count:",ocr_total_count) if not orc_answer is None: print("orc_answer:", orc_answer) if len(orc_answer)==4: diff --git a/settings.json b/settings.json index e8347a7..861dae8 100644 --- a/settings.json +++ b/settings.json @@ -1 +1 @@ -{"homepage": "https://tixcraft.com", "browser": "chrome", "language": "\u7e41\u9ad4\u4e2d\u6587", "ticket_number": 2, "pass_1_seat_remaining": true, "auto_check_agree": true, "ocr_captcha": {"enable": true, "auto_submit": false}, "kktix": {"auto_press_next_step_button": true, "auto_fill_ticket_number": true, "area_mode": "from top to bottom", "area_keyword_1": "", "area_keyword_1_and": "", "area_keyword_2": "", "area_keyword_2_and": "", "auto_guess_options": false, "user_guess_string": ""}, "tixcraft": {"date_auto_select": {"enable": true, "date_keyword": "", "mode": "from top to bottom"}, "area_auto_select": {"enable": true, "area_keyword_1": "", "area_keyword_2": "", "area_keyword_3": "", "area_keyword_4": "", "mode": "from top to bottom"}, "pass_date_is_sold_out": true, "auto_reload_coming_soon_page": true, "presale_code": ""}, "advanced": {"play_captcha_sound": {"enable": true, "filename": "ding-dong.wav"}, "facebook_account": "", "kktix_account": "", "adblock_plus_enable": true}, "debug": false} \ No newline at end of file +{"homepage": "https://tixcraft.com", "browser": "chrome", "language": "\u7e41\u9ad4\u4e2d\u6587", "ticket_number": 2, "pass_1_seat_remaining": true, "auto_check_agree": true, "ocr_captcha": {"enable": true, "auto_submit": false}, "kktix": {"auto_press_next_step_button": true, "auto_fill_ticket_number": true, "area_mode": "from top to bottom", "area_keyword_1": "", "area_keyword_1_and": "", "area_keyword_2": "", "area_keyword_2_and": "", "auto_guess_options": false, "user_guess_string": ""}, "tixcraft": {"date_auto_select": {"enable": true, "date_keyword": "0", "mode": "from top to bottom"}, "area_auto_select": {"enable": true, "area_keyword_1": "0", "area_keyword_2": "", "area_keyword_3": "", "area_keyword_4": "", "mode": "from top to bottom"}, "pass_date_is_sold_out": true, "auto_reload_coming_soon_page": true, "presale_code": ""}, "advanced": {"play_captcha_sound": {"enable": false, "filename": "ding-dong.wav"}, "facebook_account": "", "kktix_account": "", "adblock_plus_enable": true}, "debug": false} \ No newline at end of file From dd7bae884b50e22871bb529082e501efb2f85707 Mon Sep 17 00:00:00 2001 From: lovefirst02 Date: Thu, 12 Jan 2023 23:49:20 +0800 Subject: [PATCH 2/3] Modify: auto ocr fix --- NonBrowser.py | 13 +++++++++---- chrome_tixcraft.py | 14 +++++++++----- settings.json | 2 +- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/NonBrowser.py b/NonBrowser.py index dbb57a5..dc6fad4 100644 --- a/NonBrowser.py +++ b/NonBrowser.py @@ -27,7 +27,12 @@ class NonBrowser(): return base64_data def Request_Refresh_Captcha(self) -> str: - result = self.Session.get(self.refresh_url, stream = True) - if result.status_code == 200: - json_data = json.loads(result.text) - return json_data.get("url","") \ No newline at end of file + try: + result = self.Session.get(self.refresh_url, stream = True) + if result.status_code == 200: + json_data = json.loads(result.text) + return json_data.get("url","") + else: + return "" + except Exception as e: + return "" \ No newline at end of file diff --git a/chrome_tixcraft.py b/chrome_tixcraft.py index 3fcc3f0..ec70f59 100644 --- a/chrome_tixcraft.py +++ b/chrome_tixcraft.py @@ -1676,17 +1676,21 @@ def tixcraft_auto_ocr(driver, ocr, ocr_captcha_with_submit): ocr_total_count = 0 #ocr 總次數 orc_answer = None while True: - ocr_count += 1 - ocr_total_count += 1 - if ocr_total_count > 6: #如果總次數大於6次, 直接手動輸入 + if ocr_total_count >= 6: #如果總次數大於6次, 直接手動介入 orc_answer = None break - if ocr_count > 3: #如果次數大於3次, 取得新的Captcha + if ocr_count >= 3: #如果次數大於3次, 取得新的Captcha + print("Get New Captcha") new_captcha_url = Non_Browser.Request_Refresh_Captcha() #取得新的CAPTCHA - if new_captcha_url != "": + if new_captcha_url != "": #如果capthca取得成功繼續OCR, 失敗直接手動介入 ocr_count = 0 tixcraft_change_captcha(driver,new_captcha_url) #更改CAPTCHA圖 continue + else: + orc_answer = None + break + ocr_count += 1 + ocr_total_count += 1 img_base64 = base64.b64decode(Non_Browser.Request_Captcha()) if not ocr is None: try: diff --git a/settings.json b/settings.json index 861dae8..e8347a7 100644 --- a/settings.json +++ b/settings.json @@ -1 +1 @@ -{"homepage": "https://tixcraft.com", "browser": "chrome", "language": "\u7e41\u9ad4\u4e2d\u6587", "ticket_number": 2, "pass_1_seat_remaining": true, "auto_check_agree": true, "ocr_captcha": {"enable": true, "auto_submit": false}, "kktix": {"auto_press_next_step_button": true, "auto_fill_ticket_number": true, "area_mode": "from top to bottom", "area_keyword_1": "", "area_keyword_1_and": "", "area_keyword_2": "", "area_keyword_2_and": "", "auto_guess_options": false, "user_guess_string": ""}, "tixcraft": {"date_auto_select": {"enable": true, "date_keyword": "0", "mode": "from top to bottom"}, "area_auto_select": {"enable": true, "area_keyword_1": "0", "area_keyword_2": "", "area_keyword_3": "", "area_keyword_4": "", "mode": "from top to bottom"}, "pass_date_is_sold_out": true, "auto_reload_coming_soon_page": true, "presale_code": ""}, "advanced": {"play_captcha_sound": {"enable": false, "filename": "ding-dong.wav"}, "facebook_account": "", "kktix_account": "", "adblock_plus_enable": true}, "debug": false} \ No newline at end of file +{"homepage": "https://tixcraft.com", "browser": "chrome", "language": "\u7e41\u9ad4\u4e2d\u6587", "ticket_number": 2, "pass_1_seat_remaining": true, "auto_check_agree": true, "ocr_captcha": {"enable": true, "auto_submit": false}, "kktix": {"auto_press_next_step_button": true, "auto_fill_ticket_number": true, "area_mode": "from top to bottom", "area_keyword_1": "", "area_keyword_1_and": "", "area_keyword_2": "", "area_keyword_2_and": "", "auto_guess_options": false, "user_guess_string": ""}, "tixcraft": {"date_auto_select": {"enable": true, "date_keyword": "", "mode": "from top to bottom"}, "area_auto_select": {"enable": true, "area_keyword_1": "", "area_keyword_2": "", "area_keyword_3": "", "area_keyword_4": "", "mode": "from top to bottom"}, "pass_date_is_sold_out": true, "auto_reload_coming_soon_page": true, "presale_code": ""}, "advanced": {"play_captcha_sound": {"enable": true, "filename": "ding-dong.wav"}, "facebook_account": "", "kktix_account": "", "adblock_plus_enable": true}, "debug": false} \ No newline at end of file From c94c468f252562c4dbae33008489f9593cc66b0c Mon Sep 17 00:00:00 2001 From: lovefirst02 <38812009+lovefirst02@users.noreply.github.com> Date: Fri, 13 Jan 2023 01:44:14 +0800 Subject: [PATCH 3/3] Modify: fix ocr bug --- chrome_tixcraft.py | 1 + 1 file changed, 1 insertion(+) diff --git a/chrome_tixcraft.py b/chrome_tixcraft.py index ec70f59..b6bd9c8 100644 --- a/chrome_tixcraft.py +++ b/chrome_tixcraft.py @@ -1702,6 +1702,7 @@ def tixcraft_auto_ocr(driver, ocr, ocr_captcha_with_submit): pass else: print("ddddocr is None") + break print("ocr_total_count:",ocr_total_count) if not orc_answer is None: print("orc_answer:", orc_answer)