Skip to content

Add projected points to ff_starters.espn_conn() #323

@scottfrechette

Description

@scottfrechette

I'm not sure about the other leagues, but would you be open to adding projected points to ff_starters() for ESPN leagues?

Here's an example by simply adding a few lines of code to hoist and parse "stats" within "player":

library(tidyverse)
library(ffscrapr)
sucioboys <- espn_connect(season = 2020, league_id = 899513)

.espn_week_starter <- function(week, conn) {
  url_query <- glue::glue(
    "https://fantasy.espn.com/apis/v3/games/ffl/seasons/",
    "{conn$season}/segments/0/leagues/{conn$league_id}",
    "?scoringPeriodId={week}&view=mMatchupScore&view=mBoxscore&view=mSettings&view=mRosterSettings"
  )
  
  week_scores <- espn_getendpoint_raw(conn, url_query) %>%
    purrr::pluck("content", "schedule") %>%
    tibble::tibble() %>%
    purrr::set_names("x") %>%
    tidyr::hoist(1, "week" = "matchupPeriodId", "home", "away") %>%
    dplyr::filter(.data$week == .env$week) %>%
    tidyr::pivot_longer(c(.data$home, .data$away), names_to = NULL, values_to = "team") %>%
    tidyr::hoist("team", "starting_lineup" = "rosterForCurrentScoringPeriod", "franchise_id" = "teamId") %>%
    dplyr::select(-"team", -"x")
  
  week_scores <- week_scores %>%
    tidyr::hoist("starting_lineup", "franchise_score" = "appliedStatTotal", "entries") %>%
    tidyr::unnest_longer("entries") %>%
    tidyr::hoist("entries", "player_id" = "playerId", "lineup_id" = "lineupSlotId", "player_data" = "playerPoolEntry", ) %>%
    tidyr::hoist("player_data", "player_score" = "appliedStatTotal", "player") %>%
    dplyr::select(-"player_data") %>%
    tidyr::hoist("player", "eligible_lineup_slots" = "eligibleSlots", "player_name" = "fullName", "pos" = "defaultPositionId", "team" = "proTeamId", "stats") %>% # add stats
    dplyr::select(-"player") %>% 
    # stats has two unnamed list elements
    unnest_wider("stats", names_sep = "_") %>% 
    # here is what I assume to be projected points based on a few players I looked up historically
    hoist("stats_2", "player_projected" = "appliedTotal") %>% 
    select(-"stats_1", -"stats_2")
  
  return(week_scores)
}

.espn_week_starter(week = 1, conn = sucioboys) %>% 
  glimpse()
#> Rows: 220
#> Columns: 11
#> $ week                  <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ~
#> $ franchise_score       <dbl> 130.50, 130.50, 130.50, 130.50, 130.50, 130.50, ~
#> $ player_id             <int> 3045147, 13994, 15795, 4035004, 3045144, 3121422~
#> $ lineup_id             <int> 2, 0, 4, 20, 20, 23, 20, 16, 2, 7, 20, 4, 20, 20~
#> $ player_score          <dbl> 2.7, 25.7, 22.1, 1.5, 5.3, 8.6, 0.0, 2.9, 14.6, ~
#> $ eligible_lineup_slots <list> [2, 3, 23, 7, 20, 21], [0, 7, 20, 21], [3, 4, 5~
#> $ player_name           <chr> "James Conner", "Cam Newton", "DeAndre Hopkins",~
#> $ pos                   <int> 2, 1, 3, 3, 3, 3, 2, 16, 2, 1, 3, 3, 2, 3, 2, 3,~
#> $ team                  <int> 23, 17, 22, 12, 4, 28, 21, 25, 10, 1, 8, 2, 8, 1~
#> $ player_projected      <dbl> 14.639026, 18.069950, 12.743296, 7.188414, 9.346~
#> $ franchise_id          <int> 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, ~

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature 🌟a feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions