wiscostret / fplscrapR

This package enables those interested in Fantasy Premier League to perform detailed data analysis of the game, using the FPL's JSON API. The fplscrapR functions help R users collect and parse data from the Official Fantasy Premier League website.
Creative Commons Zero v1.0 Universal
76 stars 16 forks source link

Function request: get_player_ids #21

Closed py9mrg closed 3 years ago

py9mrg commented 3 years ago

Hello,

Thanks for this terrific package. One small feature request - a function that can make a list of all player ids (or names - but these aren't always unique - e.g. Ben Davies) for the current season.

get_player_details works great if you have the id or name, as does get_player_id if you know the player name - so what would be great is something like get_player_ids that returns all the player ids (or at least the maximum one, given they're assigned sequentially per season). Then we can do something like:

player_ids <- get_player_ids
*apply / purrr:map_*(player_ids, get_player_details)

to automatically get the details for all players at once. Currently, if I want to do that, I have to do a binary search on the player id until I get the maximum id get_player_details will accept without throwing an error.

wiscostret commented 3 years ago

Hi there, thank you for this comment, and I agree with the conceptual idea. I will include this in the next package update, likely ahead of the new season in August. Or, if you are willing, you can submit a pull request with the functionality, then I will review and merge. Thanks again.

py9mrg commented 3 years ago

Hello, thanks for the reply. Have had a quick look at the code and this seems pretty easy depending how much functionality you want to make inherent to the package. For example, in player_functions.R the function get_player_id has the line(s):

elements <- jsonlite::fromJSON("https://fantasy.premierleague.com/api/bootstrap-static/")$elements

I'm completely new to JSON and the FPL API so I didn't realise that that returns the info for all players for the current GW. That then means that nrow(elements) will return the total number of players - and this can then be used in my example above. e.g. something like:

# I haven't actually run this so may not work as is, but roughly this:
max_id <- nrow(jsonlite::fromJSON("https://fantasy.premierleague.com/api/bootstrap-static/")$elements)
purrr::map_dfr(1:max_id, get_player_details)

As an aside - I really don't understand what FPL are doing as their API is confusing to me. For example, elements above contains completely different columns as get_player_details (which uses $history not $elements from the FPL API) - I mean, why don't they just include all columns in both so elements and history match? Aaaaanyway.

But this is where the questions come of how you want to do this.

wiscostret commented 3 years ago

We are now live for the 21-22 season and the function update includes a new function - get_player_ids() - which returns a data frame of all player ids and names. Should make things easier to combine datasets and work with the API data. Thanks for the suggestion!