File size: 1,951 Bytes
cb5cd5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

from datetime import datetime
from typing import Any

import pandas as pd
import requests

from config.settings import (
    ODDS_API_KEY,
    ODDS_BASE_URL,
    ODDS_FEATURED_MARKETS,
    ODDS_FORMAT,
    ODDS_REGIONS,
    ODDS_SPORT_KEY,
)


def fetch_featured_odds() -> pd.DataFrame:
    if not ODDS_API_KEY:
        return pd.DataFrame()

    url = f"{ODDS_BASE_URL}/sports/{ODDS_SPORT_KEY}/odds"
    params = {
        "apiKey": ODDS_API_KEY,
        "regions": ODDS_REGIONS,
        "markets": ODDS_FEATURED_MARKETS,
        "oddsFormat": ODDS_FORMAT,
    }

    response = requests.get(url, params=params, timeout=30)
    response.raise_for_status()
    payload = response.json()

    rows: list[dict[str, Any]] = []

    for event in payload:
        event_id = event.get("id", "")
        commence_time = event.get("commence_time")
        home_team = event.get("home_team", "")
        away_team = event.get("away_team", "")

        for bookmaker in event.get("bookmakers", []):
            sportsbook = bookmaker.get("title", "")
            for market in bookmaker.get("markets", []):
                market_key = market.get("key", "")
                for outcome in market.get("outcomes", []):
                    rows.append(
                        {
                            "fetched_at": datetime.utcnow(),
                            "event_id": event_id,
                            "commence_time": commence_time,
                            "home_team": home_team,
                            "away_team": away_team,
                            "sportsbook": sportsbook,
                            "market_key": market_key,
                            "outcome_name": outcome.get("name", ""),
                            "price": outcome.get("price"),
                            "point": outcome.get("point"),
                        }
                    )

    return pd.DataFrame(rows)