-
Notifications
You must be signed in to change notification settings - Fork 48
/
bet365.py
291 lines (259 loc) · 9.57 KB
/
bet365.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# coding:utf-8
import re
import sys
import time
import requests
from requests import request
from autobahn.twisted.websocket import connectWS, WebSocketClientFactory, WebSocketClientProtocol
from autobahn.websocket.compress import (
PerMessageDeflateOffer,
PerMessageDeflateResponse,
PerMessageDeflateResponseAccept,
)
from autobahn.twisted.util import sleep
from twisted.python import log
from twisted.internet.defer import inlineCallbacks, returnValue
from twisted.internet import reactor, ssl
from twisted.internet.protocol import ReconnectingClientFactory
from txaio import start_logging, use_twisted
# use_twisted()
# start_logging(level='debug')
log.startLogging(sys.stdout)
occurred_eventids = []
checklist = {}
language = 'en' # en or cn
sport_type = 'football' # football or basketball
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0'
}
ODATA = {}
EV = {}
def toJson(string):
try:
dic = {}
data = string[:-1].split(';')
for item in data:
arr = item.split('=')
dic[arr[0]] = arr[1]
except Exception as e:
# print(e)
pass
return dic
def dataParse(self, string):
inPlayDatas = string.split('|CL;')
footballDatas = ""
basketballDatas = ""
if len(inPlayDatas) >= 2:
# soccerDatas = inPlayDatas[1]
for inPlayData in inPlayDatas:
if 'ID=1;' in inPlayData and 'CD=1;' in inPlayData:
footballDatas = inPlayData
elif 'ID=18;' in inPlayData:
basketballDatas = inPlayData
else:
return # End generator
if sport_type == 'football':
sportDatas = footballDatas
elif sport_type == 'basketball':
sportDatas = basketballDatas
else:
sportDatas = ''
competitions = sportDatas.split('|CT;')
if len(competitions) > 0:
competitions = competitions[1:]
else:
competitions = []
for comp in competitions:
data = comp.split('|EV;')
league = toJson(data[0]).get('NA')
for item in data[1:]:
MA = toJson(item.split('|MA;')[0])
eventid = MA['ID'][:8]
score = MA['SS']
# print(item.split('|MA;')[0])
PA0 = item.split('|PA;')[0]
PA0Json = toJson(PA0)
TU = PA0Json['TU']
TT = int(PA0Json['TT'])
TS = int(PA0Json['TS'])
TM = int(PA0Json['TM'])
begints = time.mktime(time.strptime(TU, '%Y%m%d%H%M%S'))
nowts = time.time() - 8 * 60 * 60
# The match has not started. TT=0 means the match has not started or paused, TM=45 means in the midfield.
if TM == 0 and TT == 0:
retimeset = '00:00'
else:
if TT == 1:
retimeset = str(int((nowts - begints)/60.0) + TM) + \
':' + str(int((nowts - begints) % 60.0)).zfill(2)
else:
retimeset = '45:00'
details = item.split('|PA;')[1:]
if len(details) >= 2:
hometeam = toJson(details[0]).get('NA')
awayteam = toJson(details[1]).get('NA')
else:
hometeam = ''
awayteam = ''
yield league, hometeam, awayteam, score, retimeset, eventid
time.sleep(3)
if language == 'en': # English
req = u'\x16\x00CONFIG_1_3,OVInPlay_1_3,Media_L1_Z3,XL_L1_Z3_C1_W3\x01'.encode('utf-8')
elif language == 'cn': # Chinese
req = u'\x16\x00CONFIG_10_0,OVInPlay_10_0,Media_L10_Z0,XL_L10_Z0_C1_W3\x01'.encode('utf-8')
else:
req = ''
self.sendMessage(req)
@inlineCallbacks
def search(league, hometeam, awayteam, score, retimeset, eventid):
yield sleep(0.3)
global occurred_eventids
global checklist
occurred_eventids.append(eventid)
checklist[eventid] = {
'league': league,
'hometeam': hometeam,
'awayteam': awayteam,
'score': score,
'retimeset': retimeset
}
print(league, hometeam, awayteam, score, retimeset, eventid)
req = u'\x16\x006V{}C18A_1_1\x01'.format(eventid).encode('utf-8')
returnValue(req)
class MyClientProtocol(WebSocketClientProtocol):
@inlineCallbacks
def subscribeGames(self, msg):
for league, hometeam, awayteam, score, retimeset, eventid in dataParse(self, msg):
try:
req = yield search(league, hometeam, awayteam, score, retimeset, eventid)
except Exception as e:
print('error!')
print(e)
self.sendClose(1000)
else:
self.sendMessage(req)
def updateGameData(self, msg):
for m in msg.split('|\x08'):
d = m.split('\x01U|')
IT = d[0].replace('\x15', '')
if len(d) > 1 and IT in ODATA.keys():
dic = toJson(d[1])
for k in dic.keys():
ODATA[IT][k] = dic[k]
# print('update ', IT, dic)
def newGameDataParse(self, msg):
data = msg.split('|')
EVC = {}
MGC = {}
MAC = {}
for item in data:
if item.startswith('EV;'):
dic = toJson(item[3:])
IT = dic.get('IT')
ODATA[IT] = dic
EVC = dic
EVC["ST"] = []
EVC["MG"] = []
EV[EVC["FI"]] = EVC
if item.startswith('ST;'):
dic = toJson(item[3:])
EVC["ST"].append(dic)
IT = dic.get('IT')
ODATA[IT] = dic
if(item.startswith('MG;')):
MGC = toJson(item[3:])
EVC["MG"].append(MGC)
MGC["MA"] = []
IT = dic.get('IT')
ODATA[IT] = MGC
if(item.startswith('MA;')):
MAC = toJson(item[3:])
MGC["MA"].append(MAC)
MAC["PA"] = []
IT = dic.get('IT')
ODATA[IT] = MAC
if(item.startswith('PA')):
dic = toJson(item[3:])
MAC["PA"].append(dic)
IT = dic.get('IT')
ODATA[IT] = dic
# print(len(EV.keys()))
# print(len(ODATA.keys()))
def sendMessage(self, message):
# print("Send: ", message)
super().sendMessage(message)
def onOpen(self):
print('On Open')
req = str('\x23\x03P\x01__time,S_{}\x00'.format(
self.factory.session_id)).encode('utf-8')
# print('sending message:', req)
self.sendMessage(req)
@inlineCallbacks
def onMessage(self, payload, isBinary):
msg = payload.decode('utf-8')
if msg.startswith('100'):
if language == 'en': # English
req = u'\x16\x00CONFIG_1_3,OVInPlay_1_3,Media_L1_Z3,XL_L1_Z3_C1_W3\x01'.encode(
'utf-8')
elif language == 'cn': # Chinese
req = u'\x16\x00CONFIG_10_0,OVInPlay_10_0,Media_L10_Z0,XL_L10_Z0_C1_W3\x01'.encode(
'utf-8')
else:
req = ''
self.sendMessage(req)
# req2 = u'\x16\x00OVM5\x01'.encode('utf-8')
# self.sendMessage(req2)
if language == 'en': # English
msg_header = 'OVInPlay_1_3'
elif language == 'cn': # Chinese
msg_header = 'OVInPlay_10_0'
if msg_header in msg:
yield self.subscribeGames(msg)
else:
matched_id1 = msg.split('F|EV;')[0][-17:-9]
matched_id2 = msg.split('F|EV;')[0][-16:-8]
if matched_id1 not in occurred_eventids and matched_id2 not in occurred_eventids:
self.updateGameData(msg)
else:
self.newGameDataParse(msg)
class MyFactory(WebSocketClientFactory, ReconnectingClientFactory):
def clientConnectionFailed(self, connector, reason):
self.retry(connector)
def clientConnectionLost(self, connector, reason):
self.retry(connector)
def get_session_id():
headers = {
'Cookie': 'rmbs=3; aps03=cf=N&cg=2&cst=0&ct=42&hd=N&lng=10&oty=2&tzi=27; session=processform=0',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0'
}
url = 'https://www.288365.com/defaultapi/sports-configuration'
response = requests.get(url=url, headers=headers)
session_id = response.cookies['pstk']
return session_id
if __name__ == '__main__':
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36"
url = 'wss://premws-pt3.365lpodds.com/zap/'
factory = MyFactory(
url, useragent=USER_AGENT, protocols=['zap-protocol-v1'])
factory.protocol = MyClientProtocol
factory.headers = {}
factory.session_id = get_session_id()
# factory.session_id = '44263EAB3A86367F8DF053097CF36FBB000003'
def accept(response):
if isinstance(response, PerMessageDeflateResponse):
return PerMessageDeflateResponseAccept(response)
factory.setProtocolOptions(perMessageCompressionAccept=accept)
factory.setProtocolOptions(perMessageCompressionOffers=[PerMessageDeflateOffer(
accept_max_window_bits=True,
accept_no_context_takeover=True,
request_max_window_bits=0,
request_no_context_takeover=True,
)])
# reactor.callFromThread(connectWS, factory)
# reactor.run()
if factory.isSecure:
contextFactory = ssl.ClientContextFactory()
else:
contextFactory = None
connectWS(factory, contextFactory)
reactor.run()