cfbd_json_py.draft
1# Creation Date: 08/30/2023 01:13 EDT 2# Last Updated Date: 08/13/2024 02:10 PM EDT 3# Author: Joseph Armstrong (armstrongjoseph08@gmail.com) 4# File Name: draft.py 5# Purpose: Houses functions pertaining to NFL Draft data within the CFBD API. 6############################################################################### 7 8import logging 9from datetime import datetime 10 11import pandas as pd 12import requests 13 14# from tqdm import tqdm 15from cfbd_json_py.utls import get_cfbd_api_token 16 17 18def get_cfbd_nfl_teams( 19 api_key: str = None, api_key_dir: str = None, return_as_dict: bool = False 20): 21 """ 22 Retrieves a list of NFL teams from the CFBD API. 23 24 Parameters 25 ---------- 26 `api_key` (str, optional): 27 Semi-optional argument. 28 If `api_key` is null, this function will attempt to load a CFBD API key 29 from the python environment, or from a file on this computer. 30 If `api_key` is not null, 31 this function will automatically assume that the 32 inputted `api_key` is a valid CFBD API key. 33 34 `api_key_dir` (str, optional): 35 Optional argument. 36 If `api_key` is set to am empty string, this variable is ignored. 37 If `api_key_dir` is null, and `api_key` is null, 38 this function will try to find 39 a CFBD API key file in this user's home directory. 40 If `api_key_dir` is set to a string, and `api_key` is null, 41 this function will assume that `api_key_dir` is a directory, 42 and will try to find a CFBD API key file in that directory. 43 44 `return_as_dict` (bool, semi-optional): 45 Semi-optional argument. 46 If you want this function to return the data 47 as a dictionary (read: JSON object), 48 instead of a pandas `DataFrame` object, 49 set `return_as_dict` to `True`. 50 51 Usage 52 ---------- 53 ``` 54 import time 55 56 from cfbd_json_py.draft import get_cfbd_nfl_teams 57 58 cfbd_key = "tigersAreAwesome" # placeholder for your CFBD API Key. 59 60 if cfbd_key is not "tigersAreAwesome": 61 print( 62 "Using the user's API key declared in this script " + 63 "for this example." 64 ) 65 66 # Gets NFL team info from the CFBD API. 67 print("Gets NFL team info from the CFBD API.") 68 json_data = get_cfbd_nfl_teams(api_key=cfbd_key) 69 print(json_data) 70 time.sleep(5) 71 72 # You can also tell this function to just return the API call 73 # as a Dictionary (read: JSON) object. 74 print( 75 "You can also tell this function to just return the API call " + 76 "as a Dictionary (read: JSON) object." 77 ) 78 json_data = get_cfbd_nfl_teams( 79 api_key=cfbd_key, 80 return_as_dict=True) 81 print(json_data) 82 83 else: 84 # Alternatively, if the CFBD API key exists in this python environment, 85 # or it's been set by cfbd_json_py.utls.set_cfbd_api_token(), 86 # you could just call these functions directly, 87 # without setting the API key in the script. 88 print( 89 "Using the user's API key supposedly loaded into " + 90 "this python environment for this example." 91 ) 92 93 # Gets NFL team info from the CFBD API. 94 print("Gets NFL team info from the CFBD API.") 95 json_data = get_cfbd_nfl_teams() 96 print(json_data) 97 time.sleep(5) 98 99 # You can also tell this function to just return the API call 100 # as a Dictionary (read: JSON) object. 101 print( 102 "You can also tell this function to just return the API call " + 103 "as a Dictionary (read: JSON) object." 104 ) 105 json_data = get_cfbd_nfl_teams(return_as_dict=True) 106 print(json_data) 107 108 ``` 109 110 Returns 111 ---------- 112 A pandas `DataFrame` object with NFL team data, 113 or (if `return_as_dict` is set to `True`) 114 a dictionary object with NFL team data. 115 116 """ 117 118 nfl_teams_df = pd.DataFrame() 119 # row_df = pd.DataFrame() 120 url = "https://api.collegefootballdata.com/draft/teams" 121 122 # Input validation 123 ########################################################################## 124 125 if api_key is not None: 126 real_api_key = api_key 127 del api_key 128 else: 129 real_api_key = get_cfbd_api_token(api_key_dir=api_key_dir) 130 131 if real_api_key == "tigersAreAwesome": 132 raise ValueError( 133 "You actually need to change `cfbd_key` to your CFBD API key." 134 ) 135 elif "Bearer " in real_api_key: 136 pass 137 elif "Bearer" in real_api_key: 138 real_api_key = real_api_key.replace("Bearer", "Bearer ") 139 else: 140 real_api_key = "Bearer " + real_api_key 141 142 headers = { 143 "Authorization": f"{real_api_key}", 144 "accept": "application/json" 145 } 146 147 response = requests.get(url, headers=headers) 148 149 if response.status_code == 200: 150 pass 151 elif response.status_code == 401: 152 raise ConnectionRefusedError( 153 "Could not connect. The connection was refused.\n" + 154 "HTTP Status Code 401." 155 ) 156 else: 157 raise ConnectionError( 158 f"Could not connect.\nHTTP Status code {response.status_code}" 159 ) 160 161 json_data = response.json() 162 163 if return_as_dict is True: 164 return json_data 165 166 nfl_teams_df = pd.json_normalize(json_data) 167 nfl_teams_df.rename(columns={"displayName": "display_name"}, inplace=True) 168 return nfl_teams_df 169 170 171def get_cfbd_nfl_positions( 172 api_key: str = None, api_key_dir: str = None, return_as_dict: bool = False 173): 174 """ 175 Retrieves a list of player positions for the NFL Draft from the CFBD API. 176 177 Parameters 178 ---------- 179 `api_key` (str, optional): 180 Semi-optional argument. 181 If `api_key` is null, this function will attempt to load a CFBD API key 182 from the python environment, or from a file on this computer. 183 If `api_key` is not null, 184 this function will automatically assume that the 185 inputted `api_key` is a valid CFBD API key. 186 187 `api_key_dir` (str, optional): 188 Optional argument. 189 If `api_key` is set to am empty string, this variable is ignored. 190 If `api_key_dir` is null, and `api_key` is null, 191 this function will try to find 192 a CFBD API key file in this user's home directory. 193 If `api_key_dir` is set to a string, and `api_key` is null, 194 this function will assume that `api_key_dir` is a directory, 195 and will try to find a CFBD API key file in that directory. 196 197 `return_as_dict` (bool, semi-optional): 198 Semi-optional argument. 199 If you want this function to return 200 the data as a dictionary (read: JSON object), 201 instead of a pandas `DataFrame` object, 202 set `return_as_dict` to `True`. 203 204 Usage 205 ---------- 206 ``` 207 import time 208 209 from cfbd_json_py.draft import get_cfbd_nfl_positions 210 211 cfbd_key = "tigersAreAwesome" # placeholder for your CFBD API Key. 212 213 if cfbd_key is not "tigersAreAwesome": 214 print( 215 "Using the user's API key declared in this script " + 216 "for this example." 217 ) 218 219 # Gets a list of player positions for the NFL Draft from the CFBD API. 220 print( 221 "Gets a list of player positions for the NFL Draft " + 222 "from the CFBD API." 223 ) 224 json_data = get_cfbd_nfl_positions(api_key=cfbd_key) 225 print(json_data) 226 time.sleep(5) 227 228 # You can also tell this function to just return the API call 229 # as a Dictionary (read: JSON) object. 230 print( 231 "You can also tell this function to just return the API call " + 232 "as a Dictionary (read: JSON) object." 233 ) 234 json_data = get_cfbd_nfl_positions( 235 api_key=cfbd_key, 236 return_as_dict=True) 237 print(json_data) 238 239 else: 240 # Alternatively, if the CFBD API key exists in this python environment, 241 # or it's been set by cfbd_json_py.utls.set_cfbd_api_token(), 242 # you could just call these functions directly, 243 # without setting the API key in the script. 244 print( 245 "Using the user's API key supposedly loaded into " + 246 "this python environment for this example." 247 ) 248 249 # Gets a list of player positions for the NFL Draft from the CFBD API. 250 print( 251 "Gets a list of player positions for the NFL Draft " + 252 "from the CFBD API." 253 ) 254 json_data = get_cfbd_nfl_positions() 255 print(json_data) 256 time.sleep(5) 257 258 # You can also tell this function to just return the API call 259 # as a Dictionary (read: JSON) object. 260 print( 261 "You can also tell this function to just return the API call " + 262 "as a Dictionary (read: JSON) object." 263 ) 264 json_data = get_cfbd_nfl_positions(return_as_dict=True) 265 print(json_data) 266 267 ``` 268 Returns 269 ---------- 270 A pandas `DataFrame` object with player position data, 271 or (if `return_as_dict` is set to `True`) 272 a dictionary object with player position data. 273 274 """ 275 276 positions_df = pd.DataFrame() 277 # row_df = pd.DataFrame() 278 url = "https://api.collegefootballdata.com/draft/positions" 279 280 # Input validation 281 ########################################################################## 282 283 if api_key is not None: 284 real_api_key = api_key 285 del api_key 286 else: 287 real_api_key = get_cfbd_api_token(api_key_dir=api_key_dir) 288 289 if real_api_key == "tigersAreAwesome": 290 raise ValueError( 291 "You actually need to change `cfbd_key` to your CFBD API key." 292 ) 293 elif "Bearer " in real_api_key: 294 pass 295 elif "Bearer" in real_api_key: 296 real_api_key = real_api_key.replace("Bearer", "Bearer ") 297 else: 298 real_api_key = "Bearer " + real_api_key 299 300 headers = { 301 "Authorization": f"{real_api_key}", 302 "accept": "application/json" 303 } 304 305 response = requests.get(url, headers=headers) 306 307 if response.status_code == 200: 308 pass 309 elif response.status_code == 401: 310 raise ConnectionRefusedError( 311 "Could not connect. The connection was refused.\n" + 312 "HTTP Status Code 401." 313 ) 314 else: 315 raise ConnectionError( 316 f"Could not connect.\nHTTP Status code {response.status_code}" 317 ) 318 319 json_data = response.json() 320 321 if return_as_dict is True: 322 return json_data 323 324 # for p in json_data: 325 # position_name = p['name'] 326 # position_abbreviation = p['abbreviation'] 327 328 # row_df = pd.DataFrame( 329 # { 330 # "position_name": position_name, 331 # "position_abbreviation": position_abbreviation 332 # }, index=[0] 333 # ) 334 # positions_df = pd.concat([positions_df, row_df], ignore_index=True) 335 336 # del position_name, position_abbreviation 337 # del row_df 338 positions_df = pd.json_normalize(json_data) 339 positions_df.rename( 340 columns={ 341 "name": "position_name", 342 "abbreviation": "position_abbreviation", 343 }, 344 inplace=True, 345 ) 346 347 return positions_df 348 349 350def get_cfbd_nfl_draft_info( 351 api_key: str = None, 352 api_key_dir: str = None, 353 season: int = None, 354 nfl_team: str = None, 355 college: str = None, 356 conference: str = None, 357 position: str = None, 358 year: int = None, 359 return_as_dict: bool = False, 360): 361 """ 362 Retrieves a list of actual NFL Draft selections from the CFBD API. 363 364 Parameters 365 ---------- 366 367 `api_key` (str, optional): 368 Semi-optional argument. 369 If `api_key` is null, this function will attempt to load a CFBD API key 370 from the python environment, or from a file on this computer. 371 If `api_key` is not null, 372 this function will automatically assume that the 373 inputted `api_key` is a valid CFBD API key. 374 375 `api_key_dir` (str, optional): 376 Optional argument. 377 If `api_key` is set to am empty string, this variable is ignored. 378 If `api_key_dir` is null, and `api_key` is null, 379 this function will try to find 380 a CFBD API key file in this user's home directory. 381 If `api_key_dir` is set to a string, and `api_key` is null, 382 this function will assume that `api_key_dir` is a directory, 383 and will try to find a CFBD API key file in that directory. 384 385 The following parameters are optional, 386 but it is highly recommended to not call this function 387 without declaring one of these five optional parameters 388 as a non-null value. 389 390 `season` (int, semi-optional): 391 Semi-Optional argument. 392 This is the season you want NFL Draft information for. 393 For example, if you only want data for the 2020 NFL Draft, 394 set `season` to `2020`. 395 396 `nfl_team` (str, optional): 397 Semi-Optional argument. 398 If you only want NFL Draft selections from a specific NFL team, 399 set `nfl_team` to the name of that team. 400 For example, if you want to only get NFL Draft information for 401 draft picks made by the Cincinnati Bengals, 402 set `nfl_team` to `Cincinnati`. 403 404 `college` (str, optional): 405 Semi-Optional argument. 406 If you only want NFL Draft selections from a specific CFB team, 407 set `college` to the name of that team. 408 For example, if you want to only get NFL Draft information for 409 draft picks from the Clemson Tigers Football Program, 410 set `college` to `Clemson`. 411 412 `conference` (str, optional): 413 Semi-Optional argument. 414 If you only want NFL Draft selections from a specific CFB conference, 415 set `conference` to the abbreviation of that conference. 416 A list of CFBD API conference abbreviations can be found 417 in the `conference_abbreviation` column from 418 the pandas DataFrame that is returned by calling 419 `cfbd_json_py.conferences.get_cfbd_conference_info()`. 420 For example, if you want to only get NFL Draft information for 421 draft picks that played in the Big 12, set `conference` to `B12`. 422 423 `year` (int): 424 Alternative keyword for `season` 425 426 `position` (str, optional): 427 Semi-Optional argument. 428 If you only want NFL Draft selections who played a specific position, 429 set `position` to that position's abbreviation. 430 A list of CFBD API positions can be found in the 431 `position_abbreviation` column from 432 the pandas DataFrame that is returned by calling 433 `cfbd_json_py.draft.get_cfbd_nfl_positions()`. 434 435 `return_as_dict` (bool, semi-optional): 436 Semi-optional argument. 437 If you want this function to return the data 438 as a dictionary (read: JSON object), 439 instead of a pandas `DataFrame` object, 440 set `return_as_dict` to `True`. 441 Usage 442 ---------- 443 444 ``` 445 import time 446 447 from cfbd_json_py.draft import get_cfbd_nfl_draft_info 448 449 cfbd_key = "tigersAreAwesome" # placeholder for your CFBD API Key. 450 451 if cfbd_key is not "tigersAreAwesome": 452 print( 453 "Using the user's API key declared in this script " + 454 "for this example." 455 ) 456 457 # Get NFL Draft selections from the 2020 NFL Draft. 458 print("Get NFL Draft selections from the 2020 NFL Draft.") 459 json_data = get_cfbd_nfl_draft_info( 460 api_key=cfbd_key, 461 season=2020 462 ) 463 print(json_data) 464 time.sleep(5) 465 466 # Get NFL Draft selections from the 2020 NFL Draft made by the 467 # 2020 Cincinnati Bengals. 468 print( 469 "Get NFL Draft selections from the 2020 NFL Draft made " + 470 "by the 2020 Cincinnati Bengals." 471 ) 472 json_data = get_cfbd_nfl_draft_info( 473 api_key=cfbd_key, 474 season=2020, 475 nfl_team="Cincinnati" 476 ) 477 print(json_data) 478 time.sleep(5) 479 480 # Get NFL Draft selections from the 2020 NFL Draft made involving 481 # Clemson Tigers football players. 482 print( 483 "Get NFL Draft selections from the 2020 NFL Draft made " + 484 "involving Clemson Tigers football players." 485 ) 486 json_data = get_cfbd_nfl_draft_info( 487 api_key=cfbd_key, 488 season=2020, 489 college="Clemson" 490 ) 491 print(json_data) 492 time.sleep(5) 493 494 # Get NFL Draft selections from the 2020 NFL Draft made involving 495 # players who played in the Southeastern conference (SEC). 496 print( 497 "Get NFL Draft selections from the 2020 NFL Draft made " + 498 "involving players who played " + 499 "in the Southeastern conference (SEC)." 500 ) 501 json_data = get_cfbd_nfl_draft_info( 502 api_key=cfbd_key, 503 season=2020, 504 conference="SEC" 505 ) 506 print(json_data) 507 time.sleep(5) 508 509 # Get NFL Draft selections from the 2020 NFL Draft made 510 # where the selected player was a QB in college. 511 print( 512 "Get NFL Draft selections from the 2020 NFL Draft made " + 513 "where the selected player was a QB in college." 514 ) 515 json_data = get_cfbd_nfl_draft_info( 516 api_key=cfbd_key, 517 season=2020, 518 position="QB" 519 ) 520 print(json_data) 521 time.sleep(5) 522 523 # You can also tell this function to just return the API call 524 # as a Dictionary (read: JSON) object. 525 print( 526 "You can also tell this function to just return the API call " + 527 "as a Dictionary (read: JSON) object." 528 ) 529 json_data = get_cfbd_nfl_draft_info( 530 season=2020, 531 position="QB", 532 api_key=cfbd_key, 533 return_as_dict=True 534 ) 535 print(json_data) 536 537 else: 538 # Alternatively, if the CFBD API key exists in this python environment, 539 # or it's been set by cfbd_json_py.utls.set_cfbd_api_token(), 540 # you could just call these functions directly, 541 # without setting the API key in the script. 542 print( 543 "Using the user's API key supposedly loaded " + 544 "into this python environment for this example." 545 ) 546 547 # Get NFL Draft selections from the 2020 NFL Draft. 548 print("Get NFL Draft selections from the 2020 NFL Draft.") 549 json_data = get_cfbd_nfl_draft_info(season=2020) 550 print(json_data) 551 time.sleep(5) 552 553 # Get NFL Draft selections from the 2020 NFL Draft made by the 554 # 2020 Cincinnati Bengals. 555 print( 556 "Get NFL Draft selections from the 2020 NFL Draft " + 557 "made by the 2020 Cincinnati Bengals." 558 ) 559 json_data = get_cfbd_nfl_draft_info( 560 season=2020, 561 nfl_team="Cincinnati" 562 ) 563 print(json_data) 564 time.sleep(5) 565 566 # Get NFL Draft selections from the 2020 NFL Draft made involving 567 # Clemson Tigers football players. 568 print( 569 "Get NFL Draft selections from the 2020 NFL Draft made " + 570 "involving Clemson Tigers football players." 571 ) 572 json_data = get_cfbd_nfl_draft_info( 573 season=2020, 574 college="Clemson" 575 ) 576 print(json_data) 577 time.sleep(5) 578 579 # Get NFL Draft selections from the 2020 NFL Draft made involving 580 # players who played in the Southeastern conference (SEC). 581 print( 582 "Get NFL Draft selections from the 2020 NFL Draft made " + 583 "involving players who played " + 584 "in the Southeastern conference (SEC)." 585 ) 586 json_data = get_cfbd_nfl_draft_info( 587 season=2020, 588 conference="SEC" 589 ) 590 print(json_data) 591 time.sleep(5) 592 593 # Get NFL Draft selections from the 2020 NFL Draft made 594 # where the selected player was a QB in college. 595 print( 596 "Get NFL Draft selections from the 2020 NFL Draft made " + 597 "where the selected player was a QB in college." 598 ) 599 json_data = get_cfbd_nfl_draft_info( 600 season=2020, 601 position="QB" 602 ) 603 print(json_data) 604 time.sleep(5) 605 606 # You can also tell this function to just return the API call 607 # as a Dictionary (read: JSON) object. 608 print( 609 "You can also tell this function to just return the API call " + 610 "as a Dictionary (read: JSON) object." 611 ) 612 json_data = get_cfbd_nfl_draft_info( 613 season=2020, 614 position="QB", 615 return_as_dict=True 616 ) 617 print(json_data) 618 619 ``` 620 621 Returns 622 ---------- 623 A pandas `DataFrame` object with NFL Draft selection data, 624 or (if `return_as_dict` is set to `True`) 625 a dictionary object with NFL Draft selection data. 626 627 """ 628 now = datetime.now() 629 nfl_draft_df = pd.DataFrame() 630 # row_df = pd.DataFrame() 631 url = "https://api.collegefootballdata.com/draft/picks" 632 633 # Input validation 634 ########################################################################## 635 636 # `year` to `season` 637 if season is not None and year is not None and (year is not season): 638 raise ValueError( 639 "When using this function, " 640 + "please specify a season in EITHER `year` or `season`." 641 ) 642 if season is not None: 643 pass 644 elif year is not None: 645 season = year 646 else: 647 raise ValueError("No year/season inputted for this function.") 648 649 if api_key is not None: 650 real_api_key = api_key 651 del api_key 652 else: 653 real_api_key = get_cfbd_api_token(api_key_dir=api_key_dir) 654 655 if real_api_key == "tigersAreAwesome": 656 raise ValueError( 657 "You actually need to change `cfbd_key` to your CFBD API key." 658 ) 659 elif "Bearer " in real_api_key: 660 pass 661 elif "Bearer" in real_api_key: 662 real_api_key = real_api_key.replace("Bearer", "Bearer ") 663 else: 664 real_api_key = "Bearer " + real_api_key 665 666 if season is None \ 667 and nfl_team is None \ 668 and college is None \ 669 and conference is None: 670 671 logging.warning( 672 "Not specifying a `season`, `nfl_team`, `college`, " 673 + "or `conference` will still result in " 674 + "a successful get request (assuming the API key is valid)" 675 + ", but this is not a recommended method " 676 + "of calling this function." 677 ) 678 679 if season < 1936 or season > now.year: 680 raise ValueError( 681 f"`season` must be an integer between 1936 and {now.year}.\n" + 682 f"You entered:\n{season}" 683 ) 684 685 # URL builder 686 ########################################################################## 687 688 url_elements = 0 689 690 if season is not None and url_elements == 0: 691 url += f"?year={season}" 692 url_elements += 1 693 elif season is not None: 694 url += f"&year={season}" 695 url_elements += 1 696 697 if nfl_team is not None and url_elements == 0: 698 url += f"?nflTeam={nfl_team}" # nfl_team = "Cincinnati", not "CIN" 699 url_elements += 1 700 elif nfl_team is not None: 701 url += f"&nflTeam={nfl_team}" 702 url_elements += 1 703 704 if college is not None and url_elements == 0: 705 url += f"?college={college}" 706 url_elements += 1 707 elif college is not None: 708 url += f"&college={college}" 709 url_elements += 1 710 711 if conference is not None and url_elements == 0: 712 # conference = "SEC", not "Southeastern conference" 713 url += f"?conference={conference}" 714 url_elements += 1 715 elif conference is not None: 716 url += f"&conference={conference}" 717 url_elements += 1 718 719 if position is not None and url_elements == 0: 720 url += f"?position={position}" 721 url_elements += 1 722 elif position is not None: 723 url += f"&position={position}" 724 url_elements += 1 725 726 headers = { 727 "Authorization": f"{real_api_key}", 728 "accept": "application/json" 729 } 730 731 response = requests.get(url, headers=headers) 732 733 if response.status_code == 200: 734 pass 735 elif response.status_code == 401: 736 raise ConnectionRefusedError( 737 "Could not connect. The connection was refused.\n" + 738 "HTTP Status Code 401." 739 ) 740 else: 741 raise ConnectionError( 742 f"Could not connect.\nHTTP Status code {response.status_code}" 743 ) 744 745 json_data = response.json() 746 747 if return_as_dict is True: 748 return json_data 749 750 nfl_draft_df = pd.json_normalize(json_data) 751 # print(nfl_draft_df.columns) 752 nfl_draft_df.rename( 753 columns={ 754 "collegeAthleteId": "college_athlete_id", 755 "nflAthleteId": "nfl_athlete_id", 756 "collegeId": "college_id", 757 "collegeTeam": "college_team_name", 758 "collegeConference": "college_conference_name", 759 "preDraftRanking": "pre_draft_ranking", 760 "preDraftPositionRanking": "pre_draft_position_ranking", 761 "preDraftGrade": "pre_draft_grade", 762 "hometownInfo.city": "player_hometown_city", 763 "hometownInfo.state": "player_hometown_state", 764 "hometownInfo.country": "player_hometown_country", 765 "hometownInfo.latitude": "player_hometown_latitude", 766 "hometownInfo.longitude": "player_hometown_longitude", 767 "hometownInfo.countyFips": "player_hometown_county_fips", 768 }, 769 inplace=True, 770 ) 771 if len(nfl_draft_df) == 0: 772 logging.error( 773 "The CFBD API accepted your inputs, " 774 + "but found no data within your specified input parameters." 775 + " Please double check your input parameters." 776 ) 777 778 return nfl_draft_df
19def get_cfbd_nfl_teams( 20 api_key: str = None, api_key_dir: str = None, return_as_dict: bool = False 21): 22 """ 23 Retrieves a list of NFL teams from the CFBD API. 24 25 Parameters 26 ---------- 27 `api_key` (str, optional): 28 Semi-optional argument. 29 If `api_key` is null, this function will attempt to load a CFBD API key 30 from the python environment, or from a file on this computer. 31 If `api_key` is not null, 32 this function will automatically assume that the 33 inputted `api_key` is a valid CFBD API key. 34 35 `api_key_dir` (str, optional): 36 Optional argument. 37 If `api_key` is set to am empty string, this variable is ignored. 38 If `api_key_dir` is null, and `api_key` is null, 39 this function will try to find 40 a CFBD API key file in this user's home directory. 41 If `api_key_dir` is set to a string, and `api_key` is null, 42 this function will assume that `api_key_dir` is a directory, 43 and will try to find a CFBD API key file in that directory. 44 45 `return_as_dict` (bool, semi-optional): 46 Semi-optional argument. 47 If you want this function to return the data 48 as a dictionary (read: JSON object), 49 instead of a pandas `DataFrame` object, 50 set `return_as_dict` to `True`. 51 52 Usage 53 ---------- 54 ``` 55 import time 56 57 from cfbd_json_py.draft import get_cfbd_nfl_teams 58 59 cfbd_key = "tigersAreAwesome" # placeholder for your CFBD API Key. 60 61 if cfbd_key is not "tigersAreAwesome": 62 print( 63 "Using the user's API key declared in this script " + 64 "for this example." 65 ) 66 67 # Gets NFL team info from the CFBD API. 68 print("Gets NFL team info from the CFBD API.") 69 json_data = get_cfbd_nfl_teams(api_key=cfbd_key) 70 print(json_data) 71 time.sleep(5) 72 73 # You can also tell this function to just return the API call 74 # as a Dictionary (read: JSON) object. 75 print( 76 "You can also tell this function to just return the API call " + 77 "as a Dictionary (read: JSON) object." 78 ) 79 json_data = get_cfbd_nfl_teams( 80 api_key=cfbd_key, 81 return_as_dict=True) 82 print(json_data) 83 84 else: 85 # Alternatively, if the CFBD API key exists in this python environment, 86 # or it's been set by cfbd_json_py.utls.set_cfbd_api_token(), 87 # you could just call these functions directly, 88 # without setting the API key in the script. 89 print( 90 "Using the user's API key supposedly loaded into " + 91 "this python environment for this example." 92 ) 93 94 # Gets NFL team info from the CFBD API. 95 print("Gets NFL team info from the CFBD API.") 96 json_data = get_cfbd_nfl_teams() 97 print(json_data) 98 time.sleep(5) 99 100 # You can also tell this function to just return the API call 101 # as a Dictionary (read: JSON) object. 102 print( 103 "You can also tell this function to just return the API call " + 104 "as a Dictionary (read: JSON) object." 105 ) 106 json_data = get_cfbd_nfl_teams(return_as_dict=True) 107 print(json_data) 108 109 ``` 110 111 Returns 112 ---------- 113 A pandas `DataFrame` object with NFL team data, 114 or (if `return_as_dict` is set to `True`) 115 a dictionary object with NFL team data. 116 117 """ 118 119 nfl_teams_df = pd.DataFrame() 120 # row_df = pd.DataFrame() 121 url = "https://api.collegefootballdata.com/draft/teams" 122 123 # Input validation 124 ########################################################################## 125 126 if api_key is not None: 127 real_api_key = api_key 128 del api_key 129 else: 130 real_api_key = get_cfbd_api_token(api_key_dir=api_key_dir) 131 132 if real_api_key == "tigersAreAwesome": 133 raise ValueError( 134 "You actually need to change `cfbd_key` to your CFBD API key." 135 ) 136 elif "Bearer " in real_api_key: 137 pass 138 elif "Bearer" in real_api_key: 139 real_api_key = real_api_key.replace("Bearer", "Bearer ") 140 else: 141 real_api_key = "Bearer " + real_api_key 142 143 headers = { 144 "Authorization": f"{real_api_key}", 145 "accept": "application/json" 146 } 147 148 response = requests.get(url, headers=headers) 149 150 if response.status_code == 200: 151 pass 152 elif response.status_code == 401: 153 raise ConnectionRefusedError( 154 "Could not connect. The connection was refused.\n" + 155 "HTTP Status Code 401." 156 ) 157 else: 158 raise ConnectionError( 159 f"Could not connect.\nHTTP Status code {response.status_code}" 160 ) 161 162 json_data = response.json() 163 164 if return_as_dict is True: 165 return json_data 166 167 nfl_teams_df = pd.json_normalize(json_data) 168 nfl_teams_df.rename(columns={"displayName": "display_name"}, inplace=True) 169 return nfl_teams_df
Retrieves a list of NFL teams from the CFBD API.
Parameters
api_key
(str, optional):
Semi-optional argument.
If api_key
is null, this function will attempt to load a CFBD API key
from the python environment, or from a file on this computer.
If api_key
is not null,
this function will automatically assume that the
inputted api_key
is a valid CFBD API key.
api_key_dir
(str, optional):
Optional argument.
If api_key
is set to am empty string, this variable is ignored.
If api_key_dir
is null, and api_key
is null,
this function will try to find
a CFBD API key file in this user's home directory.
If api_key_dir
is set to a string, and api_key
is null,
this function will assume that api_key_dir
is a directory,
and will try to find a CFBD API key file in that directory.
return_as_dict
(bool, semi-optional):
Semi-optional argument.
If you want this function to return the data
as a dictionary (read: JSON object),
instead of a pandas DataFrame
object,
set return_as_dict
to True
.
Usage
import time
from cfbd_json_py.draft import get_cfbd_nfl_teams
cfbd_key = "tigersAreAwesome" # placeholder for your CFBD API Key.
if cfbd_key is not "tigersAreAwesome":
print(
"Using the user's API key declared in this script " +
"for this example."
)
# Gets NFL team info from the CFBD API.
print("Gets NFL team info from the CFBD API.")
json_data = get_cfbd_nfl_teams(api_key=cfbd_key)
print(json_data)
time.sleep(5)
# You can also tell this function to just return the API call
# as a Dictionary (read: JSON) object.
print(
"You can also tell this function to just return the API call " +
"as a Dictionary (read: JSON) object."
)
json_data = get_cfbd_nfl_teams(
api_key=cfbd_key,
return_as_dict=True)
print(json_data)
else:
# Alternatively, if the CFBD API key exists in this python environment,
# or it's been set by cfbd_json_py.utls.set_cfbd_api_token(),
# you could just call these functions directly,
# without setting the API key in the script.
print(
"Using the user's API key supposedly loaded into " +
"this python environment for this example."
)
# Gets NFL team info from the CFBD API.
print("Gets NFL team info from the CFBD API.")
json_data = get_cfbd_nfl_teams()
print(json_data)
time.sleep(5)
# You can also tell this function to just return the API call
# as a Dictionary (read: JSON) object.
print(
"You can also tell this function to just return the API call " +
"as a Dictionary (read: JSON) object."
)
json_data = get_cfbd_nfl_teams(return_as_dict=True)
print(json_data)
Returns
A pandas DataFrame
object with NFL team data,
or (if return_as_dict
is set to True
)
a dictionary object with NFL team data.
172def get_cfbd_nfl_positions( 173 api_key: str = None, api_key_dir: str = None, return_as_dict: bool = False 174): 175 """ 176 Retrieves a list of player positions for the NFL Draft from the CFBD API. 177 178 Parameters 179 ---------- 180 `api_key` (str, optional): 181 Semi-optional argument. 182 If `api_key` is null, this function will attempt to load a CFBD API key 183 from the python environment, or from a file on this computer. 184 If `api_key` is not null, 185 this function will automatically assume that the 186 inputted `api_key` is a valid CFBD API key. 187 188 `api_key_dir` (str, optional): 189 Optional argument. 190 If `api_key` is set to am empty string, this variable is ignored. 191 If `api_key_dir` is null, and `api_key` is null, 192 this function will try to find 193 a CFBD API key file in this user's home directory. 194 If `api_key_dir` is set to a string, and `api_key` is null, 195 this function will assume that `api_key_dir` is a directory, 196 and will try to find a CFBD API key file in that directory. 197 198 `return_as_dict` (bool, semi-optional): 199 Semi-optional argument. 200 If you want this function to return 201 the data as a dictionary (read: JSON object), 202 instead of a pandas `DataFrame` object, 203 set `return_as_dict` to `True`. 204 205 Usage 206 ---------- 207 ``` 208 import time 209 210 from cfbd_json_py.draft import get_cfbd_nfl_positions 211 212 cfbd_key = "tigersAreAwesome" # placeholder for your CFBD API Key. 213 214 if cfbd_key is not "tigersAreAwesome": 215 print( 216 "Using the user's API key declared in this script " + 217 "for this example." 218 ) 219 220 # Gets a list of player positions for the NFL Draft from the CFBD API. 221 print( 222 "Gets a list of player positions for the NFL Draft " + 223 "from the CFBD API." 224 ) 225 json_data = get_cfbd_nfl_positions(api_key=cfbd_key) 226 print(json_data) 227 time.sleep(5) 228 229 # You can also tell this function to just return the API call 230 # as a Dictionary (read: JSON) object. 231 print( 232 "You can also tell this function to just return the API call " + 233 "as a Dictionary (read: JSON) object." 234 ) 235 json_data = get_cfbd_nfl_positions( 236 api_key=cfbd_key, 237 return_as_dict=True) 238 print(json_data) 239 240 else: 241 # Alternatively, if the CFBD API key exists in this python environment, 242 # or it's been set by cfbd_json_py.utls.set_cfbd_api_token(), 243 # you could just call these functions directly, 244 # without setting the API key in the script. 245 print( 246 "Using the user's API key supposedly loaded into " + 247 "this python environment for this example." 248 ) 249 250 # Gets a list of player positions for the NFL Draft from the CFBD API. 251 print( 252 "Gets a list of player positions for the NFL Draft " + 253 "from the CFBD API." 254 ) 255 json_data = get_cfbd_nfl_positions() 256 print(json_data) 257 time.sleep(5) 258 259 # You can also tell this function to just return the API call 260 # as a Dictionary (read: JSON) object. 261 print( 262 "You can also tell this function to just return the API call " + 263 "as a Dictionary (read: JSON) object." 264 ) 265 json_data = get_cfbd_nfl_positions(return_as_dict=True) 266 print(json_data) 267 268 ``` 269 Returns 270 ---------- 271 A pandas `DataFrame` object with player position data, 272 or (if `return_as_dict` is set to `True`) 273 a dictionary object with player position data. 274 275 """ 276 277 positions_df = pd.DataFrame() 278 # row_df = pd.DataFrame() 279 url = "https://api.collegefootballdata.com/draft/positions" 280 281 # Input validation 282 ########################################################################## 283 284 if api_key is not None: 285 real_api_key = api_key 286 del api_key 287 else: 288 real_api_key = get_cfbd_api_token(api_key_dir=api_key_dir) 289 290 if real_api_key == "tigersAreAwesome": 291 raise ValueError( 292 "You actually need to change `cfbd_key` to your CFBD API key." 293 ) 294 elif "Bearer " in real_api_key: 295 pass 296 elif "Bearer" in real_api_key: 297 real_api_key = real_api_key.replace("Bearer", "Bearer ") 298 else: 299 real_api_key = "Bearer " + real_api_key 300 301 headers = { 302 "Authorization": f"{real_api_key}", 303 "accept": "application/json" 304 } 305 306 response = requests.get(url, headers=headers) 307 308 if response.status_code == 200: 309 pass 310 elif response.status_code == 401: 311 raise ConnectionRefusedError( 312 "Could not connect. The connection was refused.\n" + 313 "HTTP Status Code 401." 314 ) 315 else: 316 raise ConnectionError( 317 f"Could not connect.\nHTTP Status code {response.status_code}" 318 ) 319 320 json_data = response.json() 321 322 if return_as_dict is True: 323 return json_data 324 325 # for p in json_data: 326 # position_name = p['name'] 327 # position_abbreviation = p['abbreviation'] 328 329 # row_df = pd.DataFrame( 330 # { 331 # "position_name": position_name, 332 # "position_abbreviation": position_abbreviation 333 # }, index=[0] 334 # ) 335 # positions_df = pd.concat([positions_df, row_df], ignore_index=True) 336 337 # del position_name, position_abbreviation 338 # del row_df 339 positions_df = pd.json_normalize(json_data) 340 positions_df.rename( 341 columns={ 342 "name": "position_name", 343 "abbreviation": "position_abbreviation", 344 }, 345 inplace=True, 346 ) 347 348 return positions_df
Retrieves a list of player positions for the NFL Draft from the CFBD API.
Parameters
api_key
(str, optional):
Semi-optional argument.
If api_key
is null, this function will attempt to load a CFBD API key
from the python environment, or from a file on this computer.
If api_key
is not null,
this function will automatically assume that the
inputted api_key
is a valid CFBD API key.
api_key_dir
(str, optional):
Optional argument.
If api_key
is set to am empty string, this variable is ignored.
If api_key_dir
is null, and api_key
is null,
this function will try to find
a CFBD API key file in this user's home directory.
If api_key_dir
is set to a string, and api_key
is null,
this function will assume that api_key_dir
is a directory,
and will try to find a CFBD API key file in that directory.
return_as_dict
(bool, semi-optional):
Semi-optional argument.
If you want this function to return
the data as a dictionary (read: JSON object),
instead of a pandas DataFrame
object,
set return_as_dict
to True
.
Usage
import time
from cfbd_json_py.draft import get_cfbd_nfl_positions
cfbd_key = "tigersAreAwesome" # placeholder for your CFBD API Key.
if cfbd_key is not "tigersAreAwesome":
print(
"Using the user's API key declared in this script " +
"for this example."
)
# Gets a list of player positions for the NFL Draft from the CFBD API.
print(
"Gets a list of player positions for the NFL Draft " +
"from the CFBD API."
)
json_data = get_cfbd_nfl_positions(api_key=cfbd_key)
print(json_data)
time.sleep(5)
# You can also tell this function to just return the API call
# as a Dictionary (read: JSON) object.
print(
"You can also tell this function to just return the API call " +
"as a Dictionary (read: JSON) object."
)
json_data = get_cfbd_nfl_positions(
api_key=cfbd_key,
return_as_dict=True)
print(json_data)
else:
# Alternatively, if the CFBD API key exists in this python environment,
# or it's been set by cfbd_json_py.utls.set_cfbd_api_token(),
# you could just call these functions directly,
# without setting the API key in the script.
print(
"Using the user's API key supposedly loaded into " +
"this python environment for this example."
)
# Gets a list of player positions for the NFL Draft from the CFBD API.
print(
"Gets a list of player positions for the NFL Draft " +
"from the CFBD API."
)
json_data = get_cfbd_nfl_positions()
print(json_data)
time.sleep(5)
# You can also tell this function to just return the API call
# as a Dictionary (read: JSON) object.
print(
"You can also tell this function to just return the API call " +
"as a Dictionary (read: JSON) object."
)
json_data = get_cfbd_nfl_positions(return_as_dict=True)
print(json_data)
Returns
A pandas DataFrame
object with player position data,
or (if return_as_dict
is set to True
)
a dictionary object with player position data.
351def get_cfbd_nfl_draft_info( 352 api_key: str = None, 353 api_key_dir: str = None, 354 season: int = None, 355 nfl_team: str = None, 356 college: str = None, 357 conference: str = None, 358 position: str = None, 359 year: int = None, 360 return_as_dict: bool = False, 361): 362 """ 363 Retrieves a list of actual NFL Draft selections from the CFBD API. 364 365 Parameters 366 ---------- 367 368 `api_key` (str, optional): 369 Semi-optional argument. 370 If `api_key` is null, this function will attempt to load a CFBD API key 371 from the python environment, or from a file on this computer. 372 If `api_key` is not null, 373 this function will automatically assume that the 374 inputted `api_key` is a valid CFBD API key. 375 376 `api_key_dir` (str, optional): 377 Optional argument. 378 If `api_key` is set to am empty string, this variable is ignored. 379 If `api_key_dir` is null, and `api_key` is null, 380 this function will try to find 381 a CFBD API key file in this user's home directory. 382 If `api_key_dir` is set to a string, and `api_key` is null, 383 this function will assume that `api_key_dir` is a directory, 384 and will try to find a CFBD API key file in that directory. 385 386 The following parameters are optional, 387 but it is highly recommended to not call this function 388 without declaring one of these five optional parameters 389 as a non-null value. 390 391 `season` (int, semi-optional): 392 Semi-Optional argument. 393 This is the season you want NFL Draft information for. 394 For example, if you only want data for the 2020 NFL Draft, 395 set `season` to `2020`. 396 397 `nfl_team` (str, optional): 398 Semi-Optional argument. 399 If you only want NFL Draft selections from a specific NFL team, 400 set `nfl_team` to the name of that team. 401 For example, if you want to only get NFL Draft information for 402 draft picks made by the Cincinnati Bengals, 403 set `nfl_team` to `Cincinnati`. 404 405 `college` (str, optional): 406 Semi-Optional argument. 407 If you only want NFL Draft selections from a specific CFB team, 408 set `college` to the name of that team. 409 For example, if you want to only get NFL Draft information for 410 draft picks from the Clemson Tigers Football Program, 411 set `college` to `Clemson`. 412 413 `conference` (str, optional): 414 Semi-Optional argument. 415 If you only want NFL Draft selections from a specific CFB conference, 416 set `conference` to the abbreviation of that conference. 417 A list of CFBD API conference abbreviations can be found 418 in the `conference_abbreviation` column from 419 the pandas DataFrame that is returned by calling 420 `cfbd_json_py.conferences.get_cfbd_conference_info()`. 421 For example, if you want to only get NFL Draft information for 422 draft picks that played in the Big 12, set `conference` to `B12`. 423 424 `year` (int): 425 Alternative keyword for `season` 426 427 `position` (str, optional): 428 Semi-Optional argument. 429 If you only want NFL Draft selections who played a specific position, 430 set `position` to that position's abbreviation. 431 A list of CFBD API positions can be found in the 432 `position_abbreviation` column from 433 the pandas DataFrame that is returned by calling 434 `cfbd_json_py.draft.get_cfbd_nfl_positions()`. 435 436 `return_as_dict` (bool, semi-optional): 437 Semi-optional argument. 438 If you want this function to return the data 439 as a dictionary (read: JSON object), 440 instead of a pandas `DataFrame` object, 441 set `return_as_dict` to `True`. 442 Usage 443 ---------- 444 445 ``` 446 import time 447 448 from cfbd_json_py.draft import get_cfbd_nfl_draft_info 449 450 cfbd_key = "tigersAreAwesome" # placeholder for your CFBD API Key. 451 452 if cfbd_key is not "tigersAreAwesome": 453 print( 454 "Using the user's API key declared in this script " + 455 "for this example." 456 ) 457 458 # Get NFL Draft selections from the 2020 NFL Draft. 459 print("Get NFL Draft selections from the 2020 NFL Draft.") 460 json_data = get_cfbd_nfl_draft_info( 461 api_key=cfbd_key, 462 season=2020 463 ) 464 print(json_data) 465 time.sleep(5) 466 467 # Get NFL Draft selections from the 2020 NFL Draft made by the 468 # 2020 Cincinnati Bengals. 469 print( 470 "Get NFL Draft selections from the 2020 NFL Draft made " + 471 "by the 2020 Cincinnati Bengals." 472 ) 473 json_data = get_cfbd_nfl_draft_info( 474 api_key=cfbd_key, 475 season=2020, 476 nfl_team="Cincinnati" 477 ) 478 print(json_data) 479 time.sleep(5) 480 481 # Get NFL Draft selections from the 2020 NFL Draft made involving 482 # Clemson Tigers football players. 483 print( 484 "Get NFL Draft selections from the 2020 NFL Draft made " + 485 "involving Clemson Tigers football players." 486 ) 487 json_data = get_cfbd_nfl_draft_info( 488 api_key=cfbd_key, 489 season=2020, 490 college="Clemson" 491 ) 492 print(json_data) 493 time.sleep(5) 494 495 # Get NFL Draft selections from the 2020 NFL Draft made involving 496 # players who played in the Southeastern conference (SEC). 497 print( 498 "Get NFL Draft selections from the 2020 NFL Draft made " + 499 "involving players who played " + 500 "in the Southeastern conference (SEC)." 501 ) 502 json_data = get_cfbd_nfl_draft_info( 503 api_key=cfbd_key, 504 season=2020, 505 conference="SEC" 506 ) 507 print(json_data) 508 time.sleep(5) 509 510 # Get NFL Draft selections from the 2020 NFL Draft made 511 # where the selected player was a QB in college. 512 print( 513 "Get NFL Draft selections from the 2020 NFL Draft made " + 514 "where the selected player was a QB in college." 515 ) 516 json_data = get_cfbd_nfl_draft_info( 517 api_key=cfbd_key, 518 season=2020, 519 position="QB" 520 ) 521 print(json_data) 522 time.sleep(5) 523 524 # You can also tell this function to just return the API call 525 # as a Dictionary (read: JSON) object. 526 print( 527 "You can also tell this function to just return the API call " + 528 "as a Dictionary (read: JSON) object." 529 ) 530 json_data = get_cfbd_nfl_draft_info( 531 season=2020, 532 position="QB", 533 api_key=cfbd_key, 534 return_as_dict=True 535 ) 536 print(json_data) 537 538 else: 539 # Alternatively, if the CFBD API key exists in this python environment, 540 # or it's been set by cfbd_json_py.utls.set_cfbd_api_token(), 541 # you could just call these functions directly, 542 # without setting the API key in the script. 543 print( 544 "Using the user's API key supposedly loaded " + 545 "into this python environment for this example." 546 ) 547 548 # Get NFL Draft selections from the 2020 NFL Draft. 549 print("Get NFL Draft selections from the 2020 NFL Draft.") 550 json_data = get_cfbd_nfl_draft_info(season=2020) 551 print(json_data) 552 time.sleep(5) 553 554 # Get NFL Draft selections from the 2020 NFL Draft made by the 555 # 2020 Cincinnati Bengals. 556 print( 557 "Get NFL Draft selections from the 2020 NFL Draft " + 558 "made by the 2020 Cincinnati Bengals." 559 ) 560 json_data = get_cfbd_nfl_draft_info( 561 season=2020, 562 nfl_team="Cincinnati" 563 ) 564 print(json_data) 565 time.sleep(5) 566 567 # Get NFL Draft selections from the 2020 NFL Draft made involving 568 # Clemson Tigers football players. 569 print( 570 "Get NFL Draft selections from the 2020 NFL Draft made " + 571 "involving Clemson Tigers football players." 572 ) 573 json_data = get_cfbd_nfl_draft_info( 574 season=2020, 575 college="Clemson" 576 ) 577 print(json_data) 578 time.sleep(5) 579 580 # Get NFL Draft selections from the 2020 NFL Draft made involving 581 # players who played in the Southeastern conference (SEC). 582 print( 583 "Get NFL Draft selections from the 2020 NFL Draft made " + 584 "involving players who played " + 585 "in the Southeastern conference (SEC)." 586 ) 587 json_data = get_cfbd_nfl_draft_info( 588 season=2020, 589 conference="SEC" 590 ) 591 print(json_data) 592 time.sleep(5) 593 594 # Get NFL Draft selections from the 2020 NFL Draft made 595 # where the selected player was a QB in college. 596 print( 597 "Get NFL Draft selections from the 2020 NFL Draft made " + 598 "where the selected player was a QB in college." 599 ) 600 json_data = get_cfbd_nfl_draft_info( 601 season=2020, 602 position="QB" 603 ) 604 print(json_data) 605 time.sleep(5) 606 607 # You can also tell this function to just return the API call 608 # as a Dictionary (read: JSON) object. 609 print( 610 "You can also tell this function to just return the API call " + 611 "as a Dictionary (read: JSON) object." 612 ) 613 json_data = get_cfbd_nfl_draft_info( 614 season=2020, 615 position="QB", 616 return_as_dict=True 617 ) 618 print(json_data) 619 620 ``` 621 622 Returns 623 ---------- 624 A pandas `DataFrame` object with NFL Draft selection data, 625 or (if `return_as_dict` is set to `True`) 626 a dictionary object with NFL Draft selection data. 627 628 """ 629 now = datetime.now() 630 nfl_draft_df = pd.DataFrame() 631 # row_df = pd.DataFrame() 632 url = "https://api.collegefootballdata.com/draft/picks" 633 634 # Input validation 635 ########################################################################## 636 637 # `year` to `season` 638 if season is not None and year is not None and (year is not season): 639 raise ValueError( 640 "When using this function, " 641 + "please specify a season in EITHER `year` or `season`." 642 ) 643 if season is not None: 644 pass 645 elif year is not None: 646 season = year 647 else: 648 raise ValueError("No year/season inputted for this function.") 649 650 if api_key is not None: 651 real_api_key = api_key 652 del api_key 653 else: 654 real_api_key = get_cfbd_api_token(api_key_dir=api_key_dir) 655 656 if real_api_key == "tigersAreAwesome": 657 raise ValueError( 658 "You actually need to change `cfbd_key` to your CFBD API key." 659 ) 660 elif "Bearer " in real_api_key: 661 pass 662 elif "Bearer" in real_api_key: 663 real_api_key = real_api_key.replace("Bearer", "Bearer ") 664 else: 665 real_api_key = "Bearer " + real_api_key 666 667 if season is None \ 668 and nfl_team is None \ 669 and college is None \ 670 and conference is None: 671 672 logging.warning( 673 "Not specifying a `season`, `nfl_team`, `college`, " 674 + "or `conference` will still result in " 675 + "a successful get request (assuming the API key is valid)" 676 + ", but this is not a recommended method " 677 + "of calling this function." 678 ) 679 680 if season < 1936 or season > now.year: 681 raise ValueError( 682 f"`season` must be an integer between 1936 and {now.year}.\n" + 683 f"You entered:\n{season}" 684 ) 685 686 # URL builder 687 ########################################################################## 688 689 url_elements = 0 690 691 if season is not None and url_elements == 0: 692 url += f"?year={season}" 693 url_elements += 1 694 elif season is not None: 695 url += f"&year={season}" 696 url_elements += 1 697 698 if nfl_team is not None and url_elements == 0: 699 url += f"?nflTeam={nfl_team}" # nfl_team = "Cincinnati", not "CIN" 700 url_elements += 1 701 elif nfl_team is not None: 702 url += f"&nflTeam={nfl_team}" 703 url_elements += 1 704 705 if college is not None and url_elements == 0: 706 url += f"?college={college}" 707 url_elements += 1 708 elif college is not None: 709 url += f"&college={college}" 710 url_elements += 1 711 712 if conference is not None and url_elements == 0: 713 # conference = "SEC", not "Southeastern conference" 714 url += f"?conference={conference}" 715 url_elements += 1 716 elif conference is not None: 717 url += f"&conference={conference}" 718 url_elements += 1 719 720 if position is not None and url_elements == 0: 721 url += f"?position={position}" 722 url_elements += 1 723 elif position is not None: 724 url += f"&position={position}" 725 url_elements += 1 726 727 headers = { 728 "Authorization": f"{real_api_key}", 729 "accept": "application/json" 730 } 731 732 response = requests.get(url, headers=headers) 733 734 if response.status_code == 200: 735 pass 736 elif response.status_code == 401: 737 raise ConnectionRefusedError( 738 "Could not connect. The connection was refused.\n" + 739 "HTTP Status Code 401." 740 ) 741 else: 742 raise ConnectionError( 743 f"Could not connect.\nHTTP Status code {response.status_code}" 744 ) 745 746 json_data = response.json() 747 748 if return_as_dict is True: 749 return json_data 750 751 nfl_draft_df = pd.json_normalize(json_data) 752 # print(nfl_draft_df.columns) 753 nfl_draft_df.rename( 754 columns={ 755 "collegeAthleteId": "college_athlete_id", 756 "nflAthleteId": "nfl_athlete_id", 757 "collegeId": "college_id", 758 "collegeTeam": "college_team_name", 759 "collegeConference": "college_conference_name", 760 "preDraftRanking": "pre_draft_ranking", 761 "preDraftPositionRanking": "pre_draft_position_ranking", 762 "preDraftGrade": "pre_draft_grade", 763 "hometownInfo.city": "player_hometown_city", 764 "hometownInfo.state": "player_hometown_state", 765 "hometownInfo.country": "player_hometown_country", 766 "hometownInfo.latitude": "player_hometown_latitude", 767 "hometownInfo.longitude": "player_hometown_longitude", 768 "hometownInfo.countyFips": "player_hometown_county_fips", 769 }, 770 inplace=True, 771 ) 772 if len(nfl_draft_df) == 0: 773 logging.error( 774 "The CFBD API accepted your inputs, " 775 + "but found no data within your specified input parameters." 776 + " Please double check your input parameters." 777 ) 778 779 return nfl_draft_df
Retrieves a list of actual NFL Draft selections from the CFBD API.
Parameters
api_key
(str, optional):
Semi-optional argument.
If api_key
is null, this function will attempt to load a CFBD API key
from the python environment, or from a file on this computer.
If api_key
is not null,
this function will automatically assume that the
inputted api_key
is a valid CFBD API key.
api_key_dir
(str, optional):
Optional argument.
If api_key
is set to am empty string, this variable is ignored.
If api_key_dir
is null, and api_key
is null,
this function will try to find
a CFBD API key file in this user's home directory.
If api_key_dir
is set to a string, and api_key
is null,
this function will assume that api_key_dir
is a directory,
and will try to find a CFBD API key file in that directory.
The following parameters are optional, but it is highly recommended to not call this function without declaring one of these five optional parameters as a non-null value.
season
(int, semi-optional):
Semi-Optional argument.
This is the season you want NFL Draft information for.
For example, if you only want data for the 2020 NFL Draft,
set season
to 2020
.
nfl_team
(str, optional):
Semi-Optional argument.
If you only want NFL Draft selections from a specific NFL team,
set nfl_team
to the name of that team.
For example, if you want to only get NFL Draft information for
draft picks made by the Cincinnati Bengals,
set nfl_team
to Cincinnati
.
college
(str, optional):
Semi-Optional argument.
If you only want NFL Draft selections from a specific CFB team,
set college
to the name of that team.
For example, if you want to only get NFL Draft information for
draft picks from the Clemson Tigers Football Program,
set college
to Clemson
.
conference
(str, optional):
Semi-Optional argument.
If you only want NFL Draft selections from a specific CFB conference,
set conference
to the abbreviation of that conference.
A list of CFBD API conference abbreviations can be found
in the conference_abbreviation
column from
the pandas DataFrame that is returned by calling
cfbd_json_py.conferences.get_cfbd_conference_info()
.
For example, if you want to only get NFL Draft information for
draft picks that played in the Big 12, set conference
to B12
.
year
(int):
Alternative keyword for season
position
(str, optional):
Semi-Optional argument.
If you only want NFL Draft selections who played a specific position,
set position
to that position's abbreviation.
A list of CFBD API positions can be found in the
position_abbreviation
column from
the pandas DataFrame that is returned by calling
cfbd_json_py.draft.get_cfbd_nfl_positions()
.
return_as_dict
(bool, semi-optional):
Semi-optional argument.
If you want this function to return the data
as a dictionary (read: JSON object),
instead of a pandas DataFrame
object,
set return_as_dict
to True
.
Usage
import time
from cfbd_json_py.draft import get_cfbd_nfl_draft_info
cfbd_key = "tigersAreAwesome" # placeholder for your CFBD API Key.
if cfbd_key is not "tigersAreAwesome":
print(
"Using the user's API key declared in this script " +
"for this example."
)
# Get NFL Draft selections from the 2020 NFL Draft.
print("Get NFL Draft selections from the 2020 NFL Draft.")
json_data = get_cfbd_nfl_draft_info(
api_key=cfbd_key,
season=2020
)
print(json_data)
time.sleep(5)
# Get NFL Draft selections from the 2020 NFL Draft made by the
# 2020 Cincinnati Bengals.
print(
"Get NFL Draft selections from the 2020 NFL Draft made " +
"by the 2020 Cincinnati Bengals."
)
json_data = get_cfbd_nfl_draft_info(
api_key=cfbd_key,
season=2020,
nfl_team="Cincinnati"
)
print(json_data)
time.sleep(5)
# Get NFL Draft selections from the 2020 NFL Draft made involving
# Clemson Tigers football players.
print(
"Get NFL Draft selections from the 2020 NFL Draft made " +
"involving Clemson Tigers football players."
)
json_data = get_cfbd_nfl_draft_info(
api_key=cfbd_key,
season=2020,
college="Clemson"
)
print(json_data)
time.sleep(5)
# Get NFL Draft selections from the 2020 NFL Draft made involving
# players who played in the Southeastern conference (SEC).
print(
"Get NFL Draft selections from the 2020 NFL Draft made " +
"involving players who played " +
"in the Southeastern conference (SEC)."
)
json_data = get_cfbd_nfl_draft_info(
api_key=cfbd_key,
season=2020,
conference="SEC"
)
print(json_data)
time.sleep(5)
# Get NFL Draft selections from the 2020 NFL Draft made
# where the selected player was a QB in college.
print(
"Get NFL Draft selections from the 2020 NFL Draft made " +
"where the selected player was a QB in college."
)
json_data = get_cfbd_nfl_draft_info(
api_key=cfbd_key,
season=2020,
position="QB"
)
print(json_data)
time.sleep(5)
# You can also tell this function to just return the API call
# as a Dictionary (read: JSON) object.
print(
"You can also tell this function to just return the API call " +
"as a Dictionary (read: JSON) object."
)
json_data = get_cfbd_nfl_draft_info(
season=2020,
position="QB",
api_key=cfbd_key,
return_as_dict=True
)
print(json_data)
else:
# Alternatively, if the CFBD API key exists in this python environment,
# or it's been set by cfbd_json_py.utls.set_cfbd_api_token(),
# you could just call these functions directly,
# without setting the API key in the script.
print(
"Using the user's API key supposedly loaded " +
"into this python environment for this example."
)
# Get NFL Draft selections from the 2020 NFL Draft.
print("Get NFL Draft selections from the 2020 NFL Draft.")
json_data = get_cfbd_nfl_draft_info(season=2020)
print(json_data)
time.sleep(5)
# Get NFL Draft selections from the 2020 NFL Draft made by the
# 2020 Cincinnati Bengals.
print(
"Get NFL Draft selections from the 2020 NFL Draft " +
"made by the 2020 Cincinnati Bengals."
)
json_data = get_cfbd_nfl_draft_info(
season=2020,
nfl_team="Cincinnati"
)
print(json_data)
time.sleep(5)
# Get NFL Draft selections from the 2020 NFL Draft made involving
# Clemson Tigers football players.
print(
"Get NFL Draft selections from the 2020 NFL Draft made " +
"involving Clemson Tigers football players."
)
json_data = get_cfbd_nfl_draft_info(
season=2020,
college="Clemson"
)
print(json_data)
time.sleep(5)
# Get NFL Draft selections from the 2020 NFL Draft made involving
# players who played in the Southeastern conference (SEC).
print(
"Get NFL Draft selections from the 2020 NFL Draft made " +
"involving players who played " +
"in the Southeastern conference (SEC)."
)
json_data = get_cfbd_nfl_draft_info(
season=2020,
conference="SEC"
)
print(json_data)
time.sleep(5)
# Get NFL Draft selections from the 2020 NFL Draft made
# where the selected player was a QB in college.
print(
"Get NFL Draft selections from the 2020 NFL Draft made " +
"where the selected player was a QB in college."
)
json_data = get_cfbd_nfl_draft_info(
season=2020,
position="QB"
)
print(json_data)
time.sleep(5)
# You can also tell this function to just return the API call
# as a Dictionary (read: JSON) object.
print(
"You can also tell this function to just return the API call " +
"as a Dictionary (read: JSON) object."
)
json_data = get_cfbd_nfl_draft_info(
season=2020,
position="QB",
return_as_dict=True
)
print(json_data)
Returns
A pandas DataFrame
object with NFL Draft selection data,
or (if return_as_dict
is set to True
)
a dictionary object with NFL Draft selection data.