# devtools::install_github("stillmatic/leagueR")
pacman::p_load(dplyr, purrr, ggplot2, rvest, stringr)
key <- "RGAPI-e7463574-6c7b-42f1-9fe1-b649dcd08720"
#leagueR::set_riot_key(key)
end <- "lol/summoner/v3/summoners/by-name/favstats"
api_url <- "https://euw1.api.riotgames.com/"


get_content <- function(endpoint){
  endpoint %>% 
    paste0(api_url, .) %>%
    paste0(., "?api_key=") %>% 
    paste0(., key) %>% 
    httr::GET()%>%
    httr::content(., "text") %>%
    jsonlite::fromJSON(.)
}

get_content(end)

gameid <- 2898388927

get_match <- function(gameid){
  get_content(paste0("lol/match/v3/matches/", gameid))
}

ch <- get_match("2898388927")

names(ch)

ch$gameId

ch$platformId

ch$gameCreation

ch$gameDuration

ch$queueId

ch$mapId

ch$seasonId

ch$gameVersion

ch$gameMode

ch$gameType

ch$teams %>% glimpse

ch$participants %>% glimpse

### not interesting
#ch$participantIdentities %>% glimpse

is the gamid random? plot

  1. Get all player ids from existing
  2. Get one player from each team
match_stats <- dir("data/raw/") %>%
  stringr::str_detect("matches") %>%
  dir("data/raw/")[.] %>%
  paste0("data/raw/", .) %>%
  as.list() %>%
  purrr::map(
    ~ openxlsx::read.xlsx(.x, sheet = 1) %>% 
        mutate_all(as.character)
  ) %>%
  purrr::reduce(bind_rows) %>% 
  as_tibble()
save(match_stats, file = "data/match_stats.Rdata")
match_stats %>% glimpse

match_stats %>% 

    select(gameid, team, player, playerid)

match_stats %>% 
  count(team)


top_player <- match_stats %>% 
  #group_by(team) %>%
  count(player) %>% 
  arrange(desc(n)) %>% 
  #slice(1) %>% 
  ungroup()

top_player
key <- "RGAPI-55447a64-cd9b-4063-979e-346eb6b5c475"

end <- "lol/summoner/v3/summoners/by-name/kaizen"
dnn <- get_content(end)

get_summoner_id <- function(name){
  get_content(paste0("lol/summoner/v3/summoners/by-name/", name))
}
dat <- get_summoner_id("Mystic")


extract_ids <- function(x){
  dat <- get_summoner_id(x) 

  out <- tibble(
    id = dat$id, 
    accountId = dat$accountId, 
    player = dat$name
  )

  Sys.sleep(1.3) # 120/100 Rate Limit

  return(out)
}

safe_extract <- safely(extract_ids)
safe_extract_progress <- tidyMBO::progressively(safe_extract, nrow(rest))

player_ids <- rest$player_org %>% 
  as.list() %>% 
  purrr::map(safe_extract_progress) 


player_data3 <- player_ids %>% 
  purrr::map("result") %>% 
  reduce(bind_rows)

save(player_data3, file = "data/player_data3.Rdata")

player_all <- bind_rows(player_data, player_data2, player_data3) %>%
  filter(!is.na(id)) %>%
  filter(!duplicated(id)) %>% 
  mutate(player = str_to_lower(player)) 

all <- top_player %>%
  mutate(player = str_to_lower(player)) %>% 
  left_join(player_all, by = c("player")) %>%
  filter(!is.na(id))

# save(all, file = "player_ids_all.Rdata")

rest <- top_player %>%
  mutate(player_org = player) %>% 
  mutate(player = str_to_lower(player)) %>% 
  left_join(player_all, by = c("player")) %>%
  filter(is.na(id))


player_ids %>% 
  map("error") 
team_player <- match_stats %>%
  filter(player != "Team") %>%
  mutate(player = str_to_lower(player)) %>% 
  select(team, player) %>% 
  left_join(player_all, by = c("player")) %>%
  filter(!is.na(accountId)) %>%
  group_by(team) %>% 
  count(player) %>%
  arrange(desc(n)) %>%
  slice(1) %>% 
  ungroup() %>%  
  select(-n) %>%  
  left_join(all, by = "player")

#save(team_player, file = "data/team_player.Rdata")
get_summoner_matches <- function(accid){
  Sys.sleep(.5)
  get_content(paste0("/lol/match/v3/matchlists/by-account/", accid))
}


matchlist_progress <- tidyMBO::progressively(get_summoner_matches, nrow(tplayer))


tplayer <- team_player %>% 
  select(accountId) %>% 
  unique() 

gamelist <- tplayer$accountId %>% 
  purrr::map(matchlist_progress)

glist <- gamelist %>% 
  map_df("matches") %>% 
  as_tibble() %>% 
  filter(!duplicated(gameId))

