-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correção na conexão com websocket crash.
- Loading branch information
1 parent
806f3e9
commit 53b8278
Showing
2 changed files
with
152 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,180 @@ | ||
import requests | ||
import websocket | ||
import _thread | ||
import time | ||
import json | ||
|
||
URL_API = "https://blaze.com" | ||
WSS_BASE = "wss://api-v2.blaze.com" | ||
VERSION_API = "0.0.1-professional" | ||
|
||
close_ws = False | ||
result_dict = None | ||
updated_at = None | ||
last_crashs = [] | ||
|
||
|
||
def get_ws_result(): | ||
return result_dict | ||
|
||
|
||
def set_ws_closed(status): | ||
global close_ws | ||
close_ws = status | ||
|
||
|
||
def get_crashs(): | ||
doubles = ba.get_last_crashs() | ||
if doubles: | ||
return [[item["value"], item["color"]] for item in doubles["items"]][::-1] | ||
|
||
|
||
def crashs_preview(): | ||
global last_crashs | ||
last_crashs = last_crashs[1:] | ||
colored_string = ', '.join([ | ||
f"\033[10;40m {item[0]} \033[m" if item[1] == "preto" | ||
else f"\033[10;42m {item[0]} \033[m" for item in last_crashs]) | ||
print(f"\r{colored_string}", end="") | ||
|
||
|
||
class Browser(object): | ||
|
||
def __init__(self): | ||
self.response = None | ||
self.headers = None | ||
self.session = requests.Session() | ||
|
||
def set_headers(self, headers=None): | ||
self.headers = { | ||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " | ||
"Chrome/87.0.4280.88 Safari/537.36" | ||
} | ||
if headers: | ||
for key, value in headers.items(): | ||
self.headers[key] = value | ||
|
||
def get_headers(self): | ||
return self.headers | ||
|
||
def send_request(self, method, url, **kwargs): | ||
return self.session.request(method, url, **kwargs) | ||
|
||
|
||
class BlazeAPI(Browser): | ||
|
||
def __init__(self, username=None, password=None): | ||
super().__init__() | ||
self.proxies = None | ||
self.token = None | ||
self.wallet_id = None | ||
self.username = username | ||
self.password = password | ||
self.set_headers() | ||
self.headers = self.get_headers() | ||
self.auth() | ||
|
||
def auth(self): | ||
data = { | ||
"username": self.username, | ||
"password": self.password | ||
} | ||
self.headers["referer"] = f"{URL_API}/pt/?modal=auth&tab=login" | ||
self.response = self.send_request("PUT", | ||
f"{URL_API}/api/auth/password", | ||
json=data, | ||
headers=self.headers) | ||
|
||
if not self.response.json().get("error"): | ||
self.token = self.response.json()["access_token"] | ||
|
||
return self.response.json() | ||
|
||
def reconnect(self): | ||
return self.auth() | ||
|
||
def get_profile(self): | ||
self.headers["authorization"] = f"Bearer {self.token}" | ||
self.response = self.send_request("GET", | ||
f"{URL_API}/api/users/me", | ||
headers=self.headers) | ||
return self.response.json() | ||
|
||
def get_balance(self): | ||
self.headers["authorization"] = f"Bearer {self.token}" | ||
self.response = self.send_request("GET", | ||
f"{URL_API}/api/wallets", | ||
headers=self.headers) | ||
if self.response.status_code == 502: | ||
self.reconnect() | ||
return self.get_balance() | ||
elif self.response: | ||
self.wallet_id = self.response.json()[0]["id"] | ||
return self.response.json() | ||
|
||
def get_last_crashs(self): | ||
self.response = self.send_request("GET", | ||
f"{URL_API}/api/crash_games/recent", | ||
proxies=self.proxies, | ||
headers=self.headers) | ||
if self.response: | ||
result = { | ||
"items": [{"color": "preto" if float(i["crash_point"]) < 2 else "verde", "value": i["crash_point"]} | ||
for i in self.response.json()]} | ||
return result | ||
return False | ||
|
||
|
||
def on_message(ws, message): | ||
global length | ||
global result_dict | ||
global close_ws | ||
global updated_at | ||
global last_crashs | ||
if "crash.tick" in message: | ||
data = json.loads(message[2:])[1]["payload"] | ||
if data["status"] == "complete" and length != len(message): | ||
length = len(message) | ||
print(f'\rCrash Point: {data["crash_point"]}, cor {"verde" if float(data["crash_point"]) > 2 else "preto"}') | ||
if data["crash_point"] == "0" or data["crash_point"].endswith('.00') or float(data["crash_point"]) > 49: | ||
print("\nPOSSÍVEL BRANCO NO DOUBLE...") | ||
result_dict = json.loads(message[2:])[1]["payload"] | ||
if result_dict["status"] == "complete" and updated_at != result_dict["updated_at"]: | ||
updated_at = result_dict["updated_at"] | ||
last_crashs.append([result_dict["crash_point"], | ||
"verde" if float(result_dict["crash_point"]) > 2 else "preto"]) | ||
crashs_preview() | ||
|
||
|
||
def on_error(ws, error): | ||
print(error) | ||
|
||
|
||
def on_close(ws, close_status_code, close_msg): | ||
time.sleep(2) | ||
def on_close(ws, status, msg): | ||
time.sleep(1) | ||
connect_websocket() | ||
|
||
|
||
def on_ping(ws, message): | ||
print("Got a ping! A pong reply has already been automatically sent.") | ||
|
||
|
||
def on_pong(ws, message): | ||
def on_pong(ws, msg): | ||
ws.send("2") | ||
|
||
|
||
def on_open(ws): | ||
def run(*args): | ||
time.sleep(1) | ||
message = '%d["cmd", {"id": "subscribe", "payload": {"room": "crash_v2"}}]' % 420 | ||
ws.send(message) | ||
time.sleep(0.1) | ||
message = '%d["cmd", {"id": "subscribe", "payload": {"room": "chat_room_2"}}]' % 422 | ||
ws.send(message) | ||
print('connection established') | ||
|
||
_thread.start_new_thread(run, ()) | ||
global last_crashs | ||
message = '%d["cmd", {"id": "subscribe", "payload": {"room": "crash_v2"}}]' % 420 | ||
ws.send(message) | ||
last_crashs = get_crashs() | ||
|
||
|
||
def connect_websocket(): | ||
# websocket.enableTrace(True) | ||
ws = websocket.WebSocketApp(f"{WSS_BASE}/replication/?EIO=3&transport=websocket", | ||
header=ba.headers, | ||
on_open=on_open, | ||
on_message=on_message, | ||
on_error=on_error, | ||
on_close=on_close, | ||
on_ping=on_ping, | ||
on_pong=on_pong | ||
) | ||
|
||
ws.run_forever(ping_interval=24, ping_timeout=1, ping_payload="2") | ||
ws.run_forever(ping_interval=24, | ||
ping_timeout=5, | ||
ping_payload="2", | ||
origin="https://blaze.com", | ||
host="api-v2.blaze.com") | ||
|
||
|
||
ba = BlazeAPI() | ||
|
||
if __name__ == "__main__": | ||
length = 0 | ||
try: | ||
connect_websocket() | ||
except Exception as err: | ||
print(err) | ||
print("connect failed") | ||
connect_websocket() |