cfbd_json_py.coaches

  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: coaches.py
  5# Purpose: Houses functions pertaining to coaching data within the CFBD API.
  6###############################################################################
  7
  8import logging
  9import warnings
 10
 11import pandas as pd
 12import requests
 13from tqdm import tqdm
 14
 15from cfbd_json_py.utls import get_cfbd_api_token
 16
 17
 18def get_cfbd_coaches_info(
 19    api_key: str = None,
 20    api_key_dir: str = None,
 21    first_name: str = None,
 22    last_name: str = None,
 23    team: str = None,
 24    season: int = None,
 25    min_season: int = None,
 26    max_season: int = None,
 27    year: int = None,
 28    return_as_dict: bool = False,
 29):
 30    """
 31    Retrieves information from the CFBD API on CFB Head Coaches.
 32
 33    Parameters
 34    ----------
 35    `api_key` (str, optional):
 36        Semi-optional argument.
 37        If `api_key` is null, this function will attempt to load a CFBD API key
 38        from the python environment, or from a file on this computer.
 39        If `api_key` is not null,
 40        this function will automatically assume that the
 41        inputted `api_key` is a valid CFBD API key.
 42
 43    `api_key_dir` (str, optional):
 44        Optional argument.
 45        If `api_key` is set to am empty string, this variable is ignored.
 46        If `api_key_dir` is null, and `api_key` is null,
 47        this function will try to find
 48        a CFBD API key file in this user's home directory.
 49        If `api_key_dir` is set to a string, and `api_key` is null,
 50        this function will assume that `api_key_dir` is a directory,
 51        and will try to find a CFBD API key file in that directory.
 52
 53    `first_name` (str, optional):
 54        Optional argument.
 55        If you want to only look up coaches with a specific first name,
 56        set this variable to that specific first name, and this function
 57        will attempt to look up coaches with that specific first name.
 58
 59    `last_name` (str, optional):
 60        Optional argument.
 61        If you want to only look up coaches with a specific last name,
 62        set this variable to that specific first name, and this function
 63        will attempt to look up coaches with that specific last name.
 64
 65    `team` (str, optional):
 66        Optional argument.
 67        If you want to filter and drill down to coaches who coached a specific
 68        CFB team, set this
 69
 70    `season` (int, optional):
 71        Optional argument.
 72        If you only want coaches from a specific season,
 73        set this variable to that season.
 74
 75    `min_season` (int, optional):
 76        Optional argument.
 77        Similar to `year`, but used in tandem with `max_season`
 78        to get coaches who coached with in a range of seasons.
 79
 80    `max_season` (int, optional):
 81        Optional argument.
 82        Similar to `year`, but used in tandem with `min_season`
 83        to get coaches who coached with in a range of seasons.
 84
 85    `year` (int):
 86        Alternative keyword for `season`
 87
 88    `return_as_dict` (bool, semi-optional):
 89        Semi-optional argument.
 90        If you want this function to return the data
 91        as a dictionary (read: JSON object),
 92        instead of a pandas `DataFrame` object,
 93        set `return_as_dict` to `True`.
 94
 95    Usage
 96    ----------
 97    ```
 98    import time
 99
100    from cfbd_json_py.coaches import get_cfbd_coaches_info
101
102    cfbd_key = "tigersAreAwesome"  # placeholder for your CFBD API Key.
103
104    if cfbd_key is not "tigersAreAwesome":
105        print(
106            "Using the user's API key declared in this script "+
107            "for this example."
108        )
109
110        # Getting all coaches in the 2020 CFB season
111        print("Getting every coach in the 2020 CFB season.")
112        json_data = get_cfbd_coaches_info(
113            api_key=cfbd_key,
114            season=2020
115        )
116        print(json_data)
117        time.sleep(5)
118
119        # Getting all coaches in the 2020 CFB season,
120        # with a first name of "Luke"
121        print(
122            "Getting every coach in the 2020 CFB season, "+
123            "with a first name of \"Luke\"."
124        )
125        json_data = get_cfbd_coaches_info(
126            api_key=cfbd_key,
127            season=2020,
128            first_name="Luke"
129        )
130        print(json_data)
131        time.sleep(5)
132
133        # Getting all coaches in the 2020 CFB season, with a last name of "Day"
134        print(
135            "Getting all coaches in the 2020 CFB season, " +
136            "with a last name of \"Day\"."
137        )
138        json_data = get_cfbd_coaches_info(
139            api_key=cfbd_key,
140            season=2020,
141            last_name="Day"
142        )
143        print(json_data)
144        time.sleep(5)
145
146        # Getting every head coach for
147        # the 2020 Southern Mississippi Golden Eagles
148        print(
149            "Getting every head coach for " +
150            "the 2020 Southern Mississippi Golden Eagles."
151        )
152        json_data = get_cfbd_coaches_info(
153            api_key=cfbd_key,
154            season=2020,
155            team="Southern Mississippi"
156        )
157        print(json_data)
158        time.sleep(5)
159
160        # Getting every head coach between the 2019 and 2022 CFB seasons
161        print("Getting every head coach between the 2019 and 2022 CFB seasons")
162        json_data = get_cfbd_coaches_info(
163            api_key=cfbd_key,
164            min_season=2019,
165            max_season=2022
166        )
167        print(json_data)
168        time.sleep(5)
169
170        # You can also tell this function to just return the API call
171        # as a Dictionary (read: JSON) object.
172        print(
173            "You can also tell this function to just return the API call " +
174            "as a Dictionary (read: JSON) object."
175        )
176        json_data = get_cfbd_coaches_info(
177            api_key=cfbd_key,
178            season=2022,
179            team="Cincinnati",
180            return_as_dict=True
181        )
182        print(json_data)
183
184    else:
185        # Alternatively, if the CFBD API key exists in this python environment,
186        # or it's been set by cfbd_json_py.utls.set_cfbd_api_token(),
187        # you could just call these functions directly,
188        # without setting the API key in the script.
189        print(
190            "Using the user's API key supposedly loaded " +
191            "into this python environment for this example."
192        )
193
194        # Getting every coach in the 2020 CFB season.
195        print("Getting every coach in the 2020 CFB season.")
196        json_data = get_cfbd_coaches_info(season=2020)
197        print(json_data)
198        time.sleep(5)
199
200
201        # Getting every coach in the 2020 CFB season,
202        # with a first name of "Luke".
203        print(
204            "Getting every coach in the 2020 CFB season, " +
205            "with a first name of \"Luke\"."
206        )
207        json_data = get_cfbd_coaches_info(
208            season=2020,
209            first_name="Luke"
210        )
211        print(json_data)
212        time.sleep(5)
213
214        # Getting every coach in the 2020 CFB season,
215        # with a last name of "Day".
216        print(
217            "Getting every coach in the 2020 CFB season, " +
218            "with a last name of \"Day\"."
219        )
220        json_data = get_cfbd_coaches_info(
221            season=2020,
222            last_name="Day"
223        )
224        print(json_data)
225        time.sleep(5)
226
227        # Getting every head coach for
228        # the 2020 Southern Mississippi Golden Eagles.
229        print(
230            "Getting every head coach for the " +
231            "2020 Southern Mississippi Golden Eagles."
232        )
233        json_data = get_cfbd_coaches_info(
234            season=2020,
235            team="Southern Mississippi"
236        )
237        print(json_data)
238        time.sleep(5)
239
240        # Getting every head coach between the 2019 and 2022 CFB seasons.
241        print(
242            "Getting every head coach between the 2019 and 2022 CFB seasons."
243        )
244        json_data = get_cfbd_coaches_info(
245            min_season=2019,
246            max_season=2022
247        )
248        print(json_data)
249        time.sleep(5)
250
251        # You can also tell this function to just return the API call
252        # as a Dictionary (read: JSON) object.
253        print(
254            "You can also tell this function to just return the API call " +
255            "as a Dictionary (read: JSON) object."
256        )
257        json_data = get_cfbd_coaches_info(
258            season=2022,
259            team="Cincinnati",
260            return_as_dict=True
261        )
262        print(json_data)
263
264    ```
265    Returns
266    ----------
267    A pandas `DataFrame` object with CFB head coach data,
268    or (if `return_as_dict` is set to `True`)
269    a dictionary object with CFB head coach data.
270    """
271    warnings.simplefilter(action="ignore", category=FutureWarning)
272
273    coaches_df = pd.DataFrame()
274    row_df = pd.DataFrame()
275    url = "https://api.collegefootballdata.com/coaches"
276
277    # Input validation
278    ##########################################################################
279
280    # `year` to `season`
281    if season is not None and year is not None and (year is not season):
282        raise ValueError(
283            "When using this function, "
284            + "please specify a season in EITHER `year` or `season`."
285        )
286    if season is not None:
287        pass
288    elif year is not None:
289        season = year
290    else:
291        raise ValueError("No year/season inputted for this function.")
292
293    if api_key is not None:
294        real_api_key = api_key
295        del api_key
296    else:
297        real_api_key = get_cfbd_api_token(api_key_dir=api_key_dir)
298
299    if real_api_key == "tigersAreAwesome":
300        raise ValueError(
301            "You actually need to change `cfbd_key` to your CFBD API key."
302        )
303    elif "Bearer " in real_api_key:
304        pass
305    elif "Bearer" in real_api_key:
306        real_api_key = real_api_key.replace("Bearer", "Bearer ")
307    else:
308        real_api_key = "Bearer " + real_api_key
309
310    if min_season is not None and max_season is None:
311        if season is not None and min_season is not season:
312            raise LookupError(
313                "It is ambiguous which year you want coaching information "
314                + "from, because you set the following values "
315                + "for the following variables:"
316                + f"\n- `year`: {season}"
317                + f"\n- `min_season`: {min_season}"
318                + f"\n`max_season`: {max_season}"
319                + "\nIf you want to get coaches who coached in "
320                + "a specified range of seasons, "
321                + "set `min_season` and `max_season` to the range of seasons "
322                + "you want coaching information from."
323            )
324        elif season is not None and min_season is season:
325            logging.warning(
326                "If you only want coaching information for a single season, "
327                + "and not for a range of seasons, only set `year` "
328                + "to the season you want coaching info for, "
329                + "and leave `min_season` and `max_season` as `None` (NULL)."
330            )
331            min_season = None
332        elif season is None:
333            logging.warning(
334                "If you only want coaching information for a single season, "
335                + "and not for a range of seasons, only set `year` to "
336                + "the season you want coaching info for, "
337                + "and leave `min_season` and `max_season` as `None` (NULL)."
338            )
339            season = min_season
340            min_season = None
341
342    elif min_season is None and max_season is not None:
343        if season is not None and max_season is not season:
344            raise LookupError(
345                "It is ambiguous which year you want "
346                + "coaching information from, "
347                + "because you set the following values "
348                + "for the following variables:"
349                + f"\n- `year`: {season}"
350                + f"\n- `min_season`: {min_season}"
351                + f"\n`max_season`: {max_season}"
352                + "\nIf you want to get coaches who coached "
353                + "in a specified range of seasons, "
354                + "set `min_season` and `max_season` to the range of seasons "
355                + "you want coaching information from."
356            )
357        elif season is not None and max_season == season:
358            logging.warning(
359                "If you only want coaching information for "
360                + "a single season, and not for a range of seasons, "
361                + "only set `year` to the season you want coaching info for, "
362                + "and leave `min_season` and `max_season` as `None` (NULL)."
363            )
364            min_season = None
365        elif season is None:
366            logging.warning(
367                "If you only want coaching information for "
368                + "a single season, and not for a range of seasons, "
369                + "only set `year` to the season you want coaching info for, "
370                + "and leave `min_season` and `max_season` as `None` (NULL)."
371            )
372            season = max_season
373            max_season = None
374
375    if min_season is not None and max_season is not None:
376        if season is not None:
377            raise LookupError(
378                "It is ambiguous which year you want "
379                + "coaching information from, because you set "
380                + "the following values for the following variables:"
381                + f"\n- `year`: {season}"
382                + f"\n- `min_season`: {min_season}"
383                + f"\n`max_season`: {max_season}"
384                + "\nIf you want to get coaches who coached in "
385                + "a specified range of seasons, "
386                + "set `min_season` and `max_season` to the range of "
387                + "seasons you want coaching information from."
388            )
389        elif min_season > max_season:
390            raise ValueError(
391                "`min_season` cannot be greater than `max_season`."
392            )
393
394    # URL builder
395    ##########################################################################
396
397    url_elements = 0
398
399    if first_name is not None and url_elements == 0:
400        url += f"?firstName={first_name}"
401        url_elements += 1
402    elif first_name is not None:
403        url += f"&firstName={first_name}"
404        url_elements += 1
405
406    if last_name is not None and url_elements == 0:
407        url += f"?lastName={last_name}"
408        url_elements += 1
409    elif last_name is not None:
410        url += f"&lastName={last_name}"
411        url_elements += 1
412
413    if team is not None and url_elements == 0:
414        url += f"?team={team}"
415        url_elements += 1
416    elif team is not None:
417        url += f"&team={team}"
418        url_elements += 1
419
420    if season is not None:
421        if season is not None and url_elements == 0:
422            url += f"?year={season}"
423            url_elements += 1
424        elif season is not None:
425            url += f"&year={season}"
426            url_elements += 1
427
428    elif min_season is not None and max_season is not None:
429        if url_elements == 0:
430            url += f"?minYear={min_season}&maxYear={max_season}"
431            url_elements += 1
432        else:
433            url += f"&minYear={min_season}&maxYear={max_season}"
434            url_elements += 1
435
436    headers = {
437        "Authorization": f"{real_api_key}", "accept": "application/json"
438    }
439
440    response = requests.get(url, headers=headers)
441
442    if response.status_code == 200:
443        pass
444    elif response.status_code == 401:
445        raise ConnectionRefusedError(
446            "Could not connect. The connection was refused.\n" +
447            "HTTP Status Code 401."
448        )
449    else:
450        raise ConnectionError(
451            f"Could not connect.\nHTTP Status code {response.status_code}"
452        )
453
454    json_data = response.json()
455
456    if return_as_dict is True:
457        return json_data
458
459    for coach in tqdm(json_data):
460        coach_first_name = coach["first_name"]
461        coach_last_name = coach["last_name"]
462        coach_hire_date = coach["hire_date"]
463
464        row_df = pd.json_normalize(coach["seasons"])
465        row_df["coach_first_name"] = coach_first_name
466        row_df["coach_last_name"] = coach_last_name
467        row_df["coach_hire_date"] = coach_hire_date
468
469        coaches_df = pd.concat([coaches_df, row_df], ignore_index=True)
470
471        del coach_first_name, coach_last_name, coach_hire_date
472    # coaches_df = pd.json_normalize(json_data)
473    return coaches_df
def get_cfbd_coaches_info( api_key: str = None, api_key_dir: str = None, first_name: str = None, last_name: str = None, team: str = None, season: int = None, min_season: int = None, max_season: int = None, year: int = None, return_as_dict: bool = False):
 19def get_cfbd_coaches_info(
 20    api_key: str = None,
 21    api_key_dir: str = None,
 22    first_name: str = None,
 23    last_name: str = None,
 24    team: str = None,
 25    season: int = None,
 26    min_season: int = None,
 27    max_season: int = None,
 28    year: int = None,
 29    return_as_dict: bool = False,
 30):
 31    """
 32    Retrieves information from the CFBD API on CFB Head Coaches.
 33
 34    Parameters
 35    ----------
 36    `api_key` (str, optional):
 37        Semi-optional argument.
 38        If `api_key` is null, this function will attempt to load a CFBD API key
 39        from the python environment, or from a file on this computer.
 40        If `api_key` is not null,
 41        this function will automatically assume that the
 42        inputted `api_key` is a valid CFBD API key.
 43
 44    `api_key_dir` (str, optional):
 45        Optional argument.
 46        If `api_key` is set to am empty string, this variable is ignored.
 47        If `api_key_dir` is null, and `api_key` is null,
 48        this function will try to find
 49        a CFBD API key file in this user's home directory.
 50        If `api_key_dir` is set to a string, and `api_key` is null,
 51        this function will assume that `api_key_dir` is a directory,
 52        and will try to find a CFBD API key file in that directory.
 53
 54    `first_name` (str, optional):
 55        Optional argument.
 56        If you want to only look up coaches with a specific first name,
 57        set this variable to that specific first name, and this function
 58        will attempt to look up coaches with that specific first name.
 59
 60    `last_name` (str, optional):
 61        Optional argument.
 62        If you want to only look up coaches with a specific last name,
 63        set this variable to that specific first name, and this function
 64        will attempt to look up coaches with that specific last name.
 65
 66    `team` (str, optional):
 67        Optional argument.
 68        If you want to filter and drill down to coaches who coached a specific
 69        CFB team, set this
 70
 71    `season` (int, optional):
 72        Optional argument.
 73        If you only want coaches from a specific season,
 74        set this variable to that season.
 75
 76    `min_season` (int, optional):
 77        Optional argument.
 78        Similar to `year`, but used in tandem with `max_season`
 79        to get coaches who coached with in a range of seasons.
 80
 81    `max_season` (int, optional):
 82        Optional argument.
 83        Similar to `year`, but used in tandem with `min_season`
 84        to get coaches who coached with in a range of seasons.
 85
 86    `year` (int):
 87        Alternative keyword for `season`
 88
 89    `return_as_dict` (bool, semi-optional):
 90        Semi-optional argument.
 91        If you want this function to return the data
 92        as a dictionary (read: JSON object),
 93        instead of a pandas `DataFrame` object,
 94        set `return_as_dict` to `True`.
 95
 96    Usage
 97    ----------
 98    ```
 99    import time
100
101    from cfbd_json_py.coaches import get_cfbd_coaches_info
102
103    cfbd_key = "tigersAreAwesome"  # placeholder for your CFBD API Key.
104
105    if cfbd_key is not "tigersAreAwesome":
106        print(
107            "Using the user's API key declared in this script "+
108            "for this example."
109        )
110
111        # Getting all coaches in the 2020 CFB season
112        print("Getting every coach in the 2020 CFB season.")
113        json_data = get_cfbd_coaches_info(
114            api_key=cfbd_key,
115            season=2020
116        )
117        print(json_data)
118        time.sleep(5)
119
120        # Getting all coaches in the 2020 CFB season,
121        # with a first name of "Luke"
122        print(
123            "Getting every coach in the 2020 CFB season, "+
124            "with a first name of \"Luke\"."
125        )
126        json_data = get_cfbd_coaches_info(
127            api_key=cfbd_key,
128            season=2020,
129            first_name="Luke"
130        )
131        print(json_data)
132        time.sleep(5)
133
134        # Getting all coaches in the 2020 CFB season, with a last name of "Day"
135        print(
136            "Getting all coaches in the 2020 CFB season, " +
137            "with a last name of \"Day\"."
138        )
139        json_data = get_cfbd_coaches_info(
140            api_key=cfbd_key,
141            season=2020,
142            last_name="Day"
143        )
144        print(json_data)
145        time.sleep(5)
146
147        # Getting every head coach for
148        # the 2020 Southern Mississippi Golden Eagles
149        print(
150            "Getting every head coach for " +
151            "the 2020 Southern Mississippi Golden Eagles."
152        )
153        json_data = get_cfbd_coaches_info(
154            api_key=cfbd_key,
155            season=2020,
156            team="Southern Mississippi"
157        )
158        print(json_data)
159        time.sleep(5)
160
161        # Getting every head coach between the 2019 and 2022 CFB seasons
162        print("Getting every head coach between the 2019 and 2022 CFB seasons")
163        json_data = get_cfbd_coaches_info(
164            api_key=cfbd_key,
165            min_season=2019,
166            max_season=2022
167        )
168        print(json_data)
169        time.sleep(5)
170
171        # You can also tell this function to just return the API call
172        # as a Dictionary (read: JSON) object.
173        print(
174            "You can also tell this function to just return the API call " +
175            "as a Dictionary (read: JSON) object."
176        )
177        json_data = get_cfbd_coaches_info(
178            api_key=cfbd_key,
179            season=2022,
180            team="Cincinnati",
181            return_as_dict=True
182        )
183        print(json_data)
184
185    else:
186        # Alternatively, if the CFBD API key exists in this python environment,
187        # or it's been set by cfbd_json_py.utls.set_cfbd_api_token(),
188        # you could just call these functions directly,
189        # without setting the API key in the script.
190        print(
191            "Using the user's API key supposedly loaded " +
192            "into this python environment for this example."
193        )
194
195        # Getting every coach in the 2020 CFB season.
196        print("Getting every coach in the 2020 CFB season.")
197        json_data = get_cfbd_coaches_info(season=2020)
198        print(json_data)
199        time.sleep(5)
200
201
202        # Getting every coach in the 2020 CFB season,
203        # with a first name of "Luke".
204        print(
205            "Getting every coach in the 2020 CFB season, " +
206            "with a first name of \"Luke\"."
207        )
208        json_data = get_cfbd_coaches_info(
209            season=2020,
210            first_name="Luke"
211        )
212        print(json_data)
213        time.sleep(5)
214
215        # Getting every coach in the 2020 CFB season,
216        # with a last name of "Day".
217        print(
218            "Getting every coach in the 2020 CFB season, " +
219            "with a last name of \"Day\"."
220        )
221        json_data = get_cfbd_coaches_info(
222            season=2020,
223            last_name="Day"
224        )
225        print(json_data)
226        time.sleep(5)
227
228        # Getting every head coach for
229        # the 2020 Southern Mississippi Golden Eagles.
230        print(
231            "Getting every head coach for the " +
232            "2020 Southern Mississippi Golden Eagles."
233        )
234        json_data = get_cfbd_coaches_info(
235            season=2020,
236            team="Southern Mississippi"
237        )
238        print(json_data)
239        time.sleep(5)
240
241        # Getting every head coach between the 2019 and 2022 CFB seasons.
242        print(
243            "Getting every head coach between the 2019 and 2022 CFB seasons."
244        )
245        json_data = get_cfbd_coaches_info(
246            min_season=2019,
247            max_season=2022
248        )
249        print(json_data)
250        time.sleep(5)
251
252        # You can also tell this function to just return the API call
253        # as a Dictionary (read: JSON) object.
254        print(
255            "You can also tell this function to just return the API call " +
256            "as a Dictionary (read: JSON) object."
257        )
258        json_data = get_cfbd_coaches_info(
259            season=2022,
260            team="Cincinnati",
261            return_as_dict=True
262        )
263        print(json_data)
264
265    ```
266    Returns
267    ----------
268    A pandas `DataFrame` object with CFB head coach data,
269    or (if `return_as_dict` is set to `True`)
270    a dictionary object with CFB head coach data.
271    """
272    warnings.simplefilter(action="ignore", category=FutureWarning)
273
274    coaches_df = pd.DataFrame()
275    row_df = pd.DataFrame()
276    url = "https://api.collegefootballdata.com/coaches"
277
278    # Input validation
279    ##########################################################################
280
281    # `year` to `season`
282    if season is not None and year is not None and (year is not season):
283        raise ValueError(
284            "When using this function, "
285            + "please specify a season in EITHER `year` or `season`."
286        )
287    if season is not None:
288        pass
289    elif year is not None:
290        season = year
291    else:
292        raise ValueError("No year/season inputted for this function.")
293
294    if api_key is not None:
295        real_api_key = api_key
296        del api_key
297    else:
298        real_api_key = get_cfbd_api_token(api_key_dir=api_key_dir)
299
300    if real_api_key == "tigersAreAwesome":
301        raise ValueError(
302            "You actually need to change `cfbd_key` to your CFBD API key."
303        )
304    elif "Bearer " in real_api_key:
305        pass
306    elif "Bearer" in real_api_key:
307        real_api_key = real_api_key.replace("Bearer", "Bearer ")
308    else:
309        real_api_key = "Bearer " + real_api_key
310
311    if min_season is not None and max_season is None:
312        if season is not None and min_season is not season:
313            raise LookupError(
314                "It is ambiguous which year you want coaching information "
315                + "from, because you set the following values "
316                + "for the following variables:"
317                + f"\n- `year`: {season}"
318                + f"\n- `min_season`: {min_season}"
319                + f"\n`max_season`: {max_season}"
320                + "\nIf you want to get coaches who coached in "
321                + "a specified range of seasons, "
322                + "set `min_season` and `max_season` to the range of seasons "
323                + "you want coaching information from."
324            )
325        elif season is not None and min_season is season:
326            logging.warning(
327                "If you only want coaching information for a single season, "
328                + "and not for a range of seasons, only set `year` "
329                + "to the season you want coaching info for, "
330                + "and leave `min_season` and `max_season` as `None` (NULL)."
331            )
332            min_season = None
333        elif season is None:
334            logging.warning(
335                "If you only want coaching information for a single season, "
336                + "and not for a range of seasons, only set `year` to "
337                + "the season you want coaching info for, "
338                + "and leave `min_season` and `max_season` as `None` (NULL)."
339            )
340            season = min_season
341            min_season = None
342
343    elif min_season is None and max_season is not None:
344        if season is not None and max_season is not season:
345            raise LookupError(
346                "It is ambiguous which year you want "
347                + "coaching information from, "
348                + "because you set the following values "
349                + "for the following variables:"
350                + f"\n- `year`: {season}"
351                + f"\n- `min_season`: {min_season}"
352                + f"\n`max_season`: {max_season}"
353                + "\nIf you want to get coaches who coached "
354                + "in a specified range of seasons, "
355                + "set `min_season` and `max_season` to the range of seasons "
356                + "you want coaching information from."
357            )
358        elif season is not None and max_season == season:
359            logging.warning(
360                "If you only want coaching information for "
361                + "a single season, and not for a range of seasons, "
362                + "only set `year` to the season you want coaching info for, "
363                + "and leave `min_season` and `max_season` as `None` (NULL)."
364            )
365            min_season = None
366        elif season is None:
367            logging.warning(
368                "If you only want coaching information for "
369                + "a single season, and not for a range of seasons, "
370                + "only set `year` to the season you want coaching info for, "
371                + "and leave `min_season` and `max_season` as `None` (NULL)."
372            )
373            season = max_season
374            max_season = None
375
376    if min_season is not None and max_season is not None:
377        if season is not None:
378            raise LookupError(
379                "It is ambiguous which year you want "
380                + "coaching information from, because you set "
381                + "the following values for the following variables:"
382                + f"\n- `year`: {season}"
383                + f"\n- `min_season`: {min_season}"
384                + f"\n`max_season`: {max_season}"
385                + "\nIf you want to get coaches who coached in "
386                + "a specified range of seasons, "
387                + "set `min_season` and `max_season` to the range of "
388                + "seasons you want coaching information from."
389            )
390        elif min_season > max_season:
391            raise ValueError(
392                "`min_season` cannot be greater than `max_season`."
393            )
394
395    # URL builder
396    ##########################################################################
397
398    url_elements = 0
399
400    if first_name is not None and url_elements == 0:
401        url += f"?firstName={first_name}"
402        url_elements += 1
403    elif first_name is not None:
404        url += f"&firstName={first_name}"
405        url_elements += 1
406
407    if last_name is not None and url_elements == 0:
408        url += f"?lastName={last_name}"
409        url_elements += 1
410    elif last_name is not None:
411        url += f"&lastName={last_name}"
412        url_elements += 1
413
414    if team is not None and url_elements == 0:
415        url += f"?team={team}"
416        url_elements += 1
417    elif team is not None:
418        url += f"&team={team}"
419        url_elements += 1
420
421    if season is not None:
422        if season is not None and url_elements == 0:
423            url += f"?year={season}"
424            url_elements += 1
425        elif season is not None:
426            url += f"&year={season}"
427            url_elements += 1
428
429    elif min_season is not None and max_season is not None:
430        if url_elements == 0:
431            url += f"?minYear={min_season}&maxYear={max_season}"
432            url_elements += 1
433        else:
434            url += f"&minYear={min_season}&maxYear={max_season}"
435            url_elements += 1
436
437    headers = {
438        "Authorization": f"{real_api_key}", "accept": "application/json"
439    }
440
441    response = requests.get(url, headers=headers)
442
443    if response.status_code == 200:
444        pass
445    elif response.status_code == 401:
446        raise ConnectionRefusedError(
447            "Could not connect. The connection was refused.\n" +
448            "HTTP Status Code 401."
449        )
450    else:
451        raise ConnectionError(
452            f"Could not connect.\nHTTP Status code {response.status_code}"
453        )
454
455    json_data = response.json()
456
457    if return_as_dict is True:
458        return json_data
459
460    for coach in tqdm(json_data):
461        coach_first_name = coach["first_name"]
462        coach_last_name = coach["last_name"]
463        coach_hire_date = coach["hire_date"]
464
465        row_df = pd.json_normalize(coach["seasons"])
466        row_df["coach_first_name"] = coach_first_name
467        row_df["coach_last_name"] = coach_last_name
468        row_df["coach_hire_date"] = coach_hire_date
469
470        coaches_df = pd.concat([coaches_df, row_df], ignore_index=True)
471
472        del coach_first_name, coach_last_name, coach_hire_date
473    # coaches_df = pd.json_normalize(json_data)
474    return coaches_df