ww <- "221175489" %>% get_summoner_matches
ww$matches %>% as_tibble
names(ww)
ww$totalGames
get_content2 <- function(endpoint){
  endpoint %>% 
    paste0(api_url, .) %>%
    paste0(., "&api_key=") %>% 
    paste0(., key) %>% 
    httr::GET()%>%
    httr::content(., "text") %>%
    jsonlite::fromJSON(.)
}

get_summoner_matches <- function(accid){
  get_content(paste0("/lol/match/v3/matchlists/by-account/", accid))
}
nn <- get_summoner_matches(232060798)
options(scipen = 999)
nn$matches[1,] %>% as_tibble() %>% .$gameId
get_champions <- function(){
  get_content("/lol/platform/v3/champions/")
}
get_champions()

get_champion <- function(champid){
  get_content(paste0("/lol/platform/v3/champions/", champid))
}

get_champion(266)

Getting matches

get_match <- function(gameid){

  out <- get_content(paste0("lol/match/v3/matches/", gameid))
  print(out$gameId)
  Sys.sleep(1.2)
  return(out)
}      

matchdetails_progress <- tidyMBO::progressively(get_match, nrow(glist))

gamedetails <- glist$gameId %>% 
  purrr::map(matchdetails_progress)

gamedetails[[1]]
names(gamedetails[[1]])

# save(gamedetails, file = "data/gamedetails_raw.Rdata")

unnesting shit

clean_matches <- function(x) {

  match_base <- x[1:10] %>% 
    as_tibble() %>% 
    janitor::clean_names() %>% 
    mutate(date = anytime(game_creation/1000))

  match_details <- x$teams %>% 
    janitor::clean_names() %>% 
    split(.$team_id) %>% 
    map(~{
      if(.x$team_id == 100){
        colnames(.x) <- paste0("blue_", colnames(.x))
        return(.x)
      } else {
        colnames(.x) <- paste0("red_", colnames(.x))
        return(.x)
      }
    }) %>% 
    reduce(bind_cols)

  match_part <- x$participants[1:5] %>% 
    as_tibble() %>% 
    ifelse(is.null(.), NA, .) %>% 
    list() %>%
    tibble(match_part = .)

  match_part_details <- x$participants$stats %>% 
    as_tibble() %>% 
    mutate(win = ifelse(win, "Win", "Fail")) %>% 
    ifelse(is.null(.), NA, .) %>% 
    list() %>%
    tibble(match_part_details = .)

  dat <- match_base %>% 
    bind_cols(
      match_details, 
      match_part, 
      match_part_details
    ) 

  return(dat)
}

x <- gamedetails[[1]]
clean_matches(x)

# ss <- nn$participants$timeline %>% 
#   janitor::clean_names() 
# 
# list_names1 <- names(ss)[c(2:3)]
# list_names2 <- names(ss)[c(5)]
# list_names3 <- names(ss)[c(8)]
# 
# unnest_it <- function(x, names) {
# x %>% 
#   janitor::clean_names() %>% 
#   as_tibble() %>% 
#   set_names(~paste0(names, .))
# }
# 
# deltas <- #bind_cols(
# ss[c(2:3)] %>% 
#   as.list() %>% 
#   purrr::map(~unnest_it(
#     x = .x,
#     names = list_names1)) %>% 
#   bind_cols() %>% 
# 
# # ss[5] %>% 
# #   as.list() %>% 
# #   purrr::map(~unnest_it(
# #     x = .x,
# #     names = list_names2)) %>% 
# #   bind_cols(),
# 
# # ss[8] %>% 
# #   as.list() %>% 
# #   purrr::map(~unnest_it(
# #     x = .x,
# #     names = list_names3)) %>% 
# #   bind_cols()
# #) %>% 
#   add_rownames("participantId") %>% 
#   mutate(participantId = as.integer(participantId)) %>%
#   left_join(ss %>% 
#               select(participantId = participant_id,
#                      role, lane))

getting it together

clean_matches_safely <- purrr::safely(clean_matches)
gdetails <- gamedetails[1:768] %>% 
  purrr::map(clean_matches_safely)

gdetails %>% 
  map("result") 

gdetails %>% 
  map("result") %>% 
  reduce(bind_rows)


#nn$participantIdentities %>% glimpse
library(anytime)
anytime(1525900731766/1000)

scrape

url <- "http://www.gamesoflegends.com/game/gameshow.php?id=11650"
html <- url %>% 
  xml2::read_html()

blue_team <- html %>% 
  rvest::html_node(".blue_line a") %>% 
  html_text()

red_team <- html %>% 
  rvest::html_node(".red_line a") %>% 
  html_text()


html %>% 
  rvest::html_nodes(".black_link") %>% 
  html_text() %>% 
  .[.!=""] %>% 
  tibble(player = .) %>% 
  split(c(rep("blue",5), rep("red", 5)))


systats/lolR documentation built on May 31, 2019, 6:17 p.m.