Refactor code to follow pep8
parent
c4bbe9fcc6
commit
f6b33c603d
|
@ -1,49 +1,44 @@
|
|||
import base64
|
||||
import json
|
||||
from io import BytesIO
|
||||
|
||||
import requests
|
||||
from io import BytesIO
|
||||
from PIL import Image
|
||||
from typing import Optional
|
||||
from requests.exceptions import RequestException
|
||||
|
||||
class NonBrowser:
|
||||
def __init__(self, domain_name: str = "tixcraft.com") -> None:
|
||||
self.session = requests.Session()
|
||||
self.set_domain(domain_name)
|
||||
|
||||
class NonBrowser():
|
||||
def __init__(self, domain_name = "tixcraft.com") -> None:
|
||||
self.Session = requests.session()
|
||||
self.Set_Domain(domain_name)
|
||||
def set_cookies(self, cookies: Optional[dict]) -> bool:
|
||||
if cookies is not None:
|
||||
[self.session.cookies.set(cookie["name"], cookie["value"]) for cookie in cookies]
|
||||
return True
|
||||
return False
|
||||
|
||||
def Set_cookies(self, cookies:dict):
|
||||
ret = False
|
||||
if not cookies is None:
|
||||
for cookie in cookies:
|
||||
self.Session.cookies.set(cookie["name"],cookie["value"])
|
||||
ret = True
|
||||
return ret
|
||||
def get_cookies(self) -> dict:
|
||||
return self.session.cookies.get_dict()
|
||||
|
||||
def Get_cookies(self):
|
||||
return self.Session.cookies.get_dict()
|
||||
def set_headers(self, header: str) -> None:
|
||||
self.session.headers = header
|
||||
|
||||
def set_headers(self, header:str):
|
||||
self.Session.headers = header
|
||||
def set_domain(self, domain_name: str, captcha_url: str = "ticket/captcha", refresh_url: str = "ticket/captcha?refresh=1") -> None:
|
||||
self.url = f"https://{domain_name}/{captcha_url}"
|
||||
self.refresh_url = f"https://{domain_name}/{refresh_url}"
|
||||
|
||||
def Set_Domain(self, domain_name, captcha_url="ticket/captcha", refresh_url="ticket/captcha?refresh=1"):
|
||||
self.url = "https://%s/%s" % (domain_name, captcha_url)
|
||||
self.refresh_url = "https://%s/%s" % (domain_name, refresh_url)
|
||||
|
||||
def Request_Captcha(self):
|
||||
img = Image.open(BytesIO(self.Session.get(self.url, stream = True).content))
|
||||
def request_captcha(self) -> bytes:
|
||||
response = self.session.get(self.url, stream=True)
|
||||
img = Image.open(BytesIO(response.content))
|
||||
output_buffer = BytesIO()
|
||||
img.save(output_buffer, format='JPEG')
|
||||
binary_data = output_buffer.getvalue()
|
||||
base64_data = base64.b64encode(binary_data)
|
||||
return base64_data
|
||||
return base64.b64encode(binary_data)
|
||||
|
||||
def Request_Refresh_Captcha(self) -> str:
|
||||
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 ""
|
||||
response = self.session.get(self.refresh_url, stream=True)
|
||||
if response.status_code == 200:
|
||||
return response.json().get("url", "")
|
||||
except RequestException:
|
||||
pass
|
||||
return ""
|
|
@ -2039,7 +2039,7 @@ def tixcraft_get_ocr_answer(driver, ocr, ocr_captcha_image_source, Captcha_Brows
|
|||
|
||||
if ocr_captcha_image_source == CONST_OCR_CAPTCH_IMAGE_SOURCE_NON_BROWSER:
|
||||
if not Captcha_Browser is None:
|
||||
img_base64 = base64.b64decode(Captcha_Browser.Request_Captcha())
|
||||
img_base64 = base64.b64decode(Captcha_Browser.request_captcha())
|
||||
|
||||
if ocr_captcha_image_source == CONST_OCR_CAPTCH_IMAGE_SOURCE_CANVAS:
|
||||
image_id = 'TicketForm_verifyCode-image'
|
||||
|
@ -2073,7 +2073,7 @@ def tixcraft_get_ocr_answer(driver, ocr, ocr_captcha_image_source, Captcha_Brows
|
|||
if img_base64 is None:
|
||||
if not Captcha_Browser is None:
|
||||
print("canvas get image fail, use plan_b: NonBrowser")
|
||||
img_base64 = base64.b64decode(Captcha_Browser.Request_Captcha())
|
||||
img_base64 = base64.b64decode(Captcha_Browser.request_captcha())
|
||||
except Exception as exc:
|
||||
if show_debug_message:
|
||||
print("canvas exception:", str(exc))
|
||||
|
@ -2152,7 +2152,7 @@ def tixcraft_auto_ocr(driver, ocr, away_from_keyboard_enable, previous_answer, C
|
|||
else:
|
||||
# Non_Browser solution.
|
||||
if not Captcha_Browser is None:
|
||||
new_captcha_url = Captcha_Browser.Request_Refresh_Captcha() #取得新的CAPTCHA
|
||||
new_captcha_url = Captcha_Browser.request_refresh_captcha() #取得新的CAPTCHA
|
||||
if new_captcha_url != "":
|
||||
tixcraft_change_captcha(driver, new_captcha_url) #更改CAPTCHA圖
|
||||
else:
|
||||
|
@ -5730,10 +5730,10 @@ def set_non_browser_cookies(driver, url, Captcha_Browser):
|
|||
#PS: need set cookies once, if user change domain.
|
||||
if not Captcha_Browser is None:
|
||||
try:
|
||||
Captcha_Browser.Set_cookies(driver.get_cookies())
|
||||
Captcha_Browser.set_cookies(driver.get_cookies())
|
||||
except Exception as e:
|
||||
pass
|
||||
Captcha_Browser.Set_Domain(domain_name)
|
||||
Captcha_Browser.set_domain(domain_name)
|
||||
|
||||
def ticketmaster_parse_zone_info(driver, config_dict):
|
||||
show_debug_message = True # debug.
|
||||
|
@ -7007,7 +7007,7 @@ def ibon_auto_ocr(driver, config_dict, ocr, away_from_keyboard_enable, previous_
|
|||
img_base64 = None
|
||||
if ocr_captcha_image_source == CONST_OCR_CAPTCH_IMAGE_SOURCE_NON_BROWSER:
|
||||
if not Captcha_Browser is None:
|
||||
img_base64 = base64.b64decode(Captcha_Browser.Request_Captcha())
|
||||
img_base64 = base64.b64decode(Captcha_Browser.request_captcha())
|
||||
if ocr_captcha_image_source == CONST_OCR_CAPTCH_IMAGE_SOURCE_CANVAS:
|
||||
image_id = 'chk_pic'
|
||||
image_element = None
|
||||
|
@ -7072,7 +7072,7 @@ def ibon_auto_ocr(driver, config_dict, ocr, away_from_keyboard_enable, previous_
|
|||
else:
|
||||
# Non_Browser solution.
|
||||
if not Captcha_Browser is None:
|
||||
new_captcha_url = Captcha_Browser.Request_Refresh_Captcha() #取得新的CAPTCHA
|
||||
new_captcha_url = Captcha_Browser.request_refresh_captcha() #取得新的CAPTCHA
|
||||
if new_captcha_url != "":
|
||||
#PS:[TODO]
|
||||
#tixcraft_change_captcha(driver, new_captcha_url) #更改CAPTCHA圖
|
||||
|
@ -7245,7 +7245,7 @@ def ibon_main(driver, url, config_dict, ocr, Captcha_Browser):
|
|||
captcha_url = '/pic.aspx?TYPE=%s' % (model_name)
|
||||
#PS: need set cookies once, if user change domain.
|
||||
if not Captcha_Browser is None:
|
||||
Captcha_Browser.Set_Domain(domain_name, captcha_url=captcha_url)
|
||||
Captcha_Browser.set_domain(domain_name, captcha_url=captcha_url)
|
||||
|
||||
is_captcha_sent = ibon_captcha(driver, config_dict, ocr, Captcha_Browser, model_name)
|
||||
|
||||
|
@ -8984,7 +8984,7 @@ def kham_auto_ocr(driver, config_dict, ocr, away_from_keyboard_enable, previous_
|
|||
img_base64 = None
|
||||
if ocr_captcha_image_source == CONST_OCR_CAPTCH_IMAGE_SOURCE_NON_BROWSER:
|
||||
if not Captcha_Browser is None:
|
||||
img_base64 = base64.b64decode(Captcha_Browser.Request_Captcha())
|
||||
img_base64 = base64.b64decode(Captcha_Browser.request_captcha())
|
||||
if ocr_captcha_image_source == CONST_OCR_CAPTCH_IMAGE_SOURCE_CANVAS:
|
||||
image_id = 'chk_pic'
|
||||
image_element = None
|
||||
|
@ -9049,7 +9049,7 @@ def kham_auto_ocr(driver, config_dict, ocr, away_from_keyboard_enable, previous_
|
|||
else:
|
||||
# Non_Browser solution.
|
||||
if not Captcha_Browser is None:
|
||||
new_captcha_url = Captcha_Browser.Request_Refresh_Captcha() #取得新的CAPTCHA
|
||||
new_captcha_url = Captcha_Browser.request_refresh_captcha() #取得新的CAPTCHA
|
||||
if new_captcha_url != "":
|
||||
#PS:[TODO]
|
||||
#tixcraft_change_captcha(driver, new_captcha_url) #更改CAPTCHA圖
|
||||
|
@ -9169,8 +9169,8 @@ def kham_main(driver, url, config_dict, ocr, Captcha_Browser):
|
|||
|
||||
if config_dict["ocr_captcha"]["enable"]:
|
||||
if not Captcha_Browser is None:
|
||||
Captcha_Browser.Set_cookies(driver.get_cookies())
|
||||
Captcha_Browser.Set_Domain(domain_name)
|
||||
Captcha_Browser.set_cookies(driver.get_cookies())
|
||||
Captcha_Browser.set_domain(domain_name)
|
||||
break
|
||||
|
||||
#https://kham.com.tw/application/UTK02/UTK0201_.aspx?PRODUCT_ID=XXX
|
||||
|
@ -9264,7 +9264,7 @@ def kham_main(driver, url, config_dict, ocr, Captcha_Browser):
|
|||
captcha_url = '/pic.aspx?TYPE=%s' % (model_name)
|
||||
#PS: need set cookies once, if user change domain.
|
||||
if not Captcha_Browser is None:
|
||||
Captcha_Browser.Set_Domain(domain_name, captcha_url=captcha_url)
|
||||
Captcha_Browser.set_domain(domain_name, captcha_url=captcha_url)
|
||||
|
||||
is_captcha_sent = False
|
||||
|
||||
|
@ -9332,7 +9332,7 @@ def kham_main(driver, url, config_dict, ocr, Captcha_Browser):
|
|||
captcha_url = '/pic.aspx?TYPE=%s' % (model_name)
|
||||
#PS: need set cookies once, if user change domain.
|
||||
if not Captcha_Browser is None:
|
||||
Captcha_Browser.Set_Domain(domain_name, captcha_url=captcha_url)
|
||||
Captcha_Browser.set_domain(domain_name, captcha_url=captcha_url)
|
||||
|
||||
is_captcha_sent = False
|
||||
if config_dict["ocr_captcha"]["enable"]:
|
||||
|
@ -9382,7 +9382,7 @@ def kham_main(driver, url, config_dict, ocr, Captcha_Browser):
|
|||
captcha_url = '/pic.aspx?TYPE=%s' % (model_name)
|
||||
#PS: need set cookies once, if user change domain.
|
||||
if not Captcha_Browser is None:
|
||||
Captcha_Browser.Set_Domain(domain_name, captcha_url=captcha_url)
|
||||
Captcha_Browser.set_domain(domain_name, captcha_url=captcha_url)
|
||||
|
||||
kham_captcha(driver, config_dict, ocr, Captcha_Browser, model_name)
|
||||
|
||||
|
@ -10197,7 +10197,7 @@ def ticketplus_auto_ocr(driver, config_dict, ocr, previous_answer, Captcha_Brows
|
|||
img_base64 = None
|
||||
if ocr_captcha_image_source == CONST_OCR_CAPTCH_IMAGE_SOURCE_NON_BROWSER:
|
||||
if not Captcha_Browser is None:
|
||||
img_base64 = base64.b64decode(Captcha_Browser.Request_Captcha())
|
||||
img_base64 = base64.b64decode(Captcha_Browser.request_captcha())
|
||||
if ocr_captcha_image_source == CONST_OCR_CAPTCH_IMAGE_SOURCE_CANVAS:
|
||||
image_id = 'span.captcha-img'
|
||||
image_element = None
|
||||
|
@ -10660,8 +10660,8 @@ def ticketplus_main(driver, url, config_dict, ocr, Captcha_Browser):
|
|||
if config_dict["ocr_captcha"]["enable"]:
|
||||
domain_name = url.split('/')[2]
|
||||
if not Captcha_Browser is None:
|
||||
Captcha_Browser.Set_cookies(driver.get_cookies())
|
||||
Captcha_Browser.Set_Domain(domain_name)
|
||||
Captcha_Browser.set_cookies(driver.get_cookies())
|
||||
Captcha_Browser.set_domain(domain_name)
|
||||
|
||||
is_user_signin = ticketplus_account_auto_fill(driver, config_dict)
|
||||
if is_user_signin:
|
||||
|
|
|
@ -1376,8 +1376,8 @@ async def nodriver_ticketplus_main(tab, url, config_dict, ocr, Captcha_Browser):
|
|||
domain_name = url.split('/')[2]
|
||||
if not Captcha_Browser is None:
|
||||
# TODO:
|
||||
#Captcha_Browser.Set_cookies(driver.get_cookies())
|
||||
Captcha_Browser.Set_Domain(domain_name)
|
||||
#Captcha_Browser.set_cookies(driver.get_cookies())
|
||||
Captcha_Browser.set_domain(domain_name)
|
||||
|
||||
is_user_signin = await nodriver_ticketplus_account_auto_fill(tab, config_dict)
|
||||
|
||||
|
@ -1603,7 +1603,7 @@ async def nodriver_ibon_main(tab, url, config_dict, ocr, Captcha_Browser):
|
|||
captcha_url = '/pic.aspx?TYPE=%s' % (model_name)
|
||||
#PS: need set cookies once, if user change domain.
|
||||
if not Captcha_Browser is None:
|
||||
Captcha_Browser.Set_Domain(domain_name, captcha_url=captcha_url)
|
||||
Captcha_Browser.set_domain(domain_name, captcha_url=captcha_url)
|
||||
|
||||
# TODO:
|
||||
#is_captcha_sent = ibon_captcha(driver, config_dict, ocr, Captcha_Browser, model_name)
|
||||
|
|
Loading…
Reference in New Issue