Merge pull request #22 from lovefirst02/master

Modify And Update Auto Ocr Feature
master
Max 2023-01-13 07:32:27 +08:00 committed by GitHub
commit 8b110b0e74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 14 deletions

38
NonBrowser.py Normal file
View File

@ -0,0 +1,38 @@
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:
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 ""

View File

@ -1608,6 +1608,12 @@ def tixcraft_verify(driver, presale_code):
return ret 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): def tixcraft_toast(driver, message):
toast_element = None toast_element = None
try: try:
@ -1686,25 +1692,17 @@ def tixcraft_reload_captcha(driver):
#PS: credit to LinShihJhang's share #PS: credit to LinShihJhang's share
def tixcraft_auto_ocr(driver, ocr, ocr_captcha_with_submit, ocr_captcha_force_submit, previous_answer): def tixcraft_auto_ocr(driver, ocr, ocr_captcha_with_submit, ocr_captcha_force_submit, previous_answer):
print("start to ddddocr") print("start to ddddocr")
from NonBrowser import NonBrowser
is_need_redo_ocr = False is_need_redo_ocr = False
is_form_sumbited = False is_form_sumbited = False
orc_answer = None orc_answer = None
if not ocr is None: if not ocr is None:
Non_Browser = NonBrowser()
Non_Browser.Set_cookies(driver.get_cookies())
img_base64 = base64.b64decode(Non_Browser.Request_Captcha())
try: 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) orc_answer = ocr.classification(img_base64)
except Exception as exc: except Exception as exc:
pass pass