Update app.py
Browse files
app.py
CHANGED
@@ -2,121 +2,96 @@ import asyncio
|
|
2 |
import websockets
|
3 |
import json
|
4 |
import requests
|
5 |
-
from datetime import datetime
|
6 |
import logging
|
|
|
|
|
7 |
|
8 |
# Set up logging configuration
|
9 |
logging.basicConfig(level=logging.INFO)
|
10 |
|
11 |
-
|
12 |
-
|
13 |
|
|
|
|
|
|
|
14 |
try:
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
logging.info("Submitted successfully to Google Form.")
|
19 |
except requests.exceptions.RequestException as e:
|
20 |
logging.error("Failed to submit to Google Form: " + str(e))
|
21 |
-
def send_form(form_data):
|
22 |
-
url = "https://docs.google.com/forms/u/0/d/e/1FAIpQLSfGlFpSsV9KG40ZT_ttcEFdNl_l2R6FqNDOsXvv8kdTBufVow/formResponse"
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
if dtCard[x] % 20 in [1, 14]:
|
36 |
-
a = 1
|
37 |
-
elif dtCard[x] % 20 in [11, 12, 13]:
|
38 |
-
a = 10
|
39 |
-
elif dtCard[x] % 20 in [2,3,4,5,6,7,8,9,10]:
|
40 |
-
a = dtCard[x] % 20
|
41 |
else:
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
def handle_case_25(data):
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
54 |
result = "T"
|
55 |
-
elif
|
56 |
result = "P"
|
57 |
else:
|
58 |
result = "B"
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
today = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
"
|
65 |
-
"
|
66 |
-
"
|
67 |
-
"
|
68 |
-
"
|
69 |
-
"
|
70 |
-
"
|
71 |
-
"
|
72 |
-
"
|
73 |
-
}
|
74 |
-
record_form = {
|
75 |
-
"entry.1910151925": groupID,
|
76 |
-
"entry.345081202": cardArr[0],
|
77 |
-
"entry.790265993": cardArr[2],
|
78 |
-
"entry.266290562": cardArr[4],
|
79 |
-
"entry.1840046760": cardArr[1],
|
80 |
-
"entry.2050858893": cardArr[3],
|
81 |
-
"entry.1372409472": cardArr[5],
|
82 |
-
"entry.211810544": playerScore,
|
83 |
-
"entry.2078666320": bankerScore,
|
84 |
"entry.1824106848": result,
|
|
|
|
|
|
|
|
|
85 |
}
|
86 |
return record_form
|
87 |
-
async def init_sma_ws():
|
88 |
-
uri = "wss://a45gs-t.wmetg.com/15101"
|
89 |
-
try:
|
90 |
-
async with websockets.connect(uri) as websocket:
|
91 |
-
# Send login message to the WebSocket server
|
92 |
-
await send_login(websocket)
|
93 |
-
print('Login message sent')
|
94 |
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
json_obj = json.loads(response)
|
102 |
-
protocol = json_obj["protocol"]
|
103 |
-
#print('protocol:', protocol)
|
104 |
-
#c = json_obj["data"]
|
105 |
-
#print('data:', json.dumps(c))
|
106 |
-
if protocol==25 :
|
107 |
-
data=handle_case_25(json_obj["data"])
|
108 |
-
send_form(data)
|
109 |
-
# Perform other processing based on the server response
|
110 |
-
|
111 |
-
# Send periodic heartbeat messages to maintain the connection
|
112 |
-
#await send_heartbeat(websocket)
|
113 |
-
except websockets.exceptions.ConnectionClosedError as e:
|
114 |
-
print(f"Connection closed: {e}")
|
115 |
-
break
|
116 |
-
except asyncio.CancelledError:
|
117 |
-
print("WebSocket connection cancelled.")
|
118 |
-
except Exception as e:
|
119 |
-
print(f"Error: {e}")
|
120 |
|
121 |
async def send_login(websocket):
|
122 |
# Create a JSON message containing login information
|
@@ -125,22 +100,10 @@ async def send_login(websocket):
|
|
125 |
"data": {
|
126 |
"sid": "ANONYMOUS-1000759",
|
127 |
"dtBetLimitSelectID": {
|
128 |
-
"101": 99101,
|
129 |
-
"
|
130 |
-
"
|
131 |
-
"
|
132 |
-
"105": 99105,
|
133 |
-
"106": 99106,
|
134 |
-
"107": 99107,
|
135 |
-
"108": 99108,
|
136 |
-
"110": 99110,
|
137 |
-
"111": 99111,
|
138 |
-
"112": 99112,
|
139 |
-
"113": 99113,
|
140 |
-
"125": 99125,
|
141 |
-
"126": 99126,
|
142 |
-
"128": 99128,
|
143 |
-
"129": 99129
|
144 |
},
|
145 |
"bGroupList": False,
|
146 |
"videoName": "HttpFlv",
|
@@ -149,19 +112,58 @@ async def send_login(websocket):
|
|
149 |
}
|
150 |
}
|
151 |
login_message = json.dumps(login_data)
|
152 |
-
|
153 |
# Send the login message to the WebSocket server
|
154 |
await websocket.send(login_message)
|
155 |
|
156 |
-
async def
|
157 |
-
|
158 |
-
|
159 |
-
|
|
|
|
|
|
|
|
|
160 |
|
161 |
-
|
162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
-
|
165 |
-
asyncio.run(init_sma_ws())
|
166 |
-
#await init_sma_ws()
|
167 |
-
# Run the event loop and execute the asynchronous main function
|
|
|
2 |
import websockets
|
3 |
import json
|
4 |
import requests
|
|
|
5 |
import logging
|
6 |
+
import gradio as gr
|
7 |
+
from datetime import datetime
|
8 |
|
9 |
# Set up logging configuration
|
10 |
logging.basicConfig(level=logging.INFO)
|
11 |
|
12 |
+
# Dictionary to store table information
|
13 |
+
table = {}
|
14 |
|
15 |
+
def send_form(form_data):
|
16 |
+
# URL for Google Form
|
17 |
+
url = "https://docs.google.com/forms/u/0/d/e/1FAIpQLSeDkeewjc5CEZ9ORYJix20rjGWr48WFSBryEqp7D2q5Hsd4PA/formResponse"
|
18 |
try:
|
19 |
+
# Send POST request with form data
|
20 |
+
response = requests.post(url, data=form_data)
|
21 |
+
response.raise_for_status() # Raise exception for HTTP errors
|
22 |
logging.info("Submitted successfully to Google Form.")
|
23 |
except requests.exceptions.RequestException as e:
|
24 |
logging.error("Failed to submit to Google Form: " + str(e))
|
|
|
|
|
25 |
|
26 |
+
def parse_card_values(dtCard):
|
27 |
+
# Parse card values based on modulus 20
|
28 |
+
card_values = []
|
29 |
+
for card in dtCard:
|
30 |
+
value = dtCard[card] % 20
|
31 |
+
if value in [1, 14]:
|
32 |
+
card_values.append(1)
|
33 |
+
elif value in [11, 12, 13]:
|
34 |
+
card_values.append(10)
|
35 |
+
elif value in range(2, 11):
|
36 |
+
card_values.append(value)
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
else:
|
38 |
+
card_values.append("")
|
39 |
+
return {"CardValues": card_values}
|
40 |
+
|
41 |
def handle_case_25(data):
|
42 |
+
# Extract data from case 25
|
43 |
+
group_id = data['groupID']
|
44 |
+
player_score = data['playerScore']
|
45 |
+
banker_score = data['bankerScore']
|
46 |
+
dt_card = data['dtCard']
|
47 |
+
card_values = parse_card_values(dt_card)["CardValues"]
|
48 |
+
|
49 |
+
# Determine result (Player, Banker, Tie)
|
50 |
+
if player_score == banker_score:
|
51 |
result = "T"
|
52 |
+
elif player_score > banker_score:
|
53 |
result = "P"
|
54 |
else:
|
55 |
result = "B"
|
56 |
|
57 |
+
# Retrieve additional info from table
|
58 |
+
if group_id in table:
|
59 |
+
info = table[group_id]
|
60 |
+
game_no = info['gameNo']
|
61 |
+
game_no_round = info['gameNoRound']
|
62 |
+
dealer_name = info['dealerName']
|
63 |
+
group_type = info['groupType']
|
64 |
+
else:
|
65 |
+
game_no = game_no_round = dealer_name = group_type = ""
|
66 |
+
|
67 |
+
# Current timestamp
|
68 |
today = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
69 |
+
|
70 |
+
# Construct form data
|
71 |
+
record_form = {
|
72 |
+
"entry.1910151925": group_id,
|
73 |
+
"entry.345081202": card_values[0],
|
74 |
+
"entry.790265993": card_values[2],
|
75 |
+
"entry.266290562": card_values[4],
|
76 |
+
"entry.1840046760": card_values[1],
|
77 |
+
"entry.2050858893": card_values[3],
|
78 |
+
"entry.1372409472": card_values[5],
|
79 |
+
"entry.211810544": player_score,
|
80 |
+
"entry.2078666320": banker_score,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
"entry.1824106848": result,
|
82 |
+
"entry.1579231357": group_type,
|
83 |
+
"entry.911826972": game_no,
|
84 |
+
"entry.305589575": game_no_round,
|
85 |
+
"entry.992348569": dealer_name
|
86 |
}
|
87 |
return record_form
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
+
async def send_heartbeat(websocket):
|
90 |
+
# Send a heartbeat message to the WebSocket server
|
91 |
+
heartbeat_message = {"protocol": 999, "data": {}}
|
92 |
+
await websocket.send(json.dumps(heartbeat_message))
|
93 |
+
# Wait for a period of time before sending the next heartbeat
|
94 |
+
await asyncio.sleep(10) # Send heartbeat every 10 seconds
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
async def send_login(websocket):
|
97 |
# Create a JSON message containing login information
|
|
|
100 |
"data": {
|
101 |
"sid": "ANONYMOUS-1000759",
|
102 |
"dtBetLimitSelectID": {
|
103 |
+
"101": 99101, "102": 99102, "103": 99103, "104": 99104,
|
104 |
+
"105": 99105, "106": 99106, "107": 99107, "108": 99108,
|
105 |
+
"110": 99110, "111": 99111, "112": 99112, "113": 99113,
|
106 |
+
"125": 99125, "126": 99126, "128": 99128, "129": 99129
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
},
|
108 |
"bGroupList": False,
|
109 |
"videoName": "HttpFlv",
|
|
|
112 |
}
|
113 |
}
|
114 |
login_message = json.dumps(login_data)
|
|
|
115 |
# Send the login message to the WebSocket server
|
116 |
await websocket.send(login_message)
|
117 |
|
118 |
+
async def init_sma_ws():
|
119 |
+
uri = "wss://a45gs-t.wmetg.com/15101"
|
120 |
+
try:
|
121 |
+
async with websockets.connect(uri) as websocket:
|
122 |
+
# Send login message to the WebSocket server
|
123 |
+
await send_login(websocket)
|
124 |
+
logging.info('Login message sent')
|
125 |
+
await send_heartbeat(websocket)
|
126 |
|
127 |
+
# Receive and process responses from the server
|
128 |
+
while True:
|
129 |
+
try:
|
130 |
+
response = await websocket.recv()
|
131 |
+
json_obj = json.loads(response)
|
132 |
+
protocol = json_obj["protocol"]
|
133 |
+
if protocol == 21:
|
134 |
+
info = json_obj["data"]
|
135 |
+
group_id = info['groupID']
|
136 |
+
game_no = info['gameNo']
|
137 |
+
game_no_round = info['gameNoRound']
|
138 |
+
dealer_name = info['dealerName']
|
139 |
+
group_type = info['groupType']
|
140 |
+
|
141 |
+
table[group_id] = {
|
142 |
+
'groupType': group_type,
|
143 |
+
'gameNo': game_no,
|
144 |
+
'gameNoRound': game_no_round,
|
145 |
+
'dealerName': dealer_name
|
146 |
+
}
|
147 |
+
|
148 |
+
if protocol == 25:
|
149 |
+
data = handle_case_25(json_obj["data"])
|
150 |
+
send_form(data)
|
151 |
+
except websockets.exceptions.ConnectionClosedError as e:
|
152 |
+
logging.error(f"Connection closed: {e}")
|
153 |
+
break
|
154 |
+
except asyncio.CancelledError:
|
155 |
+
logging.info("WebSocket connection cancelled.")
|
156 |
+
except Exception as e:
|
157 |
+
logging.error(f"Error: {e}")
|
158 |
+
|
159 |
+
def websocket_interface():
|
160 |
+
asyncio.run(init_sma_ws())
|
161 |
+
|
162 |
+
iface = gr.Interface(
|
163 |
+
fn=websocket_interface,
|
164 |
+
inputs=None,
|
165 |
+
outputs=gr.Textbox(label="Output"),
|
166 |
+
title="WebSocket Interface"
|
167 |
+
)
|
168 |
|
169 |
+
iface.launch(debug=True)
|
|
|
|
|
|