knitr::opts_chunk$set(echo = TRUE)

pacman::p_load(tidyverse, furrr)#, fifaindex)
devtools::load_all()

To do

#devtools::install_github(benjaminguinaudeau/fifaindex)
pacman::p_load(tidyverse, furrr, fifaindex)
plan(multiprocess, workers = 2)

player <- get_player_stats(player_id = 214977, year = 2019) %>%
  glimpse
player <- get_player_stats(player_id = 229659, year = 2019) %>%
  glimpse
player_16 <- get_player_stats(player_id = 229659) %>%
  glimpse

player_16 %>%
  unnest(data)

ids <- fifaindex::ids
sample <- ids %>%
  sample(10)

sample_stats <- sample %>%
  map(~{get_player_stats(.x, 2015:2019)},.progress = T)
#future_map(get_player_stats, .progress = T)

sample_stats  %>%
  reduce(bind_rows) %>%
  unnest(data)
plan(multiprocess, workers = 2)

get_all_existing_ids <- function(potential_ids = 1:250000){

  all_ids <- tibble::tibble(potential_ids) %>%
    #slice(1:20) %>%
    mutate(url = glue::glue("https://www.fifaindex.com/player/{ potential_ids }/")) %>%
    mutate(exists = url %>% future_map_lgl(~{
      suppressWarnings(rvest::html_session(.x)$response$status_code != 404)
    }, .progress = T)) %>%
    select(-url)

  return(all_ids)
}

all_ids <- get_all_existing_ids(potential_ids = 200001:210000)
save(all_ids, file = "player_ids_200001_210000.Rdata")
#load("data/league/fifa_league_data.Rdata")
plan(multiprocess)

player_ids <- dir(".", pattern = "player_ids") %>%
  map_dfr(~{get(load(.x))})

ids <- player_ids %>%
  filter(exists) %>%
  #sample_n(10) %>%
  select(player_id = potential_ids) %>%
  mutate(index = 1:n() %/% 500) %>%
  mutate(file = paste0("data/player/player_stats_", index, ".Rdata")) %>%
  split(.$index)

ids %>%
  .[21:30] %>%
  map(~{
    tmp <- .x %>%
      #slice(61) %>%
      split(1:nrow(.)) %>%
      future_imap_dfr(~{
        #cat(.y)
        #cat("\n")
        suppressMessages(get_player_stats(.x$player_id, year = 2015:2019))
      }, .progress = T)
    save(tmp, file = unique(.x$file))
  })

player_ids <- dir(".", pattern = "player_ids") %>%
  map_dfr(~{get(load(.x))})

sample <- player_ids %>%
  filter(exists) %>%
  pull(potential_ids) %>%
  sample(10)

sample_stats <- sample %>%
  map(~{get_player_stats(.x, year = 2015:2019)})

save(sample_stats, file = "data/player/sample.Rdata")
load("data/player/sample.Rdata")

fifa_player <- sample_stats %>%
  reduce(bind_rows) %>%
  unnest(data) %>%
  transmute(player_id, player_name,
            year = str_extract(date, "\\d{4}"), 
         birth_date = parse_date(birth_date), 
         team_id = str_extract(team_link, "(?<=/)\\d+(?=/)"), 
         kit = kit_number)
load("data/player/lineups.Rdata")
player_team_dict <- lineups %>%
  distinct(team_id, player_id)
load("data/player/sample_players.Rdata")



player_team <- sample_player %>%
  select(-team_id) %>%
 left_join(player_team_dict) %>%
  glimpse
load("data/league/fifa_monks_team_dic.Rdata")

fifa_sample <- fifa_player %>%
  rename(fifa_player_id = player_id) %>%
  filter(birth_date %in% sample_player$bday) %>%
  mutate(team_id = as.numeric(team_id)) %>%
  left_join(fifa_monks_team_dic, by = c("team_id" = "fifa_team_id")) %>%
  left_join(player_team, by = c("birth_date" = "bday", 
                                "monks_team_id" = "team_id")) %>%
  glimpse

fifa_sample %>%
  select(fifa_player_id, monks_player_id = player_id, 
         fifa_league_id, monks_league_id, 
         fifa_team_id = team_id, monks_team_id) %>%
  glimpse


benjaminguinaudeau/fifaindex documentation built on June 6, 2019, 12:22 p.m.