-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_bridge.py
More file actions
112 lines (85 loc) · 3.23 KB
/
Copy pathdata_bridge.py
File metadata and controls
112 lines (85 loc) · 3.23 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
"""
data_bridge.py
──────────────
TTL cache + mock/live toggle.
Stores normalized match dicts from currentMatches so scorecard
lookup never needs a second API call on basic tier.
"""
import time
import logging
from config import MOCK_MODE, CACHE_TTL
import mock_data
import cricket_api
log = logging.getLogger(__name__)
# ── In-memory store ────────────────────────────────────────────────
_cache: dict[str, tuple[float, object]] = {}
_match_store: dict[str, dict] = {} # match_id → normalized match dict
def _cache_get(key: str):
entry = _cache.get(key)
if entry:
ts, val = entry
if time.time() - ts < CACHE_TTL:
return val
return None
def _cache_set(key: str, val):
_cache[key] = (time.time(), val)
# ── Public API ─────────────────────────────────────────────────────
async def get_live_matches() -> list[dict]:
cached = _cache_get("live_matches")
if cached is not None:
return cached
if MOCK_MODE:
result = mock_data.get_mock_matches()
else:
result = await cricket_api.fetch_live_matches()
if not result:
log.warning("Live API empty — using mock matches.")
result = mock_data.get_mock_matches()
# Index by match_id for fast scorecard lookup
for m in result:
mid = m.get("match_id")
if mid:
_match_store[mid] = m
_cache_set("live_matches", result)
return result
async def get_scorecard(match_id: str) -> dict | None:
key = f"sc_{match_id}"
cached = _cache_get(key)
if cached is not None:
return cached
if MOCK_MODE or match_id.startswith("mock_"):
result = mock_data.get_mock_scorecard(match_id)
else:
# 1st choice: match already stored from /live call
result = _match_store.get(match_id)
# 2nd choice: try dedicated scorecard endpoint (premium feature)
if not result or not result.get("innings1"):
detailed = await cricket_api.fetch_scorecard(match_id)
if detailed and detailed.get("innings1"):
result = detailed
# 3rd choice: re-fetch live matches (refreshes store)
if not result:
await get_live_matches()
result = _match_store.get(match_id)
if not result:
log.warning("No data for %s — mock fallback.", match_id)
result = mock_data.get_mock_scorecard("mock_mi_rcb_20260412")
_cache_set(key, result)
return result
async def get_last_5_overs(match_id: str) -> list:
key = f"last5_{match_id}"
cached = _cache_get(key)
if cached is not None:
return cached
sc = await get_scorecard(match_id)
result = sc.get("last_5_overs", []) if sc else []
_cache_set(key, result)
return result
def get_primary_match_id() -> str | None:
"""Return match_id of the first match in store (most relevant live match)."""
if _match_store:
return next(iter(_match_store))
return None
def clear_cache():
_cache.clear()
_match_store.clear()