Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # install.packages(c('RCurl', 'rjson'))
- options(stringsAsFactors = FALSE)
- ottoOwn <- function(position = c('CA','1B','2B','3B','SS','OF','SP','RP'),
- MajMin = c('both','majors','minors'),
- leagueId = 756
- ) {
- p <- match.arg(position)
- l <- match.arg(MajMin)
- stopifnot(length(p) == 1, length(l) == 1)
- url <- sprintf("https://ottoneu.fangraphs.com/%d/ajax/search", leagueId)
- r <- RCurl::postForm(url,
- 'data[txtSearch]' = '',
- 'data[selPos][]' = p,
- 'data[chkMinors]' = 'yes',
- 'data[chkFAOnly]' = '',
- 'data[searchFilter][]' = 'OWNPCT',
- 'data[searchComparison][]' = 'gt',
- 'data[searchQualification][]' = 1,
- style = 'post'
- )
- j <- rjson::fromJSON(r)
- type <- c('batterResults','pitcherResults')[1 + (p %in% c('SP','RP'))]
- b <- j[[type]]
- cols <- c('PlayerName', 'TeamName', 'ProTeam', 'Positions', 'OwnershipPct', 'Points', 'PointsRate')
- c1 <- lapply(cols, function(col) vapply(b, function(i) as.character(i[[col]]), character(1)))
- c1df <- as.data.frame(c1, col.names = cols)
- ocols <- c('ThirtyDayAddChange', 'ThirtyDayCutChange', 'SevenDayAddChange', 'SevenDayCutChange')
- c2 <- lapply(ocols, function(col) vapply(b, function(i) i[['OwnershipChanges']][[col]], numeric(1)))
- c2df <- as.data.frame(c2, col.names = ocols)
- df <- cbind(c1df, c2df)
- if(l != 'both') {
- select <- grepl("\\(", df[['ProTeam']])
- if(l == 'majors') select <- !select
- df <- df[select,]
- }
- df <- df[order(df[['OwnershipPct']], decreasing = TRUE),]
- row.names(df) <- NULL
- df
- }
- topAdds <- function(x, by = 7) {
- col <- c('ThirtyDayAddChange','SevenDayAddChange')[match(by, c(30, 7))]
- stopifnot(!is.na(col))
- x <- x[order(x[[col]], decreasing = TRUE),]
- }
- topCuts <- function(x, by = 7) {
- col <- c('ThirtyDayCutChange','SevenDayCutChange')[match(by, c(30, 7))]
- stopifnot(!is.na(col))
- x <- x[order(x[[col]], decreasing = TRUE),]
- }
- oS <- ottoOwn('SP')
- oS1 <- ottoOwn('SP', 'majors')
- oS2 <- ottoOwn('SP', 'minors')
- # last 7 days
- topAdds(oS2)[1:10,]
- topCuts(oS2)[1:10,]
- # last 30 days
- topAdds(oS2, 30)[1:10,]
- topCuts(oS2, 30)[1:10,]
- write.csv(oS, file = 'all-sp.csv', row.names = FALSE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement