espn-odds-scraper 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,35 @@
1
+ """ESPN MLB odds scraper package."""
2
+
3
+ from .scraper import (
4
+ scrape_espn_mlb,
5
+ scrape_espn_mlb_years,
6
+ get_games,
7
+ get_odds,
8
+ fetch_line_history,
9
+ get_open_close,
10
+ to_legacy_format,
11
+ clean_game_data,
12
+ )
13
+ from .pipeline import (
14
+ scrape,
15
+ scrape_line_movement,
16
+ add_game_ids,
17
+ get_mlb_team_map,
18
+ )
19
+
20
+ __version__ = "0.2.0"
21
+
22
+ __all__ = [
23
+ "scrape",
24
+ "scrape_line_movement",
25
+ "add_game_ids",
26
+ "get_mlb_team_map",
27
+ "scrape_espn_mlb",
28
+ "scrape_espn_mlb_years",
29
+ "get_games",
30
+ "get_odds",
31
+ "fetch_line_history",
32
+ "get_open_close",
33
+ "to_legacy_format",
34
+ "clean_game_data",
35
+ ]
@@ -0,0 +1,453 @@
1
+ """STATSAPI V2 pipeline integration: sbr_odds-style output with MLB game_ids.
2
+
3
+ Usage in Main.ipynb:
4
+
5
+ import espn_odds_scraper as espn
6
+
7
+ if update_espn_odds:
8
+ espn_odds = espn.scrape(fetch_start_date, fetch_end_date, engine)
9
+ pdu.to_sql_upsert(espn_odds, 'espn_odds', engine,
10
+ ['game_id', 'sportsbook', 'odds_type'])
11
+
12
+ # one-time historical movement backfill (ESPN carries this <=2023 only):
13
+ # movement = espn.scrape_line_movement('2015-04-01', '2023-11-01', engine)
14
+ # pdu.to_sql_upsert(movement, 'espn_line_movement', engine,
15
+ # ['espn_game_id', 'market', 'timestamp'])
16
+ """
17
+
18
+ import re
19
+ import time
20
+ from concurrent.futures import ThreadPoolExecutor, as_completed
21
+ from typing import Iterable
22
+
23
+ import pandas as pd
24
+ import requests
25
+
26
+ from .scraper import (
27
+ CORE_BASE,
28
+ BET_TYPES,
29
+ HISTORY_PROVIDER_ID,
30
+ SCOREBOARD_URL,
31
+ _daterange,
32
+ _date_str,
33
+ _get_json,
34
+ _pick,
35
+ _to_num,
36
+ )
37
+
38
+ TEAM_NAME_ALIASES = {
39
+ "d-backs": "diamondbacks",
40
+ "diamondbacks": "diamondbacks",
41
+ "athletics": "athletics",
42
+ "a's": "athletics",
43
+ "oakland athletics": "athletics",
44
+ }
45
+
46
+
47
+ # ---------------------------------------------------------------------------
48
+ # Schedule (includes venue) and per-book odds with open/current snapshots
49
+ # ---------------------------------------------------------------------------
50
+
51
+ def _get_games_pipeline(date):
52
+ data = _get_json(SCOREBOARD_URL, params={"dates": _date_str(date)})
53
+ if not data:
54
+ return []
55
+ games = []
56
+ for event in data.get("events", []):
57
+ try:
58
+ comp = event["competitions"][0]
59
+ status = comp.get("status", {}).get("type", {})
60
+ row = {
61
+ "espn_game_id": event["id"],
62
+ "game_datetime": event.get("date"),
63
+ "status": status.get("name", ""),
64
+ "completed": bool(status.get("completed", False)),
65
+ "venue": _pick(comp, "venue", "fullName") or "",
66
+ }
67
+ for side in comp.get("competitors", []):
68
+ prefix = "home" if side.get("homeAway") == "home" else "away"
69
+ team = side.get("team", {})
70
+ row[f"{prefix}_team"] = team.get("displayName", "")
71
+ row[f"{prefix}_team_short"] = team.get("abbreviation", "")
72
+ score = side.get("score", "")
73
+ row[f"{prefix}_score"] = (
74
+ pd.to_numeric(score, errors="coerce") if score != "" else None
75
+ )
76
+ games.append(row)
77
+ except Exception as e: # noqa: BLE001
78
+ print(f"Error parsing ESPN event on {date}: {e}")
79
+ return games
80
+
81
+
82
+ def _parse_phase_flat(item, phase):
83
+ home = _pick(item, "homeTeamOdds", phase) or {}
84
+ away = _pick(item, "awayTeamOdds", phase) or {}
85
+ totals = item.get(phase) or {}
86
+ return {
87
+ "home_ml": _to_num(_pick(home, "moneyLine", "american")),
88
+ "away_ml": _to_num(_pick(away, "moneyLine", "american")),
89
+ "spread": _to_num(_pick(home, "pointSpread", "american")),
90
+ "home_spread_odds": _to_num(_pick(home, "spread", "american")),
91
+ "away_spread_odds": _to_num(_pick(away, "spread", "american")),
92
+ "total": _to_num(_pick(totals, "total", "american")),
93
+ "over_odds": _to_num(_pick(totals, "over", "american")),
94
+ "under_odds": _to_num(_pick(totals, "under", "american")),
95
+ }
96
+
97
+
98
+ def _get_odds_pipeline(espn_game_id):
99
+ url = f"{CORE_BASE}/events/{espn_game_id}/competitions/{espn_game_id}/odds"
100
+ data = _get_json(url)
101
+ if not data:
102
+ return []
103
+ rows = []
104
+ for item in data.get("items", []):
105
+ try:
106
+ provider = item.get("provider", {})
107
+ cur = _parse_phase_flat(item, "current")
108
+ opn = _parse_phase_flat(item, "open")
109
+ if cur["home_ml"] is None:
110
+ cur["home_ml"] = _to_num(_pick(item, "homeTeamOdds", "moneyLine"))
111
+ if cur["away_ml"] is None:
112
+ cur["away_ml"] = _to_num(_pick(item, "awayTeamOdds", "moneyLine"))
113
+ if cur["total"] is None:
114
+ cur["total"] = _to_num(item.get("overUnder"))
115
+ if cur["over_odds"] is None:
116
+ cur["over_odds"] = _to_num(item.get("overOdds"))
117
+ if cur["under_odds"] is None:
118
+ cur["under_odds"] = _to_num(item.get("underOdds"))
119
+ rows.append(
120
+ {
121
+ "espn_game_id": str(espn_game_id),
122
+ "sportsbook": provider.get("name", ""),
123
+ "sportsbook_id": provider.get("id", ""),
124
+ "current": cur,
125
+ "open": opn,
126
+ }
127
+ )
128
+ except Exception as e: # noqa: BLE001
129
+ print(f"Error parsing odds for ESPN game {espn_game_id}: {e}")
130
+ return rows
131
+
132
+
133
+ # ---------------------------------------------------------------------------
134
+ # MLB game_id mapping (same approach as picktheodds_scraper.add_game_ids)
135
+ # ---------------------------------------------------------------------------
136
+
137
+ def _normalize_team_name(name: str) -> str:
138
+ normalized = re.sub(r"[^a-z0-9]+", " ", str(name).lower()).strip()
139
+ normalized = re.sub(r"\s+", " ", normalized)
140
+ return TEAM_NAME_ALIASES.get(normalized, normalized)
141
+
142
+
143
+ def get_mlb_team_map() -> pd.DataFrame:
144
+ response = requests.get(
145
+ "https://statsapi.mlb.com/api/v1/teams?sportId=1", timeout=30
146
+ )
147
+ response.raise_for_status()
148
+ rows = []
149
+ for team in response.json().get("teams", []):
150
+ team_id = team.get("id")
151
+ names = {
152
+ team.get("name"),
153
+ team.get("teamName"),
154
+ team.get("clubName"),
155
+ team.get("shortName"),
156
+ team.get("abbreviation"),
157
+ }
158
+ for name in names:
159
+ if name:
160
+ rows.append(
161
+ {"team_name_key": _normalize_team_name(name),
162
+ "mlb_team_id": team_id}
163
+ )
164
+ return pd.DataFrame(rows).drop_duplicates("team_name_key")
165
+
166
+
167
+ def add_game_ids(
168
+ espn_games: pd.DataFrame,
169
+ engine,
170
+ schedule_table: str = "cleaned_schedule",
171
+ tz: str = "US/Eastern",
172
+ tolerance_hrs: int = 12,
173
+ ) -> pd.DataFrame:
174
+ """Attach MLB statsapi game_id to ESPN games via the schedule table."""
175
+ if espn_games.empty or engine is None:
176
+ espn_games = espn_games.copy()
177
+ espn_games["game_id"] = None
178
+ return espn_games
179
+
180
+ try:
181
+ schedule = pd.read_sql_table(schedule_table, engine)
182
+ except ValueError:
183
+ schedule = pd.read_sql_table("processed_schedule", engine)
184
+
185
+ team_map = get_mlb_team_map()
186
+ espn_games = espn_games.copy()
187
+ espn_games["home_team_key"] = espn_games["home_team"].apply(_normalize_team_name)
188
+ espn_games["away_team_key"] = espn_games["away_team"].apply(_normalize_team_name)
189
+
190
+ espn_games = espn_games.merge(
191
+ team_map.rename(columns={"team_name_key": "home_team_key",
192
+ "mlb_team_id": "home_mlb_team_id"}),
193
+ on="home_team_key", how="left",
194
+ )
195
+ espn_games = espn_games.merge(
196
+ team_map.rename(columns={"team_name_key": "away_team_key",
197
+ "mlb_team_id": "away_mlb_team_id"}),
198
+ on="away_team_key", how="left",
199
+ )
200
+
201
+ schedule = schedule[
202
+ ["game_id", "game_datetime", "home_team", "away_team"]
203
+ ].copy()
204
+ schedule.rename(
205
+ columns={"home_team": "home_mlb_team_id", "away_team": "away_mlb_team_id"},
206
+ inplace=True,
207
+ )
208
+ schedule["merge_datetime"] = (
209
+ pd.to_datetime(schedule["game_datetime"], utc=True)
210
+ .dt.tz_convert(tz)
211
+ .dt.tz_localize(None)
212
+ .astype("datetime64[us]")
213
+ )
214
+ schedule = schedule.drop(columns=["game_datetime"]).sort_values("merge_datetime")
215
+
216
+ espn_games["merge_datetime"] = (
217
+ pd.to_datetime(espn_games["game_datetime"], utc=True)
218
+ .dt.tz_convert(tz)
219
+ .dt.tz_localize(None)
220
+ .astype("datetime64[us]")
221
+ )
222
+ espn_games.sort_values("merge_datetime", inplace=True)
223
+
224
+ espn_games = pd.merge_asof(
225
+ espn_games,
226
+ schedule,
227
+ on="merge_datetime",
228
+ by=["home_mlb_team_id", "away_mlb_team_id"],
229
+ direction="nearest",
230
+ tolerance=pd.Timedelta(f"{tolerance_hrs}hr"),
231
+ )
232
+ espn_games.drop(columns=["home_team_key", "away_team_key", "merge_datetime"],
233
+ inplace=True)
234
+ espn_games["_has_game_id"] = espn_games["game_id"].notna()
235
+ espn_games.sort_values(["espn_game_id", "_has_game_id"],
236
+ ascending=[True, False], inplace=True)
237
+ espn_games.drop_duplicates("espn_game_id", keep="first", inplace=True)
238
+ espn_games.drop(columns=["_has_game_id"], inplace=True)
239
+
240
+ mapped = espn_games["game_id"].notna().sum()
241
+ if mapped < len(espn_games):
242
+ missing = espn_games.loc[espn_games["game_id"].isna(), "game_datetime"]
243
+ missing_dates = sorted(pd.to_datetime(missing).dt.date.unique())
244
+ print(
245
+ f"Mapped {mapped}/{len(espn_games)} ESPN games to MLB game_id. "
246
+ f"Missing dates in {schedule_table}: {missing_dates[:5]}"
247
+ )
248
+ return espn_games
249
+
250
+
251
+ # ---------------------------------------------------------------------------
252
+ # Main entry point (sbr_odds-style long output)
253
+ # ---------------------------------------------------------------------------
254
+
255
+ ESPN_ODDS_COLUMNS = [
256
+ "game_id", "espn_game_id", "date", "game_datetime",
257
+ "away_team", "away_team_short", "home_team", "home_team_short",
258
+ "away_score", "home_score", "venue", "status",
259
+ "sportsbook", "sportsbook_id", "odds_type",
260
+ "opening_home_odds", "opening_away_odds", "opening_line",
261
+ "current_home_odds", "current_away_odds", "current_line",
262
+ "scraped_at",
263
+ ]
264
+
265
+
266
+ def scrape(
267
+ start_date: str,
268
+ end_date: str,
269
+ engine=None,
270
+ odds_types: Iterable[str] = ("moneyline", "spread", "total"),
271
+ exclude_live: bool = True,
272
+ max_workers: int = 8,
273
+ schedule_table: str = "cleaned_schedule",
274
+ ) -> pd.DataFrame:
275
+ """Scrape ESPN MLB odds shaped like the sbr_odds table.
276
+
277
+ One row per game x sportsbook x odds_type. For totals, home = over and
278
+ away = under. 'current' odds are the closing line for completed games.
279
+ Opening odds are real from ~2024 onward; for <=2023 openers use
280
+ scrape_line_movement.
281
+
282
+ Upsert with keys ['game_id', 'sportsbook', 'odds_type'].
283
+ """
284
+ dates = _daterange(start_date, end_date)
285
+
286
+ games = []
287
+ with ThreadPoolExecutor(max_workers=max_workers) as pool:
288
+ for result in pool.map(_get_games_pipeline, dates):
289
+ games.extend(result)
290
+ print(f"Found {len(games)} ESPN games between {start_date} and {end_date}")
291
+ if not games:
292
+ return pd.DataFrame(columns=ESPN_ODDS_COLUMNS)
293
+
294
+ games_df = pd.DataFrame(games)
295
+ games_df = add_game_ids(games_df, engine, schedule_table=schedule_table)
296
+
297
+ odds_rows = []
298
+ ids = games_df["espn_game_id"].tolist()
299
+ with ThreadPoolExecutor(max_workers=max_workers) as pool:
300
+ futures = {pool.submit(_get_odds_pipeline, gid): gid for gid in ids}
301
+ for future in as_completed(futures):
302
+ odds_rows.extend(future.result())
303
+ print(f"Fetched odds for {len(ids)} games: {len(odds_rows)} book entries")
304
+
305
+ if exclude_live:
306
+ odds_rows = [
307
+ r for r in odds_rows if "live odds" not in r["sportsbook"].lower()
308
+ ]
309
+
310
+ games_idx = games_df.set_index("espn_game_id")
311
+ scraped_at = int(time.time())
312
+ rows = []
313
+ for entry in odds_rows:
314
+ gid = entry["espn_game_id"]
315
+ if gid not in games_idx.index:
316
+ continue
317
+ game = games_idx.loc[gid]
318
+ base = {
319
+ "game_id": game["game_id"],
320
+ "espn_game_id": gid,
321
+ "date": pd.to_datetime(game["game_datetime"], utc=True)
322
+ .tz_convert("US/Eastern").strftime("%Y-%m-%d"),
323
+ "game_datetime": game["game_datetime"],
324
+ "away_team": game["away_team"],
325
+ "away_team_short": game["away_team_short"],
326
+ "home_team": game["home_team"],
327
+ "home_team_short": game["home_team_short"],
328
+ "away_score": game["away_score"],
329
+ "home_score": game["home_score"],
330
+ "venue": game["venue"],
331
+ "status": game["status"],
332
+ "sportsbook": entry["sportsbook"],
333
+ "sportsbook_id": entry["sportsbook_id"],
334
+ "scraped_at": scraped_at,
335
+ }
336
+ cur, opn = entry["current"], entry["open"]
337
+ market_fields = {
338
+ "moneyline": {
339
+ "opening_home_odds": opn["home_ml"],
340
+ "opening_away_odds": opn["away_ml"],
341
+ "opening_line": None,
342
+ "current_home_odds": cur["home_ml"],
343
+ "current_away_odds": cur["away_ml"],
344
+ "current_line": None,
345
+ },
346
+ "spread": {
347
+ "opening_home_odds": opn["home_spread_odds"],
348
+ "opening_away_odds": opn["away_spread_odds"],
349
+ "opening_line": opn["spread"],
350
+ "current_home_odds": cur["home_spread_odds"],
351
+ "current_away_odds": cur["away_spread_odds"],
352
+ "current_line": cur["spread"],
353
+ },
354
+ # totals: home = over, away = under
355
+ "total": {
356
+ "opening_home_odds": opn["over_odds"],
357
+ "opening_away_odds": opn["under_odds"],
358
+ "opening_line": opn["total"],
359
+ "current_home_odds": cur["over_odds"],
360
+ "current_away_odds": cur["under_odds"],
361
+ "current_line": cur["total"],
362
+ },
363
+ }
364
+ for odds_type in odds_types:
365
+ fields = market_fields.get(odds_type)
366
+ if fields is None:
367
+ print(f"Unknown odds_type '{odds_type}', skipping")
368
+ continue
369
+ if all(v is None for v in fields.values()):
370
+ continue
371
+ rows.append({**base, "odds_type": odds_type, **fields})
372
+
373
+ result = pd.DataFrame(rows, columns=ESPN_ODDS_COLUMNS)
374
+ print(f"Built {len(result)} espn_odds rows.")
375
+ return result
376
+
377
+
378
+ # ---------------------------------------------------------------------------
379
+ # Line movement for the pipeline (<=2023 only)
380
+ # ---------------------------------------------------------------------------
381
+
382
+ def _fetch_history_pipeline(espn_game_id, mlb_game_id=None,
383
+ markets=("moneyline", "spread", "total")):
384
+ rows = []
385
+ for market in markets:
386
+ bet_type = BET_TYPES.get(market)
387
+ if bet_type is None:
388
+ continue
389
+ url = (
390
+ f"{CORE_BASE}/events/{espn_game_id}/competitions/{espn_game_id}"
391
+ f"/odds/{HISTORY_PROVIDER_ID}/history/{bet_type}/movement"
392
+ )
393
+ data = _get_json(url, params={"limit": 1000})
394
+ if not data:
395
+ continue
396
+ for item in data.get("items", []):
397
+ rows.append(
398
+ {
399
+ "game_id": mlb_game_id,
400
+ "espn_game_id": str(espn_game_id),
401
+ "market": market,
402
+ "timestamp": item.get("lineDate"),
403
+ "away_odds": _to_num(item.get("awayOdds")),
404
+ "home_odds": _to_num(item.get("homeOdds")),
405
+ "over_odds": _to_num(item.get("overOdds")),
406
+ "under_odds": _to_num(item.get("underOdds")),
407
+ "line": item.get("line"),
408
+ }
409
+ )
410
+ df = pd.DataFrame(rows)
411
+ if not df.empty:
412
+ df = df.sort_values(["market", "timestamp"]).reset_index(drop=True)
413
+ return df
414
+
415
+
416
+ def scrape_line_movement(
417
+ start_date: str,
418
+ end_date: str,
419
+ engine=None,
420
+ max_workers: int = 8,
421
+ schedule_table: str = "cleaned_schedule",
422
+ ) -> pd.DataFrame:
423
+ """Timestamped line movement for every game in the range (<=2023 only).
424
+
425
+ Upsert with keys ['espn_game_id', 'market', 'timestamp'].
426
+ """
427
+ dates = _daterange(start_date, end_date)
428
+ games = []
429
+ with ThreadPoolExecutor(max_workers=max_workers) as pool:
430
+ for result in pool.map(_get_games_pipeline, dates):
431
+ games.extend(result)
432
+ if not games:
433
+ return pd.DataFrame()
434
+ games_df = add_game_ids(pd.DataFrame(games), engine,
435
+ schedule_table=schedule_table)
436
+
437
+ frames = []
438
+ pairs = list(zip(games_df["espn_game_id"], games_df["game_id"]))
439
+ print(f"Fetching line movement for {len(pairs)} games...")
440
+ with ThreadPoolExecutor(max_workers=max_workers) as pool:
441
+ futures = {
442
+ pool.submit(_fetch_history_pipeline, egid, gid): egid
443
+ for egid, gid in pairs
444
+ }
445
+ for future in as_completed(futures):
446
+ df = future.result()
447
+ if not df.empty:
448
+ frames.append(df)
449
+ movement = (
450
+ pd.concat(frames, ignore_index=True) if frames else pd.DataFrame()
451
+ )
452
+ print(f"Fetched {len(movement)} line-movement rows.")
453
+ return movement
@@ -0,0 +1,664 @@
1
+ """ESPN MLB odds scraper.
2
+
3
+ Pulls MLB odds (moneyline, run line, total) for every sportsbook ESPN carries,
4
+ plus timestamped line-movement history, from ESPN's public (undocumented) APIs.
5
+
6
+ Endpoints used
7
+ --------------
8
+ Scoreboard (schedule + scores, 1 request per day):
9
+ https://site.api.espn.com/apis/site/v2/sports/baseball/mlb/scoreboard?dates=YYYYMMDD
10
+
11
+ Odds (all books for one game, 1 request per game):
12
+ https://sports.core.api.espn.com/v2/sports/baseball/leagues/mlb/
13
+ events/{id}/competitions/{id}/odds
14
+
15
+ Line movement (timestamped, consensus feed, 1 request per market per game):
16
+ .../odds/{provider_id}/history/{bet_type}/movement
17
+
18
+ Notes
19
+ -----
20
+ - Game datetimes are exact UTC timestamps.
21
+ - For completed games the per-book "current" block is frozen at game time,
22
+ i.e. it is effectively the closing line.
23
+ - Full ML/spread/total coverage is solid from ~2015 onward. Older seasons
24
+ exist but are degraded (moneylines often 0).
25
+ """
26
+
27
+ import datetime
28
+ import threading
29
+ import time
30
+ from concurrent.futures import ThreadPoolExecutor, as_completed
31
+
32
+ import pandas as pd
33
+ import requests
34
+
35
+ SCOREBOARD_URL = (
36
+ "https://site.api.espn.com/apis/site/v2/sports/baseball/mlb/scoreboard"
37
+ )
38
+ CORE_BASE = "https://sports.core.api.espn.com/v2/sports/baseball/leagues/mlb"
39
+
40
+ # Provider that carries timestamped line-movement history
41
+ HISTORY_PROVIDER_ID = "1002" # "teamrankings" (ESPN's consensus movement feed)
42
+
43
+ # bet_type ids for the history endpoint
44
+ BET_TYPES = {"moneyline": 0, "spread": 1, "total": 2}
45
+
46
+ _thread_local = threading.local()
47
+
48
+ _HEADERS = {
49
+ "User-Agent": (
50
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
51
+ "(KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"
52
+ ),
53
+ "Accept": "application/json",
54
+ }
55
+
56
+
57
+ def _session() -> requests.Session:
58
+ """One requests.Session per thread (sessions are not thread-safe)."""
59
+ if not hasattr(_thread_local, "session"):
60
+ s = requests.Session()
61
+ s.headers.update(_HEADERS)
62
+ _thread_local.session = s
63
+ return _thread_local.session
64
+
65
+
66
+ def _get_json(url, params=None, retries=3, timeout=15):
67
+ last_err = None
68
+ for attempt in range(retries):
69
+ try:
70
+ resp = _session().get(url, params=params, timeout=timeout)
71
+ if resp.status_code == 200:
72
+ return resp.json()
73
+ last_err = f"HTTP {resp.status_code}"
74
+ except Exception as e: # noqa: BLE001
75
+ last_err = e
76
+ time.sleep(1.5 * (attempt + 1))
77
+ print(f"Failed GET {url}: {last_err}")
78
+ return None
79
+
80
+
81
+ def _to_num(value):
82
+ """Parse ESPN american-odds strings like '+165', '-110', 'EVEN'."""
83
+ if value is None:
84
+ return None
85
+ if isinstance(value, (int, float)):
86
+ return value if value != 0 else None
87
+ s = str(value).strip().replace("+", "")
88
+ if s.upper() in ("EVEN", "EV"):
89
+ return 100.0
90
+ try:
91
+ num = float(s)
92
+ except ValueError:
93
+ return None
94
+ # 0 is never a real price/line on ESPN; it means "missing"
95
+ return num if num != 0 else None
96
+
97
+
98
+ def _date_str(d) -> str:
99
+ """Normalize date-ish input to YYYYMMDD."""
100
+ if isinstance(d, str):
101
+ d = pd.to_datetime(d)
102
+ if isinstance(d, (datetime.datetime, pd.Timestamp)):
103
+ d = d.date()
104
+ return d.strftime("%Y%m%d")
105
+
106
+
107
+ def _daterange(start_date, end_date):
108
+ start = pd.to_datetime(start_date).date()
109
+ end = pd.to_datetime(end_date).date()
110
+ days = (end - start).days
111
+ return [start + datetime.timedelta(days=i) for i in range(days + 1)]
112
+
113
+
114
+ # ---------------------------------------------------------------------------
115
+ # Schedule / scores
116
+ # ---------------------------------------------------------------------------
117
+
118
+ def get_games(date):
119
+ """Return the list of MLB games for one date (schedule, teams, scores).
120
+
121
+ Args:
122
+ date: date/datetime/'YYYY-MM-DD'
123
+
124
+ Returns:
125
+ list[dict]: one dict per game with keys:
126
+ game_id, game_datetime (UTC ISO string), status, completed,
127
+ home_team_abbr, home_team_name, home_team_espn_id, home_score,
128
+ away_team_abbr, away_team_name, away_team_espn_id, away_score
129
+ """
130
+ data = _get_json(SCOREBOARD_URL, params={"dates": _date_str(date)})
131
+ if not data:
132
+ return []
133
+
134
+ games = []
135
+ for event in data.get("events", []):
136
+ try:
137
+ comp = event["competitions"][0]
138
+ status = comp.get("status", {}).get("type", {})
139
+ row = {
140
+ "game_id": event["id"],
141
+ "game_datetime": event.get("date"),
142
+ "status": status.get("name", ""),
143
+ "completed": bool(status.get("completed", False)),
144
+ }
145
+ for side in comp.get("competitors", []):
146
+ prefix = "home" if side.get("homeAway") == "home" else "away"
147
+ team = side.get("team", {})
148
+ row[f"{prefix}_team_abbr"] = team.get("abbreviation", "")
149
+ row[f"{prefix}_team_name"] = team.get("displayName", "")
150
+ row[f"{prefix}_team_espn_id"] = team.get("id", "")
151
+ score = side.get("score", "")
152
+ row[f"{prefix}_score"] = (
153
+ pd.to_numeric(score, errors="coerce") if score != "" else None
154
+ )
155
+ games.append(row)
156
+ except Exception as e: # noqa: BLE001
157
+ print(f"Error parsing event on {date}: {e}")
158
+ return games
159
+
160
+
161
+ # ---------------------------------------------------------------------------
162
+ # Odds
163
+ # ---------------------------------------------------------------------------
164
+
165
+ def _pick(d, *keys):
166
+ """Nested dict get: _pick(x, 'a', 'b') -> x['a']['b'] or None."""
167
+ for k in keys:
168
+ if not isinstance(d, dict):
169
+ return None
170
+ d = d.get(k)
171
+ return d
172
+
173
+
174
+ def _parse_side(side_odds):
175
+ """Parse home/awayTeamOdds block -> (moneyline, spread_line, spread_odds)."""
176
+ if not isinstance(side_odds, dict):
177
+ return None, None, None
178
+ ml = _to_num(side_odds.get("moneyLine"))
179
+ # Prefer the "current" block (frozen at game time = closing for past games)
180
+ cur = side_odds.get("current") or {}
181
+ ml_cur = _to_num(_pick(cur, "moneyLine", "american"))
182
+ if ml_cur is not None:
183
+ ml = ml_cur
184
+ spread_line = _to_num(_pick(cur, "pointSpread", "american"))
185
+ spread_odds = _to_num(_pick(cur, "spread", "american"))
186
+ if spread_odds is None:
187
+ spread_odds = _to_num(side_odds.get("spreadOdds"))
188
+ return ml, spread_line, spread_odds
189
+
190
+
191
+ def _parse_phase(item, phase):
192
+ """Extract one snapshot ('open' or 'close') from an odds item.
193
+
194
+ ESPN attaches real open/close blocks per book from ~2024 onward
195
+ (ESPN BET / DraftKings era). Older seasons carry junk here ("+0"),
196
+ which _to_num maps to None.
197
+
198
+ Returns a dict of {phase}_home_ml, {phase}_away_ml, {phase}_spread,
199
+ {phase}_home_spread_odds, {phase}_away_spread_odds, {phase}_total,
200
+ {phase}_over_odds, {phase}_under_odds.
201
+ """
202
+ home = _pick(item, "homeTeamOdds", phase) or {}
203
+ away = _pick(item, "awayTeamOdds", phase) or {}
204
+ totals = item.get(phase) or {}
205
+ return {
206
+ f"{phase}_home_ml": _to_num(_pick(home, "moneyLine", "american")),
207
+ f"{phase}_away_ml": _to_num(_pick(away, "moneyLine", "american")),
208
+ f"{phase}_spread": _to_num(_pick(home, "pointSpread", "american")),
209
+ f"{phase}_home_spread_odds": _to_num(_pick(home, "spread", "american")),
210
+ f"{phase}_away_spread_odds": _to_num(_pick(away, "spread", "american")),
211
+ f"{phase}_total": _to_num(_pick(totals, "total", "american")),
212
+ f"{phase}_over_odds": _to_num(_pick(totals, "over", "american")),
213
+ f"{phase}_under_odds": _to_num(_pick(totals, "under", "american")),
214
+ }
215
+
216
+
217
+ def get_odds(game_id):
218
+ """Return odds rows (one per sportsbook) for a single game.
219
+
220
+ Args:
221
+ game_id: ESPN event id (str or int)
222
+
223
+ Returns:
224
+ list[dict]: keys: game_id, provider_id, provider,
225
+ home_ml, away_ml, spread, home_spread_odds, away_spread_odds,
226
+ total, over_odds, under_odds, moneyline_winner, spread_winner
227
+ """
228
+ url = f"{CORE_BASE}/events/{game_id}/competitions/{game_id}/odds"
229
+ data = _get_json(url)
230
+ if not data:
231
+ return []
232
+
233
+ rows = []
234
+ for item in data.get("items", []):
235
+ try:
236
+ provider = item.get("provider", {})
237
+ home = item.get("homeTeamOdds", {})
238
+ away = item.get("awayTeamOdds", {})
239
+
240
+ home_ml, home_spread_line, home_spread_odds = _parse_side(home)
241
+ away_ml, _away_spread_line, away_spread_odds = _parse_side(away)
242
+
243
+ # Spread relative to the home team
244
+ spread = home_spread_line
245
+ if spread is None:
246
+ raw = _to_num(item.get("spread"))
247
+ if raw is not None and abs(raw) <= 30:
248
+ # item['spread'] is the favorite's line magnitude
249
+ spread = -raw if home.get("favorite") else raw
250
+
251
+ total = _to_num(item.get("overUnder"))
252
+ over_odds = _to_num(item.get("overOdds"))
253
+ under_odds = _to_num(item.get("underOdds"))
254
+ cur = item.get("current") or {}
255
+ over_cur = _to_num(_pick(cur, "over", "american"))
256
+ under_cur = _to_num(_pick(cur, "under", "american"))
257
+ total_cur = _to_num(_pick(cur, "total", "american"))
258
+ if over_cur is not None:
259
+ over_odds = over_cur
260
+ if under_cur is not None:
261
+ under_odds = under_cur
262
+ if total_cur is not None:
263
+ total = total_cur
264
+
265
+ row = {
266
+ "game_id": str(game_id),
267
+ "provider_id": provider.get("id", ""),
268
+ "provider": provider.get("name", ""),
269
+ "home_ml": home_ml,
270
+ "away_ml": away_ml,
271
+ "spread": spread,
272
+ "home_spread_odds": home_spread_odds,
273
+ "away_spread_odds": away_spread_odds,
274
+ "total": total,
275
+ "over_odds": over_odds,
276
+ "under_odds": under_odds,
277
+ "moneyline_winner": item.get("moneylineWinner"),
278
+ "spread_winner": item.get("spreadWinner"),
279
+ }
280
+ # Per-book open/close snapshots (real data from ~2024 onward)
281
+ row.update(_parse_phase(item, "open"))
282
+ row.update(_parse_phase(item, "close"))
283
+ rows.append(row)
284
+ except Exception as e: # noqa: BLE001
285
+ print(f"Error parsing odds for game {game_id}: {e}")
286
+ return rows
287
+
288
+
289
+ # ---------------------------------------------------------------------------
290
+ # Line movement (timestamped history)
291
+ # ---------------------------------------------------------------------------
292
+
293
+ def fetch_line_history(game_id, markets=("moneyline", "spread", "total"),
294
+ provider_id=HISTORY_PROVIDER_ID):
295
+ """Fetch full timestamped line movement for one game.
296
+
297
+ ESPN keeps movement history on its consensus feed (provider 1002,
298
+ "teamrankings"). One request per market.
299
+
300
+ NOTE: ESPN dropped this feed after the 2023 season. For 2024+ use the
301
+ per-book open_*/close_* columns returned by scrape_espn_mlb instead.
302
+
303
+ Args:
304
+ game_id: ESPN event id
305
+ markets: iterable of 'moneyline' / 'spread' / 'total'
306
+ provider_id: odds provider id carrying history (default consensus)
307
+
308
+ Returns:
309
+ pd.DataFrame: columns game_id, market, line_date (UTC),
310
+ away_odds, home_odds, over_odds, under_odds, line.
311
+ Sorted by line_date; the first row per market is the opener,
312
+ the last is the closer. Empty DataFrame if no history exists.
313
+ """
314
+ rows = []
315
+ for market in markets:
316
+ bet_type = BET_TYPES.get(market)
317
+ if bet_type is None:
318
+ print(f"Unknown market '{market}', skipping")
319
+ continue
320
+ url = (
321
+ f"{CORE_BASE}/events/{game_id}/competitions/{game_id}"
322
+ f"/odds/{provider_id}/history/{bet_type}/movement"
323
+ )
324
+ data = _get_json(url, params={"limit": 1000})
325
+ if not data:
326
+ continue
327
+ for item in data.get("items", []):
328
+ rows.append(
329
+ {
330
+ "game_id": str(game_id),
331
+ "market": market,
332
+ "line_date": item.get("lineDate"),
333
+ "away_odds": _to_num(item.get("awayOdds")),
334
+ "home_odds": _to_num(item.get("homeOdds")),
335
+ "over_odds": _to_num(item.get("overOdds")),
336
+ "under_odds": _to_num(item.get("underOdds")),
337
+ "line": item.get("line"),
338
+ }
339
+ )
340
+ df = pd.DataFrame(rows)
341
+ if not df.empty:
342
+ df["line_date"] = pd.to_datetime(df["line_date"], utc=True)
343
+ df = df.sort_values(["market", "line_date"]).reset_index(drop=True)
344
+ return df
345
+
346
+
347
+ def get_open_close(movement_df):
348
+ """Collapse a movement DataFrame to opening/closing lines.
349
+
350
+ Args:
351
+ movement_df: output of fetch_line_history / scrape_espn_mlb(...)[1]
352
+
353
+ Returns:
354
+ pd.DataFrame: one row per game_id x market with
355
+ open_date, open_away_odds, open_home_odds, open_over_odds,
356
+ open_under_odds, open_line, close_date, close_* equivalents.
357
+ """
358
+ if movement_df is None or movement_df.empty:
359
+ return pd.DataFrame()
360
+
361
+ df = movement_df.sort_values("line_date")
362
+ grouped = df.groupby(["game_id", "market"], as_index=False)
363
+ first = grouped.first().rename(
364
+ columns={
365
+ "line_date": "open_date",
366
+ "away_odds": "open_away_odds",
367
+ "home_odds": "open_home_odds",
368
+ "over_odds": "open_over_odds",
369
+ "under_odds": "open_under_odds",
370
+ "line": "open_line",
371
+ }
372
+ )
373
+ last = grouped.last().rename(
374
+ columns={
375
+ "line_date": "close_date",
376
+ "away_odds": "close_away_odds",
377
+ "home_odds": "close_home_odds",
378
+ "over_odds": "close_over_odds",
379
+ "under_odds": "close_under_odds",
380
+ "line": "close_line",
381
+ }
382
+ )
383
+ return first.merge(last, on=["game_id", "market"])
384
+
385
+
386
+ # ---------------------------------------------------------------------------
387
+ # Main scrape entry points
388
+ # ---------------------------------------------------------------------------
389
+
390
+ def scrape_espn_mlb(start_date, end_date=None, providers=None,
391
+ include_history=False, exclude_live=True, max_workers=8):
392
+ """Scrape MLB odds for a date range from ESPN.
393
+
394
+ Args:
395
+ start_date: first date (inclusive), date/'YYYY-MM-DD'
396
+ end_date: last date (inclusive); defaults to start_date
397
+ providers: optional list of provider names to keep
398
+ (e.g. ['DraftKings', 'FanDuel', 'consensus']); case-insensitive
399
+ substring match. None keeps every book.
400
+ include_history: if True, also fetch timestamped line movement
401
+ (consensus feed, ~3 extra requests per game)
402
+ exclude_live: drop in-game "Live Odds" feeds (default True), since
403
+ live lines pollute closing-line analysis
404
+ max_workers: concurrent request threads
405
+
406
+ Returns:
407
+ if include_history is False:
408
+ pd.DataFrame odds (one row per game x sportsbook)
409
+ else:
410
+ (odds_df, movement_df) tuple
411
+ """
412
+ if end_date is None:
413
+ end_date = start_date
414
+ dates = _daterange(start_date, end_date)
415
+
416
+ # 1) schedule + scores, one request per day (parallel)
417
+ games = []
418
+ with ThreadPoolExecutor(max_workers=max_workers) as pool:
419
+ for result in pool.map(get_games, dates):
420
+ games.extend(result)
421
+ print(f"Found {len(games)} games between {start_date} and {end_date}")
422
+ if not games:
423
+ empty = pd.DataFrame()
424
+ return (empty, empty.copy()) if include_history else empty
425
+
426
+ games_df = pd.DataFrame(games)
427
+ games_df["game_datetime"] = pd.to_datetime(games_df["game_datetime"], utc=True)
428
+
429
+ # 2) odds, one request per game (parallel)
430
+ odds_rows = []
431
+ game_ids = games_df["game_id"].tolist()
432
+ with ThreadPoolExecutor(max_workers=max_workers) as pool:
433
+ futures = {pool.submit(get_odds, gid): gid for gid in game_ids}
434
+ for future in as_completed(futures):
435
+ odds_rows.extend(future.result())
436
+ odds_df = pd.DataFrame(odds_rows)
437
+ print(f"Collected {len(odds_df)} odds rows")
438
+
439
+ if odds_df.empty:
440
+ merged = games_df.copy()
441
+ else:
442
+ if exclude_live:
443
+ odds_df = odds_df[
444
+ ~odds_df["provider"].str.lower().str.contains("live odds", na=False)
445
+ ]
446
+ if providers:
447
+ wanted = [p.lower() for p in providers]
448
+ mask = odds_df["provider"].str.lower().apply(
449
+ lambda name: any(w in name for w in wanted)
450
+ )
451
+ odds_df = odds_df[mask]
452
+ merged = games_df.merge(odds_df, on="game_id", how="left")
453
+
454
+ col_order = [
455
+ "game_id", "game_datetime", "status", "completed",
456
+ "home_team_abbr", "away_team_abbr",
457
+ "home_team_name", "away_team_name",
458
+ "home_team_espn_id", "away_team_espn_id",
459
+ "home_score", "away_score",
460
+ "provider_id", "provider",
461
+ "home_ml", "away_ml",
462
+ "spread", "home_spread_odds", "away_spread_odds",
463
+ "total", "over_odds", "under_odds",
464
+ "open_home_ml", "open_away_ml", "open_spread",
465
+ "open_home_spread_odds", "open_away_spread_odds",
466
+ "open_total", "open_over_odds", "open_under_odds",
467
+ "close_home_ml", "close_away_ml", "close_spread",
468
+ "close_home_spread_odds", "close_away_spread_odds",
469
+ "close_total", "close_over_odds", "close_under_odds",
470
+ "moneyline_winner", "spread_winner",
471
+ ]
472
+ merged = merged[[c for c in col_order if c in merged.columns]]
473
+ merged = merged.sort_values(["game_datetime", "game_id"]).reset_index(drop=True)
474
+
475
+ if not include_history:
476
+ return merged
477
+
478
+ # 3) optional line movement (parallel, 3 requests per game)
479
+ movement_frames = []
480
+ with ThreadPoolExecutor(max_workers=max_workers) as pool:
481
+ futures = {pool.submit(fetch_line_history, gid): gid for gid in game_ids}
482
+ for future in as_completed(futures):
483
+ df = future.result()
484
+ if not df.empty:
485
+ movement_frames.append(df)
486
+ movement_df = (
487
+ pd.concat(movement_frames, ignore_index=True)
488
+ if movement_frames else pd.DataFrame()
489
+ )
490
+ print(f"Collected {len(movement_df)} line-movement rows")
491
+ return merged, movement_df
492
+
493
+
494
+ def scrape_espn_mlb_years(start_year=2015, end_year=None, providers=None,
495
+ include_history=False, exclude_live=True,
496
+ max_workers=8):
497
+ """Scrape MLB odds season by season (Mar 1 through Nov 30 of each year).
498
+
499
+ Args:
500
+ start_year: first season (inclusive)
501
+ end_year: last season (exclusive, mirroring the old package);
502
+ defaults to current year + 1 (i.e. through the current season)
503
+ providers, include_history, max_workers: see scrape_espn_mlb
504
+
505
+ Returns:
506
+ pd.DataFrame, or (odds_df, movement_df) when include_history=True
507
+ """
508
+ if end_year is None:
509
+ end_year = datetime.date.today().year + 1
510
+ today = datetime.date.today()
511
+
512
+ odds_frames, movement_frames = [], []
513
+ for year in range(start_year, end_year):
514
+ start = datetime.date(year, 3, 1)
515
+ end = min(datetime.date(year, 11, 30), today)
516
+ if start > today:
517
+ break
518
+ print(f"\nScraping season {year} ({start} to {end})...")
519
+ result = scrape_espn_mlb(
520
+ start, end, providers=providers,
521
+ include_history=include_history, exclude_live=exclude_live,
522
+ max_workers=max_workers,
523
+ )
524
+ if include_history:
525
+ odds, movement = result
526
+ if not movement.empty:
527
+ movement_frames.append(movement)
528
+ else:
529
+ odds = result
530
+ if not odds.empty:
531
+ odds_frames.append(odds)
532
+
533
+ odds_df = (
534
+ pd.concat(odds_frames, ignore_index=True) if odds_frames else pd.DataFrame()
535
+ )
536
+ if include_history:
537
+ movement_df = (
538
+ pd.concat(movement_frames, ignore_index=True)
539
+ if movement_frames else pd.DataFrame()
540
+ )
541
+ return odds_df, movement_df
542
+ return odds_df
543
+
544
+
545
+ # ---------------------------------------------------------------------------
546
+ # Legacy compatibility with mlb_odds_scraper
547
+ # ---------------------------------------------------------------------------
548
+
549
+ def to_legacy_format(odds_df, provider="consensus"):
550
+ """Collapse the long odds DataFrame to the old mlb_odds_scraper schema.
551
+
552
+ Output columns: game_date, game_datetime, home_team, away_team (MLB
553
+ statsapi team ids), home_odds, away_odds (moneylines), home_score,
554
+ away_score, home_team_abbr, away_team_abbr.
555
+
556
+ Args:
557
+ odds_df: output of scrape_espn_mlb / scrape_espn_mlb_years
558
+ provider: which book's moneyline to use (case-insensitive substring;
559
+ falls back to the row with the most complete ML if not found)
560
+
561
+ Returns:
562
+ pd.DataFrame in the legacy schema (game_datetime tz-naive US/Eastern,
563
+ matching what process_game_data used to produce).
564
+ """
565
+ import statsapi
566
+
567
+ if odds_df is None or odds_df.empty:
568
+ return pd.DataFrame()
569
+
570
+ df = odds_df.copy()
571
+ df = df.dropna(subset=["home_ml", "away_ml"], how="all")
572
+
573
+ # Prefer the requested provider, else any book with a complete moneyline
574
+ df["_preferred"] = df["provider"].str.lower().str.contains(
575
+ provider.lower(), na=False
576
+ )
577
+ df["_complete"] = df["home_ml"].notna() & df["away_ml"].notna()
578
+ df = df.sort_values(
579
+ ["game_id", "_preferred", "_complete"], ascending=[True, False, False]
580
+ )
581
+ df = df.drop_duplicates("game_id", keep="first")
582
+
583
+ # Map team names to statsapi ids (like the old process_game_data)
584
+ names = pd.unique(
585
+ pd.concat([df["home_team_name"], df["away_team_name"]]).dropna()
586
+ )
587
+ mapping = {}
588
+ for name in names:
589
+ mapping[name] = None
590
+ # Historical names may no longer match (e.g. "Oakland Athletics"
591
+ # after the Athletics rename) — fall back to shorter forms.
592
+ candidates = [name, name.split()[-1], " ".join(name.split()[:-1])]
593
+ for candidate in candidates:
594
+ if not candidate:
595
+ continue
596
+ try:
597
+ info = statsapi.lookup_team(candidate)
598
+ if info:
599
+ mapping[name] = info[0]["id"]
600
+ break
601
+ except Exception as e: # noqa: BLE001
602
+ print(f"Error looking up team {candidate}: {e}")
603
+ if mapping[name] is None:
604
+ print(f"Could not find statsapi id for team: {name}")
605
+
606
+ eastern = df["game_datetime"].dt.tz_convert("US/Eastern").dt.tz_localize(None)
607
+ out = pd.DataFrame(
608
+ {
609
+ "game_date": eastern.dt.normalize(),
610
+ "game_datetime": eastern,
611
+ "home_team": df["home_team_name"].map(mapping),
612
+ "away_team": df["away_team_name"].map(mapping),
613
+ "home_odds": df["home_ml"],
614
+ "away_odds": df["away_ml"],
615
+ "home_score": df["home_score"],
616
+ "away_score": df["away_score"],
617
+ "home_team_abbr": df["home_team_abbr"],
618
+ "away_team_abbr": df["away_team_abbr"],
619
+ }
620
+ )
621
+ return out.reset_index(drop=True)
622
+
623
+
624
+ def clean_game_data(cleaned_schedule, processed_odds, tz="US/Eastern",
625
+ tolerance_hrs=12):
626
+ """Merge a statsapi schedule with odds (same behavior as the old package)."""
627
+ import numpy as np
628
+
629
+ schedule = cleaned_schedule.copy()
630
+ odds = processed_odds.replace("", np.nan).copy()
631
+
632
+ schedule.rename(
633
+ columns={"home_team_score": "home_score", "away_team_score": "away_score"},
634
+ inplace=True,
635
+ )
636
+ schedule["game_datetime"] = (
637
+ pd.to_datetime(schedule["game_datetime"], utc=True)
638
+ .dt.tz_convert(tz)
639
+ .dt.tz_localize(None)
640
+ .astype("datetime64[us]")
641
+ )
642
+ schedule["home_score"] = schedule["home_score"].astype(int, errors="ignore")
643
+ schedule["away_score"] = schedule["away_score"].astype(int, errors="ignore")
644
+ odds["home_score"] = odds["home_score"].astype(int, errors="ignore")
645
+ odds["away_score"] = odds["away_score"].astype(int, errors="ignore")
646
+ schedule = schedule.sort_values("game_datetime")
647
+
648
+ odds["game_datetime"] = (
649
+ pd.to_datetime(odds["game_datetime"]).astype("datetime64[us]")
650
+ )
651
+ odds = odds.sort_values("game_datetime")
652
+
653
+ merged = pd.merge_asof(
654
+ schedule.drop(["game_date", "home_score", "away_score"], axis=1),
655
+ odds,
656
+ on="game_datetime",
657
+ by=["home_team", "away_team"],
658
+ direction="nearest",
659
+ tolerance=pd.Timedelta(f"{tolerance_hrs}hr"),
660
+ )
661
+ merged.dropna(subset=["home_odds", "away_odds"], inplace=True)
662
+ merged["game_datetime"] = merged["game_datetime"].dt.tz_localize(tz)
663
+ return merged
664
+
@@ -0,0 +1,93 @@
1
+ Metadata-Version: 2.4
2
+ Name: espn_odds_scraper
3
+ Version: 0.2.0
4
+ Summary: A package for scraping historical and live MLB odds from ESPN's public API
5
+ Author-email: Oronto <vile319@gmail.com>
6
+ Project-URL: Homepage, https://github.com/Oronto/espn_odds_scraper
7
+ Project-URL: Bug Tracker, https://github.com/Oronto/espn_odds_scraper/issues
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.8
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Requires-Dist: pandas>=1.0.0
15
+ Requires-Dist: requests>=2.0.0
16
+ Requires-Dist: MLB-StatsAPI>=1.0.0
17
+ Dynamic: license-file
18
+
19
+ # espn_odds_scraper
20
+
21
+ Scrape historical and live MLB odds from ESPN's public (undocumented) API. No API key, no Selenium — plain JSON requests.
22
+
23
+ Successor to `mlb_odds_scraper` (OddsPortal/Selenium based). ESPN carries per-book odds (DraftKings, FanDuel, Caesars, MGM, Bet365, ...; older seasons carry BOVADA, 5Dimes, etc.), moneyline + run line + total in a single request per game, and timestamped line-movement history on its consensus feed.
24
+
25
+ **Coverage:** solid from ~2015 onward. Earlier seasons exist but are degraded (moneylines often missing).
26
+
27
+ ## Install
28
+
29
+ ```bash
30
+ pip install espn_odds_scraper
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ ```python
36
+ from espn_odds_scraper import scrape_espn_mlb, scrape_espn_mlb_years
37
+
38
+ # One day, every book — one row per game x sportsbook
39
+ df = scrape_espn_mlb("2023-07-14")
40
+
41
+ # Date range, only certain books
42
+ df = scrape_espn_mlb("2023-07-01", "2023-07-31", providers=["DraftKings", "FanDuel"])
43
+
44
+ # With full timestamped line movement (opt-in, ~3 extra requests per game)
45
+ odds, movement = scrape_espn_mlb("2023-07-14", include_history=True)
46
+
47
+ # Whole seasons
48
+ df = scrape_espn_mlb_years(2018, 2024)
49
+ ```
50
+
51
+ ### Odds DataFrame columns
52
+
53
+ `game_id, game_datetime (UTC), status, completed, home_team_abbr, away_team_abbr, home_team_name, away_team_name, home_team_espn_id, away_team_espn_id, home_score, away_score, provider_id, provider, home_ml, away_ml, spread (home line), home_spread_odds, away_spread_odds, total, over_odds, under_odds, open_* (8 cols), close_* (8 cols), moneyline_winner, spread_winner`
54
+
55
+ For completed games, per-book odds are the lines frozen at game time — effectively **closing lines**. In-game "Live Odds" feeds are dropped by default (`exclude_live=False` to keep them).
56
+
57
+ ### Opening / closing lines & line movement
58
+
59
+ ESPN's data comes in two eras:
60
+
61
+ - **2024+ (ESPN BET / DraftKings era):** each book carries real `open_*` and `close_*` snapshots (moneyline, spread + prices, total + prices) directly in the odds row — no extra requests. Fewer books, though (often just one).
62
+ - **≤2023 (multi-book era):** `open_*`/`close_*` are empty (ESPN stored junk there), but full **timestamped line movement** exists on the consensus feed:
63
+
64
+ ```python
65
+ from espn_odds_scraper import fetch_line_history, get_open_close
66
+
67
+ movement = fetch_line_history("401472391") # timestamped rows per market
68
+ oc = get_open_close(movement) # one row per market: open_* and close_*
69
+ ```
70
+
71
+ Movement rows: `game_id, market (moneyline/spread/total), line_date (UTC), away_odds, home_odds, over_odds, under_odds, line`. First row per market is the opener, last is the closer. ESPN dropped this feed after the 2023 season.
72
+
73
+ ### Drop-in compatibility with mlb_odds_scraper
74
+
75
+ ```python
76
+ from espn_odds_scraper import to_legacy_format, clean_game_data
77
+
78
+ legacy = to_legacy_format(df) # game_date, game_datetime, home_team/away_team
79
+ # (statsapi ids), home_odds/away_odds, scores, abbrs
80
+ merged = clean_game_data(cleaned_schedule, legacy) # same merge helper as before
81
+ ```
82
+
83
+ ## Request footprint
84
+
85
+ - 1 request per day (schedule + scores)
86
+ - 1 request per game (all books, all markets)
87
+ - optional: 3 requests per game (line movement)
88
+
89
+ A full season is roughly 2,600 requests without history. Requests run through a thread pool (`max_workers=8` by default). Be polite; this is an undocumented API.
90
+
91
+ ## License
92
+
93
+ MIT
@@ -0,0 +1,8 @@
1
+ espn_odds_scraper/__init__.py,sha256=h1ASHGNa1lf-IgZQUjw_zK74bcCBQYvN6Zr_bJrpz8g,635
2
+ espn_odds_scraper/pipeline.py,sha256=_NXg92pDGFdh5pg1x5hZLAYUHevXNjMdIpVvrIw-qXk,16951
3
+ espn_odds_scraper/scraper.py,sha256=bS1lGTbYzb2IR-vEobTBHaFi9g_eWnP4ia2VrEe2dWo,25016
4
+ espn_odds_scraper-0.2.0.dist-info/licenses/LICENSE,sha256=Rf_buMTuHmw3KL4La6oAYIXdxxESqz-fXLSjtqmuaFQ,1083
5
+ espn_odds_scraper-0.2.0.dist-info/METADATA,sha256=h92V2nW1M1BtUIB0pOD2GwkCgx2a1PWaeuIiJHgX9qU,4123
6
+ espn_odds_scraper-0.2.0.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
7
+ espn_odds_scraper-0.2.0.dist-info/top_level.txt,sha256=wldnao5MmqefO_1Q-0nTPWnzZttXJ00W4k8l6HRF6mU,18
8
+ espn_odds_scraper-0.2.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (83.0.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Oronto
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ espn_odds_scraper