Retrieves information from the CFBD API on CFB Head Coaches.

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.

first_name (str, optional): Optional argument. If you want to only look up coaches with a specific first name, set this variable to that specific first name, and this function will attempt to look up coaches with that specific first name.

last_name (str, optional): Optional argument. If you want to only look up coaches with a specific last name, set this variable to that specific first name, and this function will attempt to look up coaches with that specific last name.

team (str, optional): Optional argument. If you want to filter and drill down to coaches who coached a specific CFB team, set this

season (int, optional): Optional argument. If you only want coaches from a specific season, set this variable to that season.

min_season (int, optional): Optional argument. Similar to year, but used in tandem with max_season to get coaches who coached with in a range of seasons.

max_season (int, optional): Optional argument. Similar to year, but used in tandem with min_season to get coaches who coached with in a range of seasons.

year (int): Alternative keyword for season

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.coaches import get_cfbd_coaches_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."
    )

    # Getting all coaches in the 2020 CFB season
    print("Getting every coach in the 2020 CFB season.")
    json_data = get_cfbd_coaches_info(
        api_key=cfbd_key,
        season=2020
    )
    print(json_data)
    time.sleep(5)

    # Getting all coaches in the 2020 CFB season,
    # with a first name of "Luke"
    print(
        "Getting every coach in the 2020 CFB season, "+
        "with a first name of "Luke"."
    )
    json_data = get_cfbd_coaches_info(
        api_key=cfbd_key,
        season=2020,
        first_name="Luke"
    )
    print(json_data)
    time.sleep(5)

    # Getting all coaches in the 2020 CFB season, with a last name of "Day"
    print(
        "Getting all coaches in the 2020 CFB season, " +
        "with a last name of "Day"."
    )
    json_data = get_cfbd_coaches_info(
        api_key=cfbd_key,
        season=2020,
        last_name="Day"
    )
    print(json_data)
    time.sleep(5)

    # Getting every head coach for
    # the 2020 Southern Mississippi Golden Eagles
    print(
        "Getting every head coach for " +
        "the 2020 Southern Mississippi Golden Eagles."
    )
    json_data = get_cfbd_coaches_info(
        api_key=cfbd_key,
        season=2020,
        team="Southern Mississippi"
    )
    print(json_data)
    time.sleep(5)

    # Getting every head coach between the 2019 and 2022 CFB seasons
    print("Getting every head coach between the 2019 and 2022 CFB seasons")
    json_data = get_cfbd_coaches_info(
        api_key=cfbd_key,
        min_season=2019,
        max_season=2022
    )
    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_coaches_info(
        api_key=cfbd_key,
        season=2022,
        team="Cincinnati",
        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."
    )

    # Getting every coach in the 2020 CFB season.
    print("Getting every coach in the 2020 CFB season.")
    json_data = get_cfbd_coaches_info(season=2020)
    print(json_data)
    time.sleep(5)


    # Getting every coach in the 2020 CFB season,
    # with a first name of "Luke".
    print(
        "Getting every coach in the 2020 CFB season, " +
        "with a first name of "Luke"."
    )
    json_data = get_cfbd_coaches_info(
        season=2020,
        first_name="Luke"
    )
    print(json_data)
    time.sleep(5)

    # Getting every coach in the 2020 CFB season,
    # with a last name of "Day".
    print(
        "Getting every coach in the 2020 CFB season, " +
        "with a last name of "Day"."
    )
    json_data = get_cfbd_coaches_info(
        season=2020,
        last_name="Day"
    )
    print(json_data)
    time.sleep(5)

    # Getting every head coach for
    # the 2020 Southern Mississippi Golden Eagles.
    print(
        "Getting every head coach for the " +
        "2020 Southern Mississippi Golden Eagles."
    )
    json_data = get_cfbd_coaches_info(
        season=2020,
        team="Southern Mississippi"
    )
    print(json_data)
    time.sleep(5)

    # Getting every head coach between the 2019 and 2022 CFB seasons.
    print(
        "Getting every head coach between the 2019 and 2022 CFB seasons."
    )
    json_data = get_cfbd_coaches_info(
        min_season=2019,
        max_season=2022
    )
    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_coaches_info(
        season=2022,
        team="Cincinnati",
        return_as_dict=True
    )
    print(json_data)

Returns

A pandas DataFrame object with CFB head coach data, or (if return_as_dict is set to True) a dictionary object with CFB head coach data.