Skip to content

Instantly share code, notes, and snippets.

@tcash21
Created March 23, 2018 15:53
Show Gist options
  • Save tcash21/36ec73e133c6793c76de6fee4c7fa5be to your computer and use it in GitHub Desktop.
Save tcash21/36ec73e133c6793c76de6fee4c7fa5be to your computer and use it in GitHub Desktop.
library(rvest)
library(jsonlite)
#/lol/summoner/v3/summoners/{summonerId}
#/lol/match/v3/matchlists/by-account/{accountId}
key <- 'Your-API-KEY'
summoner_id <- '88770698'
base_url <- 'https://na1.api.riotgames.com'
account <- read_html(paste0(base_url, '/lol/summoner/v3/summoners/', summoner_id, '?api_key=', key))
account_info <- fromJSON(account %>% html_text())
account_id <- account_info$accountId
## static data
items <- read_html(paste0(base_url, '/lol/static-data/v3/items', '?api_key=', key, '&tags=image'))
item_detail <- fromJSON(items %>% html_text())
item_ids <- lapply(item_detail, names)[[1]]
item_names <- lapply(item_detail$data, function(x) x['name'])
item_names <- gsub('\\"|)$', "", item_names)
item_desc <- as.character(lapply(item_detail$data, function(x) x['description']))
item_desc <- gsub("list\\(description = ", "", item_desc)
item_desc <- gsub('\\"|)$', "", item_desc)
item_names <- gsub("list\\(name = ", "", as.character(item_names))
item_lookup <- data.frame(url=paste0('http://ddragon.leagueoflegends.com/cdn/7.10.1/img/item/', item_ids, '.png'), item_name = as.character(unlist(item_names)), itemId=item_ids, desc=item_desc)
item_lookup$itemId<-as.integer(as.character(item_lookup$itemId))
champions <- read_html(paste0(base_url, '/lol/static-data/v3/champions?locale=en_US&tags=blurb&tags=image&dataById=false&api_key=', key))
champion_detail <- fromJSON(champions %>% html_text())
champion_df <- data.frame(do.call('rbind', lapply(champion_detail$data, function(x) x['id'])))
champion_df$image <- paste0('https://ddragon.leagueoflegends.com/cdn/7.10.1/img/champion/', rownames(champion_df), '.png')
errors <- sapply(item_lookup$url, function(x) getURL(x, header=1, nobody=1, curl=getCurlHandle()))
item_lookup <- item_lookup[-grep("404", errors),]
item_lookup$itemId <- as.character(item_lookup$itemId)
for(i in 1:nrow(item_lookup)){
download.file(paste0('http://ddragon.leagueoflegends.com/cdn/7.10.1/img/item/', item_lookup$itemId[i], '.png'),
destfile=paste0("C:/Users/cashd/Documents/My Tableau Repository/Shapes/League/", item_lookup$itemId[i], '.png'), mode='wb')
}
write.csv(item_lookup, file="items.csv", row.names = FALSE)
matches <- read_html(paste0(base_url, '/lol/match/v3/matchlists/by-account/', account_id, '?api_key=', key))
match_info <- fromJSON(matches %>% html_text())
match_ids <- match_info$matches$gameId
match_list <- list()
i <- 1
for(match in match_ids){
print(i)
match_detail <- read_html(paste0(base_url, '/lol/match/v3/matches/', match, '?api_key=', key))
match_stats <- fromJSON(match_detail %>% html_text())
if(match_stats$gameMode != 'DARKSTAR'){
timeline <- read_html(paste0(base_url, '/lol/match/v3/timelines/by-match/', match, '?api_key=', key))
timeline_detail <- fromJSON(timeline %>% html_text())
events <- timeline_detail$frames$events
summoners <- data.frame(participantId=match_stats$participantIdentities$participantId, name=match_stats$participantIdentities$player$summonerName)
## remove position
if(length(grep("position|position\\.", unlist(lapply(events, names)))) > 0){
clean_events <- lapply(events, function(x) x[!(names(x) %in% c("position", "position.x", "position.y"))])
} else {
clean_events <- events
}
timeline_df <- dplyr::bind_rows(clean_events)
timeline_df$name <- summoners[match(timeline_df$killerId, summoners$participantId, NA),]$name
timeline_df[which(is.na(timeline_df$name)),]$name <- summoners[match(timeline_df[which(is.na(timeline_df$name)),]$participantId, summoners$participantId),]$name
timeline_df$item_url <- item_lookup[match(timeline_df$itemId, item_lookup$itemId),]$url
timeline_df$item_name <- item_lookup[match(timeline_df$itemId, item_lookup$itemId),]$item_name
timeline_df$item_desc <- item_lookup[match(timeline_df$itemId, item_lookup$itemId),]$desc
timeline_df$match_id <- match
timeline_df$teamId <- match_stats$participants[match(timeline_df$participantId, match_stats$participants$participantId),]$teamId
timeline_df$champion_id <- match_stats$participants[match(timeline_df$participantId, match_stats$participants$participantId),]$championId
timeline_df$result <- match_stats$teams[match(timeline_df$teamId, match_stats$teams$teamId),]$win
timeline_df$gameMode <- match_stats$gameMode[match(timeline_df$match_id, match_stats$gameId)]
timeline_df$gameType <- match_stats$gameType[match(timeline_df$match_id, match_stats$gameId)]
timeline_df$champion_name <- rownames(champion_df[match(timeline_df$champion_id, champion_df$id),])
timeline_df$champion_name <- gsub("\\.\\d+", "", timeline_df$champion_name)
timeline_df$champion_img <- champion_df[match(timeline_df$champion_id, champion_df$id),]$image
timeline_df$assistingParticipantIds<-vapply(timeline_df$assistingParticipantIds, paste, collapse=", ", character(1L))
match_list[[i]] <- timeline_df
i <- i + 1
}
}
all_matches<-dplyr::bind_rows(match_list)
all_matches$itemId <- as.character(all_matches$itemId)
write.csv(all_matches, file="match_timeline.csv", row.names = FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment