An AI project that includes a scraper for NFL data, postgres database, and user interface. It uses a classification algorithm to predict winners.
made some client functions
2 files changed, +121 -17
+74-7nfl_client/nfl_client.py
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | |
| 2 | 2 | import time |
| 3 | +from urllib.parse import urljoin | |
| 3 | 4 | from browsermobproxy import Server |
| 4 | 5 | from selenium import webdriver |
| 5 | 6 | from pathlib import Path |
| @@ -183,12 +184,78 @@ class NFLClient(): | ||
| 183 | 184 | |
| 184 | 185 | return url_json |
| 185 | 186 | |
| 186 | - def request(self, endpoint) -> dict: | |
| 187 | + def request(self, endpoint, params={}) -> dict: | |
| 187 | 188 | """Request an endpoint""" |
| 188 | 189 | self.load_auth_token() |
| 189 | - try: | |
| 190 | - return requests.get(endpoint, headers=self.headers) | |
| 191 | - except Exception as e: | |
| 192 | - return { | |
| 193 | - 'error': e | |
| 194 | - } | |
| 190 | + url = urljoin(self.API_ROOT, endpoint) | |
| 191 | + print(url) | |
| 192 | + return requests.get(url, headers=self.headers, params=params) | |
| 193 | + | |
| 194 | + def get_week(self, season: int, week: int, seasonType='REG', withExternalIds=True) -> requests.Response: | |
| 195 | + """## Get summary of games during a week. | |
| 196 | + ### Args | |
| 197 | + * `season`: Year of the season (i.e. 2023) | |
| 198 | + * `week`: Week of the season (i.e. 3) | |
| 199 | + * `seasonType`: `REG` or `PRE` | |
| 200 | + * `withExternalIds`: boolean | |
| 201 | + """ | |
| 202 | + url = f'/football/v2/games/season/{season}/seasonType/{seasonType}/week/{week}' | |
| 203 | + return self.request(url, { | |
| 204 | + 'withExternalIds': withExternalIds | |
| 205 | + }) | |
| 206 | + | |
| 207 | + def get_game(self, game_id: str, withExternalIds=True) -> requests.Response: | |
| 208 | + """## Get game details. | |
| 209 | + ### Args | |
| 210 | + * `game_id`: Can be found iterating the week. | |
| 211 | + * `withExternalIds`: boolean | |
| 212 | + """ | |
| 213 | + return self.request(f'/football/v2/games/{game_id}', { | |
| 214 | + 'withExternalIds': withExternalIds | |
| 215 | + }) | |
| 216 | + | |
| 217 | + def get_standings(self, week: int, season: int, seasonType='REG', limit=100) -> requests.Response: | |
| 218 | + """## Get team standings. | |
| 219 | + ### Args | |
| 220 | + * `season`: Year of the standings (i.e. 2023) | |
| 221 | + * `week`: Week of the standings (i.e. 3) | |
| 222 | + * `seasonType`: `REG` or `PRE` | |
| 223 | + * `limit`: Max items to get | |
| 224 | + """ | |
| 225 | + return self.request(f'/football/v2/standings', { | |
| 226 | + 'season': season, | |
| 227 | + 'week': week, | |
| 228 | + 'seasonType': seasonType, | |
| 229 | + 'limit': limit | |
| 230 | + }) | |
| 231 | + | |
| 232 | + def get_game_summary(self, game_summary_id) -> requests.Request: | |
| 233 | + """## Get game results. | |
| 234 | + ### Args | |
| 235 | + * `game_summary_id`: Lookup id, can be found in `get_week`. | |
| 236 | + """ | |
| 237 | + return self.request(f'/football/v2/stats/live/game-summaries/{game_summary_id}') | |
| 238 | + | |
| 239 | + def get_game_summaries(self, season: int, week: int, seasonType='REG') -> requests.Response: | |
| 240 | + """## Get game summaries for a week. | |
| 241 | + ## Args | |
| 242 | + * `season`: Season | |
| 243 | + * `week`: Week | |
| 244 | + * `seasonType`: `REG` or `PRE` | |
| 245 | + """ | |
| 246 | + return self.request(f'/football/v2/stats/live/game-summaries', { | |
| 247 | + 'season': season, | |
| 248 | + 'week': week, | |
| 249 | + 'seasonType': seasonType | |
| 250 | + }) | |
| 251 | + | |
| 252 | + def get_teams(self, season: int, limit=100) -> requests.Response: | |
| 253 | + """## Get teams in season. | |
| 254 | + ### Args | |
| 255 | + * `season`: Year | |
| 256 | + * `limit`: Query limit | |
| 257 | + """ | |
| 258 | + return self.request(f'/football/v2/teams/history', { | |
| 259 | + 'season': season, | |
| 260 | + 'limit': limit, | |
| 261 | + }) |
+47-10notes.ipynb
| @@ -234,21 +234,58 @@ | ||
| 234 | 234 | }, |
| 235 | 235 | { |
| 236 | 236 | "cell_type": "code", |
| 237 | - "execution_count": 35, | |
| 237 | + "execution_count": 4, | |
| 238 | 238 | "metadata": {}, |
| 239 | 239 | "outputs": [ |
| 240 | 240 | { |
| 241 | - "data": { | |
| 242 | - "text/plain": [ | |
| 243 | - "<Response [200]>" | |
| 244 | - ] | |
| 245 | - }, | |
| 246 | - "execution_count": 35, | |
| 247 | - "metadata": {}, | |
| 248 | - "output_type": "execute_result" | |
| 241 | + "name": "stdout", | |
| 242 | + "output_type": "stream", | |
| 243 | + "text": [ | |
| 244 | + "https://api.nfl.com/football/v2/teams/history\n", | |
| 245 | + "AFC Pro Bowl Team\n", | |
| 246 | + "Arizona Cardinals\n", | |
| 247 | + "Atlanta Falcons\n", | |
| 248 | + "Baltimore Ravens\n", | |
| 249 | + "Buffalo Bills\n", | |
| 250 | + "Carolina Panthers\n", | |
| 251 | + "Chicago Bears\n", | |
| 252 | + "Cincinnati Bengals\n", | |
| 253 | + "Cleveland Browns\n", | |
| 254 | + "Dallas Cowboys\n", | |
| 255 | + "Denver Broncos\n", | |
| 256 | + "Detroit Lions\n", | |
| 257 | + "Green Bay Packers\n", | |
| 258 | + "Houston Texans\n", | |
| 259 | + "Indianapolis Colts\n", | |
| 260 | + "Jacksonville Jaguars\n", | |
| 261 | + "Kansas City Chiefs\n", | |
| 262 | + "Las Vegas Raiders\n", | |
| 263 | + "Los Angeles Chargers\n", | |
| 264 | + "Los Angeles Rams\n", | |
| 265 | + "Miami Dolphins\n", | |
| 266 | + "Minnesota Vikings\n", | |
| 267 | + "New England Patriots\n", | |
| 268 | + "New Orleans Saints\n", | |
| 269 | + "New York Giants\n", | |
| 270 | + "New York Jets\n", | |
| 271 | + "NFC Pro Bowl Team\n", | |
| 272 | + "Philadelphia Eagles\n", | |
| 273 | + "Pittsburgh Steelers\n", | |
| 274 | + "San Francisco 49ers\n", | |
| 275 | + "Seattle Seahawks\n", | |
| 276 | + "Tampa Bay Buccaneers\n", | |
| 277 | + "Tennessee Titans\n", | |
| 278 | + "Washington Commanders\n" | |
| 279 | + ] | |
| 249 | 280 | } |
| 250 | 281 | ], |
| 251 | - "source": [] | |
| 282 | + "source": [ | |
| 283 | + "from nfl_client import NFLClient\n", | |
| 284 | + "client = NFLClient()\n", | |
| 285 | + "r = client.get_teams(2023)\n", | |
| 286 | + "for team in r.json()['teams']:\n", | |
| 287 | + " print(team['fullName'])" | |
| 288 | + ] | |
| 252 | 289 | }, |
| 253 | 290 | { |
| 254 | 291 | "cell_type": "code", |