forked from Gilg4mesh/tixcraft_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNonBrowser.py
44 lines (37 loc) · 1.56 KB
/
NonBrowser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import base64
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)
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 get_cookies(self) -> dict:
return self.session.cookies.get_dict()
def set_headers(self, header: str) -> None:
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 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()
return base64.b64encode(binary_data)
def request_refresh_captcha(self) -> str:
try:
response = self.session.get(self.refresh_url, stream=True)
if response.status_code == 200:
return response.json().get("url", "")
except RequestException:
pass
return ""