-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebsocket_feed.py
More file actions
385 lines (325 loc) · 14.3 KB
/
websocket_feed.py
File metadata and controls
385 lines (325 loc) · 14.3 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
"""
WebSocket Real-Time Odds Feed with Optional Initial Snapshot
Connects to the Odds-API WebSocket for real-time odds updates.
Optionally pre-fetches all current odds via REST API first, so you
have a complete snapshot before the live feed starts.
Usage:
# WebSocket only (no initial fetch)
python websocket_feed.py
# With initial snapshot (recommended)
python websocket_feed.py --prefetch
# Override config via environment variables
ODDS_API_KEY=abc123 python websocket_feed.py --prefetch
Requirements:
pip install websocket-client odds-api-io
"""
import os
import websocket
import json
import time
import threading
import argparse
from datetime import datetime, timezone
from urllib.parse import urlencode
from odds_api import OddsAPIClient
# ─── Configuration ────────────────────────────────────────────────────
# Set your API key via environment variable or replace the fallback below.
# Keep your key out of version control (use a .env file or export it).
API_KEY = os.environ.get("ODDS_API_KEY", "your_api_key_here")
# WebSocket filters
MARKETS = "ML,Spread,Totals" # Required (max 20, comma-separated)
SPORT = "football" # Optional (max 10, comma-separated)
LEAGUES = "england-premier-league" # Optional (max 20, comma-separated)
STATUS = "prematch" # "live" or "prematch" (optional)
BOOKMAKERS = "Bet365,SingBet" # Bookmakers for initial fetch
# WebSocket endpoint
WS_URL = "wss://api.odds-api.io/v3/ws"
# Batch size for multi-event odds fetch (max event IDs per request)
PREFETCH_BATCH_SIZE = 10
# ─────────────────────────────────────────────────────────────────────
def _timestamp():
"""Return a human-readable UTC timestamp for log lines."""
return datetime.now(timezone.utc).strftime("%H:%M:%S")
class OddsWebSocketClient:
"""
Real-time odds client with optional REST API pre-fetch.
When prefetch=True, fetches all current odds via REST before
connecting to WebSocket. This gives you a complete snapshot
so you don't miss any data while connecting.
"""
def __init__(self, api_key, markets, sport=None, leagues=None,
status=None, bookmakers=None, prefetch=False):
self.api_key = api_key
self.markets = markets
self.sport = sport
self.leagues = leagues
self.status = status
self.bookmakers = bookmakers or "Bet365"
self.prefetch = prefetch
self.ws = None
self.should_reconnect = True
self.reconnect_attempts = 0
self.max_reconnect_attempts = 10
self._reconnect_timer = None
# In-memory odds store: {event_id: {bookmaker: [markets]}}
self.odds_store = {}
# WebSocket uses "prematch"/"live", REST API uses "pending"/"live"
WS_TO_REST_STATUS = {
"prematch": "pending",
"live": "live",
}
def initial_fetch(self):
"""
Pre-fetch all current odds via REST API using batched
multi-event requests. Populates self.odds_store with a
complete snapshot.
"""
print("=" * 60)
print("INITIAL FETCH: Loading current odds via REST API...")
print("=" * 60)
client = OddsAPIClient(api_key=self.api_key)
try:
# Map WebSocket status to REST API status
rest_status = self.WS_TO_REST_STATUS.get(
self.status, self.status
) if self.status else None
# Get events for the configured sport/league(s).
# The REST API accepts a single league slug per request,
# so we iterate when multiple leagues are configured.
league_slugs = (
[l.strip() for l in self.leagues.split(",")]
if self.leagues else [None]
)
events = []
for slug in league_slugs:
events.extend(client.get_events(
sport=self.sport or "football",
league=slug,
status=rest_status,
))
event_ids = [str(e['id']) for e in events]
# Build a lookup for readable names
event_names = {
str(e['id']): f"{e.get('home', '?')} vs {e.get('away', '?')}"
for e in events
}
print(f"Found {len(events)} events. "
f"Fetching odds in batches of {PREFETCH_BATCH_SIZE}...\n")
# Fetch odds in batches using the multi-event endpoint
for i in range(0, len(event_ids), PREFETCH_BATCH_SIZE):
batch = event_ids[i:i + PREFETCH_BATCH_SIZE]
try:
results = client.get_odds_for_multiple_events(
event_ids=",".join(batch),
bookmakers=self.bookmakers,
)
for item in results:
eid = str(item.get('id', ''))
bookmakers = item.get('bookmakers', {})
if bookmakers:
# Normalise: ensure value is always
# {bookie: [market, ...]}
store_entry = {}
for bookie, markets in bookmakers.items():
store_entry[bookie] = (
markets if isinstance(markets, list)
else []
)
self.odds_store[eid] = store_entry
# Print summary
name = event_names.get(eid, eid)
for bookie, mkts in store_entry.items():
ml = next(
(m for m in mkts if m.get('name') == 'ML'),
None,
)
if ml and ml.get('odds'):
o = ml['odds'][0]
print(
f" {name} [{bookie}]: "
f"H {o.get('home', '-')} | "
f"D {o.get('draw', '-')} | "
f"A {o.get('away', '-')}"
)
except Exception as e:
print(f" Batch {i // PREFETCH_BATCH_SIZE + 1} failed: {e}")
print(f"\nInitial fetch complete: "
f"{len(self.odds_store)} events loaded")
print("=" * 60)
print()
finally:
client.close()
def build_url(self):
"""Build WebSocket URL with properly encoded query parameters."""
params = {"apiKey": self.api_key, "markets": self.markets}
if self.sport:
params["sport"] = self.sport
if self.leagues:
params["leagues"] = self.leagues
if self.status:
params["status"] = self.status
return f"{WS_URL}?{urlencode(params)}"
def on_message(self, ws, message):
"""Handle incoming WebSocket messages.
The server may send multiple JSON objects in a single
WebSocket frame (one per line), so we split and parse each.
"""
for line in message.strip().split('\n'):
line = line.strip()
if not line:
continue
try:
self._handle_parsed(json.loads(line))
except json.JSONDecodeError as e:
print(f"[{_timestamp()}] JSON parse error: {e}")
def _handle_parsed(self, data):
"""Process a single parsed message."""
try:
msg_type = data.get('type')
ts = _timestamp()
if msg_type == 'welcome':
print(f"[{ts}] Connected to Odds-API WebSocket")
print(f" Bookmakers: {data.get('bookmakers', [])}")
print(f" Sports: {data.get('sport_filter', [])}")
print(f" Leagues: {data.get('leagues_filter', [])}")
print(f" Status: {data.get('status_filter', 'all')}")
if data.get('warning'):
print(f" Warning: {data['warning']}")
print("\nListening for real-time updates...\n")
elif msg_type in ('created', 'updated'):
event_id = str(data.get('id', '?'))
bookie = data.get('bookie', '?')
label = "NEW" if msg_type == 'created' else "UPDATE"
# Update local store (always list of markets)
if event_id not in self.odds_store:
self.odds_store[event_id] = {}
self.odds_store[event_id][bookie] = data.get('markets', [])
# Print update
print(f"[{ts}] [{label}] Event {event_id} | {bookie}")
for market in data.get('markets', []):
odds = market.get('odds', [{}])[0]
name = market.get('name', '?')
if name == 'ML':
print(f" {name}: H {odds.get('home', '-')} | "
f"D {odds.get('draw', '-')} | "
f"A {odds.get('away', '-')}")
elif name == 'Totals':
print(f" {name} ({odds.get('hdp', '?')}): "
f"O {odds.get('over', '-')} | "
f"U {odds.get('under', '-')}")
elif name == 'Spread':
print(f" {name} ({odds.get('hdp', '?')}): "
f"H {odds.get('home', '-')} | "
f"A {odds.get('away', '-')}")
print()
elif msg_type == 'deleted':
event_id = str(data.get('id', '?'))
bookie = data.get('bookie', '?')
print(f"[{ts}] [DELETED] Event {event_id} | {bookie}\n")
# Remove from store
if event_id in self.odds_store:
self.odds_store[event_id].pop(bookie, None)
elif msg_type == 'no_markets':
print(f"[{ts}] [NO MARKETS] Event {data.get('id', '?')}\n")
except Exception as e:
print(f"[{_timestamp()}] Error handling message: {e}")
def on_error(self, ws, error):
print(f"[{_timestamp()}] WebSocket error: {error}")
def on_close(self, ws, close_status_code, close_msg):
print(f"[{_timestamp()}] Disconnected (code: {close_status_code})")
if self.should_reconnect:
self.reconnect_attempts += 1
if self.reconnect_attempts > self.max_reconnect_attempts:
print(f"Max reconnect attempts "
f"({self.max_reconnect_attempts}) reached. Giving up.")
return
# Exponential backoff: 1s, 2s, 4s, 8s... capped at 30s
delay = min(2 ** (self.reconnect_attempts - 1), 30)
print(f"Reconnecting in {delay}s "
f"(attempt {self.reconnect_attempts}"
f"/{self.max_reconnect_attempts})...")
# Schedule reconnect on a separate timer thread so we don't
# block the WebSocket callback thread.
self._reconnect_timer = threading.Timer(delay, self._start_ws)
self._reconnect_timer.daemon = True
self._reconnect_timer.start()
def on_open(self, ws):
print(f"[{_timestamp()}] WebSocket connection opened")
self.reconnect_attempts = 0 # Reset on successful connection
def _start_ws(self):
"""Start WebSocket connection in background thread."""
self.ws = websocket.WebSocketApp(
self.build_url(),
on_open=self.on_open,
on_message=self.on_message,
on_error=self.on_error,
on_close=self.on_close
)
# ping_interval keeps the connection alive and detects dead
# connections
ws_thread = threading.Thread(
target=self.ws.run_forever,
kwargs={"ping_interval": 30, "ping_timeout": 10}
)
ws_thread.daemon = True
ws_thread.start()
def start(self):
"""
Start the client. If prefetch is enabled, loads all current
odds via REST API first, then connects to WebSocket.
"""
if self.prefetch:
self.initial_fetch()
print(f"[{_timestamp()}] Connecting to WebSocket "
f"for real-time updates...")
self._start_ws()
def stop(self):
"""Stop the client."""
self.should_reconnect = False
if self._reconnect_timer:
self._reconnect_timer.cancel()
if self.ws:
self.ws.close()
def get_odds(self, event_id):
"""Get current odds for an event from the local store."""
return self.odds_store.get(str(event_id), {})
def main():
parser = argparse.ArgumentParser(
description="Odds-API WebSocket feed with optional initial snapshot"
)
parser.add_argument(
'--prefetch', action='store_true',
help='Pre-fetch all current odds via REST API before connecting '
'to WebSocket (recommended for complete data)'
)
args = parser.parse_args()
if API_KEY == "your_api_key_here":
print("ERROR: Set your API key via the ODDS_API_KEY environment "
"variable or edit API_KEY in this script.")
raise SystemExit(1)
print("Odds-API.io Real-Time Feed")
print("-" * 60)
if args.prefetch:
print("Mode: Initial REST fetch + WebSocket (recommended)\n")
else:
print("Mode: WebSocket only (use --prefetch for initial snapshot)\n")
client = OddsWebSocketClient(
api_key=API_KEY,
markets=MARKETS,
sport=SPORT,
leagues=LEAGUES,
status=STATUS,
bookmakers=BOOKMAKERS,
prefetch=args.prefetch
)
client.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\nStopping...")
client.stop()
print(f"Final store: {len(client.odds_store)} events cached")
print("Goodbye!")
if __name__ == "__main__":
main()