Download pool/shard008/Python/0071486_fpl_api.py from Variable-role/sajaniemi_variable_dataset_large: direct link, hf CLI and curl.
- Browser
- Download file 1.61 kB
-
https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/pool/shard008/Python/0071486_fpl_api.py
- Command line
-
hf download hf://datasets/Variable-role/sajaniemi_variable_dataset_large/pool/shard008/Python/0071486_fpl_api.py
-
curl -L -o 0071486_fpl_api.py https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/pool/shard008/Python/0071486_fpl_api.py
1.61 kB
| import requests | |
| class FantasyPremierLeagueApi(object): | |
| FPL_API_ADDRESS = "https://fantasy.premierleague.com/drf/{endpoint}" | |
| FULL_DATA_ENDPOINT = "bootstrap-static" | |
| PLAYER_DATA_ENDPOINT = "element-summary/{id}" | |
| cached_full_api_response = None | |
| def _get_full_api_data(cls): | |
| """ | |
| Gets the main API response the FPL site provides. | |
| Will be cached for the length of the program command | |
| """ | |
| if cls.cached_full_api_response is not None: | |
| return cls.cached_full_api_response | |
| else: | |
| response = requests.get(cls.FPL_API_ADDRESS.format(endpoint=cls.FULL_DATA_ENDPOINT)) | |
| cls.cached_full_api_response = response.json() | |
| return response.json() | |
| def get_cutdown_player_data(cls): | |
| api_data = cls._get_full_api_data() | |
| return api_data['elements'] | |
| def get_team_name_mapping(cls): | |
| api_data = cls._get_full_api_data() | |
| team_names = {} | |
| for team in api_data['teams']: | |
| team_names[team['code']] = team['name'] | |
| return team_names | |
| def get_full_player_data(cls, cutdown_player_data: dict): | |
| player_endpoint = cls.FPL_API_ADDRESS.format(endpoint=cls.PLAYER_DATA_ENDPOINT) | |
| response = requests.get(player_endpoint.format(id=cutdown_player_data['id'])) | |
| if response.status_code != 200: | |
| raise Exception("Failed to get full player data") | |
| player_data = response.json() | |
| player_data.update(cutdown_player_data) | |
| return player_data | |