tixcraft_bot/NonBrowser.py

44 lines
1.6 KiB
Python
Raw Normal View History

2023-01-12 15:30:34 +00:00
import base64
import requests
2024-04-24 01:57:01 +00:00
from io import BytesIO
from PIL import Image
2024-04-24 01:57:01 +00:00
from typing import Optional
from requests.exceptions import RequestException
2024-04-24 01:57:01 +00:00
class NonBrowser:
def __init__(self, domain_name: str = "tixcraft.com") -> None:
self.session = requests.Session()
self.set_domain(domain_name)
2024-04-24 01:57:01 +00:00
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
2023-01-12 15:30:34 +00:00
2024-04-24 01:57:01 +00:00
def get_cookies(self) -> dict:
return self.session.cookies.get_dict()
2024-04-24 01:57:01 +00:00
def set_headers(self, header: str) -> None:
self.session.headers = header
2023-01-12 15:30:34 +00:00
2024-04-24 01:57:01 +00:00
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}"
2023-01-14 09:53:32 +00:00
2024-04-24 01:57:01 +00:00
def request_captcha(self) -> bytes:
response = self.session.get(self.url, stream=True)
img = Image.open(BytesIO(response.content))
2023-01-12 15:30:34 +00:00
output_buffer = BytesIO()
img.save(output_buffer, format='JPEG')
binary_data = output_buffer.getvalue()
2024-04-24 01:57:01 +00:00
return base64.b64encode(binary_data)
2023-01-12 15:30:34 +00:00
2024-04-24 01:57:01 +00:00
def request_refresh_captcha(self) -> str:
2023-01-12 15:49:20 +00:00
try:
2024-04-24 01:57:01 +00:00
response = self.session.get(self.refresh_url, stream=True)
if response.status_code == 200:
return response.json().get("url", "")
except RequestException:
pass
return ""