{ "cells": [ { "cell_type": "markdown", "id": "331a18ba", "metadata": {}, "source": [ "# 🏆 ESPN Fantasy with `sportsdataverse-py`\n", "\n", "ESPN runs five fantasy games on one platform: football (`ffl`), men's basketball (`fba`), baseball (`flb`), hockey (`fhl`) and women's basketball (`wfba`). `sportsdataverse.providers.espn_fantasy` reads the parts of it that are **public**: the games and seasons, the pro-team schedules, the player universe, ESPN's default scoring, player news, and the public leagues ESPN lists in its draft lobby.\n", "\n", "No account and no cookies. A private league answers HTTP 401 and is out of scope." ] }, { "cell_type": "markdown", "id": "3f757728", "metadata": {}, "source": [ "## 🧰 The toolbox\n", "\n", "| Function | What it gives you |\n", "|---|---|\n", "| [`espn_fantasy_games`](../espn_fantasy/reference/espn_fantasy.md#espn_fantasy_games) | Every fantasy game and its current season |\n", "| [`espn_fantasy_players`](../espn_fantasy/reference/espn_fantasy.md#espn_fantasy_players) | The player universe of a season, with ownership |\n", "| [`espn_fantasy_pro_schedule`](../espn_fantasy/reference/espn_fantasy.md#espn_fantasy_pro_schedule) | Pro games by scoring period |\n", "| [`espn_fantasy_default_scoring`](../espn_fantasy/reference/espn_fantasy.md#espn_fantasy_default_scoring) | ESPN's default points per stat |\n", "| [`espn_fantasy_league_directory`](../espn_fantasy/reference/espn_fantasy.md#espn_fantasy_league_directory) | ESPN's public leagues |\n", "| [`espn_fantasy_league_teams`](../espn_fantasy/reference/espn_fantasy.md#espn_fantasy_league_teams) | A public league's standings |\n", "| [`espn_fantasy_league_matchups`](../espn_fantasy/reference/espn_fantasy.md#espn_fantasy_league_matchups) | Its head-to-head schedule |\n", "| [`espn_fantasy_player_pool`](../espn_fantasy/reference/espn_fantasy.md#espn_fantasy_player_pool) | Its free agents and rostered players |\n", "| [`espn_fantasy_player_news`](../espn_fantasy/reference/espn_fantasy.md#espn_fantasy_player_news) | News items for one player |" ] }, { "cell_type": "markdown", "id": "6c0b9a78", "metadata": {}, "source": [ "## 🔌 Setup\n", "\n", "```sh\n", "pip install --pre \"sportsdataverse\"\n", "```\n", "\n", "`--pre` is needed while 0.2.0 is in beta." ] }, { "cell_type": "code", "execution_count": null, "id": "74639c64", "metadata": {}, "outputs": [], "source": [ "import polars as pl\n", "\n", "from sportsdataverse.providers import espn_fantasy as ef\n", "\n", "pl.Config.set_tbl_rows(10)\n", "pl.Config.set_fmt_str_lengths(50)" ] }, { "cell_type": "markdown", "id": "558be408", "metadata": {}, "source": [ "Fantasy routes are seasonal: an off-season game lists no public leagues, and a league that is still drafting has empty rosters. A small `safe()` helper prints a one-line skip instead of stopping the notebook, naming whether the answer was *nothing there* (`NoDataError`) or *the fetch failed* (`AssetFetchError`). 🛟" ] }, { "cell_type": "code", "execution_count": null, "id": "5a05d32b", "metadata": {}, "outputs": [], "source": [ "from sportsdataverse.errors import AssetFetchError, NoDataError\n", "\n", "def safe(label, thunk):\n", " try:\n", " out = thunk()\n", " print(f\"✅ {label}\")\n", " return out\n", " except (NoDataError, AssetFetchError) as e:\n", " print(f\"\\u23ed\\ufe0f {label}: {type(e).__name__}: {e}\")\n", " return None" ] }, { "cell_type": "markdown", "id": "363455ea", "metadata": {}, "source": [ "## 🎮 Games and seasons\n", "\n", "Season ids are ESPN's own: football is named by the year it **starts** (`2026` = fall 2026), basketball and hockey by the year they **end** (2026-27 = `2027`). `espn_fantasy_games` gives each game's current one." ] }, { "cell_type": "code", "execution_count": null, "id": "a7700689", "metadata": {}, "outputs": [], "source": [ "games = ef.espn_fantasy_games()\n", "season = dict(games.select(pl.col(\"game_abbrev\").str.to_lowercase(), \"current_season_id\").iter_rows())\n", "games.select(\"game_abbrev\", \"game_name\", \"pro_sport_abbrev\", \"current_season_id\", \"current_scoring_period_id\")" ] }, { "cell_type": "markdown", "id": "75b88264", "metadata": {}, "source": [ "## 🧑‍🤝‍🧑 The player universe\n", "\n", "`espn_fantasy_players` returns every active player of a season with the share of ESPN leagues that roster them. `player_id` is ESPN's athlete id, the same one ESPN's other APIs use." ] }, { "cell_type": "code", "execution_count": null, "id": "f3f2c02a", "metadata": {}, "outputs": [], "source": [ "players = ef.espn_fantasy_players(\"fba\", season[\"fba\"])\n", "players.sort(\"percent_owned\", descending=True).select(\"player_id\", \"full_name\", \"pro_team_id\", \"percent_owned\").head(10)" ] }, { "cell_type": "markdown", "id": "dc340ed7", "metadata": {}, "source": [ "## 📅 Pro schedule and scoring\n", "\n", "Football's scoring period is the NFL week; for the other games it is a day. The pro schedule says which pro games count toward each period, and ESPN's default template says what each stat is worth." ] }, { "cell_type": "code", "execution_count": null, "id": "0fd2c173", "metadata": {}, "outputs": [], "source": [ "sched = ef.espn_fantasy_pro_schedule(\"ffl\", season[\"ffl\"])\n", "sched.group_by(\"scoring_period_id\").agg(pl.len().alias(\"pro_games\")).sort(\"scoring_period_id\").head(6)" ] }, { "cell_type": "code", "execution_count": null, "id": "6051f45d", "metadata": {}, "outputs": [], "source": [ "scoring = ef.espn_fantasy_default_scoring(\"ffl\", season[\"ffl\"], league_default_id=1)\n", "scoring.select(\"scoring_type\", \"stat_id\", \"points\").sort(\"points\", descending=True).head(8)" ] }, { "cell_type": "markdown", "id": "dbf8c324", "metadata": {}, "source": [ "## 🏟️ A public league\n", "\n", "ESPN's draft lobby lists public leagues anyone can join; every one of them is readable by id. Leagues whose draft is over have rosters and played matchups." ] }, { "cell_type": "code", "execution_count": null, "id": "d21cf7e6", "metadata": {}, "outputs": [], "source": [ "lobby = ef.espn_fantasy_league_directory(\"fba\", season[\"fba\"])\n", "done = lobby.filter(pl.col(\"full\") == True).sort(\"draft_date\")\n", "done.select(\"league_id\", \"league_name\", \"scoring_type\", \"draft_type\", \"draft_date\").head(5)" ] }, { "cell_type": "code", "execution_count": null, "id": "686c4c6b", "metadata": {}, "outputs": [], "source": [ "league_id = done[\"league_id\"][0] if done.height else None\n", "teams = safe(\"league teams\", lambda: ef.espn_fantasy_league_teams(\"fba\", season[\"fba\"], league_id)) if league_id else None\n", "teams.select(\"team_id\", \"wins\", \"losses\", \"points_for\", \"playoff_seed\").sort(\"playoff_seed\") if teams is not None else None" ] }, { "cell_type": "code", "execution_count": null, "id": "c8971cd7", "metadata": {}, "outputs": [], "source": [ "matchups = safe(\"league matchups\", lambda: ef.espn_fantasy_league_matchups(\"fba\", season[\"fba\"], league_id)) if league_id else None\n", "matchups.filter(pl.col(\"winner\") != \"UNDECIDED\").tail(5) if matchups is not None else None" ] }, { "cell_type": "markdown", "id": "e9b9fe84", "metadata": {}, "source": [ "The player pool answers *who is still available?* The `X-Fantasy-Filter` header narrows it: here, the ten most-owned free agents." ] }, { "cell_type": "code", "execution_count": null, "id": "90096424", "metadata": {}, "outputs": [], "source": [ "fa_filter = '{\"players\":{\"limit\":10,\"sortPercOwned\":{\"sortPriority\":1,\"sortAsc\":false},'\n", "fa_filter += '\"filterStatus\":{\"value\":[\"FREEAGENT\",\"WAIVERS\"]}}}'\n", "pool = safe(\"free agents\", lambda: ef.espn_fantasy_player_pool(\"fba\", season[\"fba\"], league_id, fantasy_filter=fa_filter)) if league_id else None\n", "pool.select(\"player_id\", \"full_name\", \"status\", \"percent_owned\") if pool is not None else None" ] }, { "cell_type": "markdown", "id": "2a020095", "metadata": {}, "source": [ "## 📰 Player news" ] }, { "cell_type": "code", "execution_count": null, "id": "f93895c2", "metadata": {}, "outputs": [], "source": [ "top = players.sort(\"percent_owned\", descending=True)[\"player_id\"][0]\n", "news = safe(\"player news\", lambda: ef.espn_fantasy_player_news(\"fba\", top))\n", "news.select(\"published\", \"type\", \"headline\").head(5) if news is not None else None" ] }, { "cell_type": "markdown", "id": "93c314dd", "metadata": {}, "source": [ "## 🧭 Where next\n", "\n", "- Every wrapper takes `return_parsed=False` for the raw body and `return_as_pandas=True` for pandas.\n", "- `team_id` is unique only inside one league; join league tables on `(league_id, team_id)`.\n", "- Private leagues need their owner's `espn_s2` / `SWID` cookies; this package does not take them.\n", "- The full list is on the [reference page](../espn_fantasy/reference/espn_fantasy.md)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }