Advertisement
Guest User

Ottoneu Ownership

a guest
Jun 20th, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.19 KB | None | 0 0
  1. # install.packages(c('RCurl', 'rjson'))
  2.  
  3. options(stringsAsFactors = FALSE)
  4.  
  5. ottoOwn <- function(position = c('CA','1B','2B','3B','SS','OF','SP','RP'),
  6.                     MajMin = c('both','majors','minors'),
  7.                     leagueId = 756
  8. ) {
  9.   p <- match.arg(position)
  10.   l <- match.arg(MajMin)
  11.   stopifnot(length(p) == 1, length(l) == 1)
  12.  
  13.   url <- sprintf("https://ottoneu.fangraphs.com/%d/ajax/search", leagueId)
  14.   r <- RCurl::postForm(url,
  15.     'data[txtSearch]' = '',
  16.     'data[selPos][]' = p,
  17.     'data[chkMinors]' = 'yes',
  18.     'data[chkFAOnly]' = '',
  19.     'data[searchFilter][]' = 'OWNPCT',
  20.     'data[searchComparison][]' = 'gt',
  21.     'data[searchQualification][]' = 1,
  22.     style = 'post'
  23.   )
  24.   j <- rjson::fromJSON(r)
  25.   type <- c('batterResults','pitcherResults')[1 + (p %in% c('SP','RP'))]
  26.   b <- j[[type]]
  27.  
  28.   cols <- c('PlayerName', 'TeamName', 'ProTeam', 'Positions', 'OwnershipPct', 'Points', 'PointsRate')
  29.   c1 <- lapply(cols, function(col) vapply(b, function(i) as.character(i[[col]]), character(1)))
  30.   c1df <- as.data.frame(c1, col.names = cols)
  31.  
  32.   ocols <- c('ThirtyDayAddChange', 'ThirtyDayCutChange', 'SevenDayAddChange', 'SevenDayCutChange')
  33.   c2 <- lapply(ocols, function(col) vapply(b, function(i) i[['OwnershipChanges']][[col]], numeric(1)))
  34.   c2df <- as.data.frame(c2, col.names = ocols)
  35.  
  36.   df <- cbind(c1df, c2df)
  37.   if(l != 'both') {
  38.     select <- grepl("\\(", df[['ProTeam']])
  39.     if(l == 'majors') select <- !select
  40.     df <- df[select,]
  41.   }
  42.   df <- df[order(df[['OwnershipPct']], decreasing = TRUE),]
  43.   row.names(df) <- NULL
  44.   df
  45. }
  46.  
  47. topAdds <- function(x, by = 7) {
  48.   col <- c('ThirtyDayAddChange','SevenDayAddChange')[match(by, c(30, 7))]
  49.   stopifnot(!is.na(col))
  50.   x <- x[order(x[[col]], decreasing = TRUE),]
  51. }
  52.  
  53. topCuts <- function(x, by = 7) {
  54.   col <- c('ThirtyDayCutChange','SevenDayCutChange')[match(by, c(30, 7))]
  55.   stopifnot(!is.na(col))
  56.   x <- x[order(x[[col]], decreasing = TRUE),]
  57. }
  58.  
  59. oS <- ottoOwn('SP')
  60. oS1 <- ottoOwn('SP', 'majors')
  61. oS2 <- ottoOwn('SP', 'minors')
  62. # last 7 days
  63. topAdds(oS2)[1:10,]
  64. topCuts(oS2)[1:10,]
  65. # last 30 days
  66. topAdds(oS2, 30)[1:10,]
  67. topCuts(oS2, 30)[1:10,]
  68.  
  69. write.csv(oS, file = 'all-sp.csv', row.names = FALSE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement