stattleship / stattleship-r

Stattleship R Wrapper
https://api.stattleship.com/
MIT License
48 stars 14 forks source link

Issue 7 #14

Closed Btibert3 closed 8 years ago

Btibert3 commented 8 years ago

This pull request starts to add some baseline helper functions, but before going too far on syntax and approach, I wanted to get some baseline started.

Assuming that this checks out on your tests, we should talk about how to attack the utility functions in a consistent way across sports to keep the syntax as easy as possible. For example, how much work do we want to do in order to simplify the results?

feats (not yet implemented) is a good example where design decisions might impact how we go about this down the road. On some of the functions, I allow the user to filter on a team, but all teams could be passed with team_id="", which I know that isn't ideal.

One thing to consider: on the teams, I am not merging any of the sideloaded data (e.g. divison), but that is something that we might want to do for ease of use and adoption.

Most importantly, I am fixing a bug where not all results were returned when walk=TRUE was set and paged through results. The functions would walk the data properly, but the results of pages 2+ were basically being ignored by accident.

batpigandme commented 8 years ago

I've run a few feats with your testing function, they're fine until dataframe conversion, but working on that right now.

Btibert3 commented 8 years ago

This works as expected for me (on the issue7 branch). The feats come back as a dataframe below.

options(stringsAsFactors=F)
library(stattleshipR)
library(dplyr)
## token
set_token("token")

## what are the teams
teams <- ss_get_result(ep = "teams", walk=TRUE)
length(teams)
teams[[1]]$teams

## i dont want to know the messages
rm(teams)
teams <- ss_get_result(ep = "teams", walk=TRUE, verbose=FALSE)
length(teams)
teams[[1]]$teams

## doesn't work if upper case, but the code will put it to lower
rm(teams)
teams <- ss_get_result(sport="HOCKEY", ep="teams", walk=TRUE, verbose=FALSE)
length(teams)
teams[[1]]$teams

## return a list of lists, one is the response, the other parsed -- not an option on ss_get_result
# teams <- ss_get_result(ep = "teams", walk=TRUE, verbose=TRUE, debug=TRUE)
# names(teams)

## games
games <- ss_get_result(ep="games", query = list(team_id="nhl-bos"), walk=TRUE)
bos_games <- games[[1]]$games
bos_games <- filter(bos_games, interval_type=="regularseason")
nrow(bos_games)

## injuries
injuries <- ss_get_result(ep="injuries", query = list(team_id="nhl-bos"), walk=TRUE)
injuries <- injuries[[1]]$injuries
filter(injuries, status != "questionable") %>% select(.,  note)

## feats -- the loop will be slower
tmp_slug <- bos_games$slug[6]
tmp_feats <- ss_get_result(ep="feats", query=list(game_id=tmp_slug), walk=TRUE)
x = do.call("rbind", lapply(tmp_feats, function(x) x$feats))
head(x)
colnames(x)
str(x)
tbl_df(x) %>% 
  group_by(humanized_stat_type, level) %>% 
  summarise(total_feats = length(vs)) %>% 
  arrange(desc(total_feats))
tcash21 commented 8 years ago

Just a few minor things: in the hockey_* scripts, need to update the README to change setToken to set_token. Update main README as well. On line 30-35 or so in all of them, change '=' to '<-', otherwise, :+1:

Btibert3 commented 8 years ago

Awesome, thanks. I will make the changes and push