| import os |
| import requests |
|
|
| import gradio as gr |
|
|
| from gradio_calendar import Calendar |
| from datetime import datetime |
| from typing import Dict, Literal, Union |
|
|
|
|
| def get_matches_by_date(date: float | datetime | str | None) -> Dict[Union[Literal["id"], Literal["error"]], |
| Union[str,Dict[Literal["competition", "home_team", "away_team", "home_score", "away_score"], str]]]: |
|
|
| """ |
| Fetches all matches for a given date from the Football Data API. |
| |
| Args: |
| date (datetime): The date to fetch matches for |
| |
| Returns: |
| dict: A dictionary with the match ID as the key, and a dictionary containing the competition, home team, away team, home score and away score as the value |
| """ |
|
|
| if date is None: |
| date = datetime.now() |
|
|
| date_formatted = date.strftime("%Y-%m-%d") if isinstance(date, datetime) else date |
|
|
| api_key = os.getenv("FOOTBALL_DATA_API_KEY") |
|
|
| uri = f"https://api.football-data.org/v4/matches/?date={date_formatted}" |
|
|
| headers = { 'X-Auth-Token': api_key } |
|
|
| response = requests.get(uri, headers=headers) |
|
|
| matches = response.json()["matches"] |
|
|
| if len(matches) == 0: |
| return {"error": "No matches on this date"} |
|
|
| matches_dict = {} |
|
|
| for match in matches: |
|
|
| competition = match["competition"]["name"] |
|
|
| home_team = match["homeTeam"]["name"] |
| away_team = match["awayTeam"]["name"] |
|
|
| score = match["score"]["fullTime"] |
|
|
| home_score = score["home"] |
| away_score = score["away"] |
|
|
| matches_dict[str(match["id"])] = { |
| "competition": competition, |
| "home_team": home_team, |
| "away_team": away_team, |
| "home_score": home_score, |
| "away_score": away_score |
| } |
|
|
| return matches_dict |
|
|
|
|
| demo = gr.Interface( |
| get_matches_by_date, |
| inputs=[ |
| Calendar( |
| value=datetime.now(), |
| type="datetime", |
| label="Matches on this date", |
| interactive=True |
| ) |
| ], |
| outputs=[ |
| gr.JSON(), |
| ] |
| ) |
|
|
|
|
| if __name__ == "__main__": |
| demo.launch(mcp_server=True) |
|
|