Modify: auto ocr feauture

master
lovefirst02 2023-01-12 23:30:34 +08:00
parent 2220c32cf2
commit 7f3524170d
3 changed files with 69 additions and 21 deletions

33
NonBrowser.py Normal file
View File

@ -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","")

View File

@ -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:

View File

@ -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}
{"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}