Skip to content

Instantly share code, notes, and snippets.

@safferli
Last active November 12, 2017 16:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save safferli/5cdf28465fd7cec7ef6b to your computer and use it in GitHub Desktop.
Save safferli/5cdf28465fd7cec7ef6b to your computer and use it in GitHub Desktop.
Query the (unofficial) Mashape metacritic game API in R
## Usage:
## 1) sign up to the Mashape API to get metacritic data: https://market.mashape.com/byroredux/metacritic-v2
## 2) save your Mashape API key in your .Renviron as Mashape.key
## 3) generate a character vector of games you want to query
## 4a) call f.generate.metacritic.data(game.vector) to generate a dataframe of the results
## 4b) alternatively, call f.call.metacritic.API(game) if you want to only call the API for one game (returns the API result, not a dataframe)
library(httr)
library(jsonlite)
library(dplyr)
library(magittr)
library(data.table)
## set your own Mashape key in your ~/.Renviron
# http://blog.revolutionanalytics.com/2015/11/how-to-store-and-use-authentication-details-with-r.html
# option 4: In a .Renviron file
Mashape.Key <- Sys.getenv("Mashape.key")
f.call.metacritic.API <- function(game, key = Mashape.Key, platform = "pc") {
## Mashape API to get metacritic data: https://market.mashape.com/byroredux/metacritic-v2
# http://stackoverflow.com/questions/30382196/use-mashape-with-r
resp <- GET(paste0("https://metacritic-2.p.mashape.com/find/game?platform=", platform, "&title=", game),
add_headers("X-Mashape-Key" = key,
"Accept" = "application/json"))
# prints the headers
#headers(resp)
# prints the content of the response
#str(content(resp))
## bind results into a dataframe
metacritic <- as.data.frame(
fromJSON(
# replace NULL with empty strings in json, so that we can coerce into a dataframe without errors
# jsonlite has no nullValue option, see issue #70: https://github.com/jeroenooms/jsonlite/issues/70
gsub(":null,", ":\"\",", content(resp, as = "text"))
), stringsAsFactors = FALSE)
# this is far nicer, do.call makes better dfs
# but resulting df has content as list of length 1... not good :(
#metacritic <- data.frame(do.call(rbind, fromJSON(content(resp, as = "text")))))
## return data
return(metacritic)
}
f.generate.metacritic.data <- function(games.vector) {
## generate a dataframe of metacritic API data for a given vector of game names
# games.vector needs to be a character vector!
if(!is.character(games.vector)) stop("f.generate.metacritic.data(): input is not a character vector")
# clean the namelist -- API does not recognise spaces, and metacritic removes & and ndashes in titles
# replace spaces
games.web <- gsub(" ", "%20",
# remove "-" ndash
gsub(" - ", " ",
# remove "&" ampersand
gsub("&", "", games.vector)))
# number of games in vector
num.of.games <- length(games.web)
## initialise dataset
mtac <- vector("list", num.of.games)
## fill list with API call results
for (i in seq_len(num.of.games)) {
mtac[[i]] <- f.call.metacritic.API(games.web[i])
}
## bind into one dataframe
metacritic <- rbindlist(mtac, fill = TRUE)
# remove last stray column (from the empty/missing results of the API calls)
#select(-result) %>% # this breaks if there are no invalid calls
# remove leading "result." in variable names
metacritic %<>% setNames(gsub("^result.", "", names(metacritic)))
# generate clean dataset from original games list
metacritic <- data.frame(name = games.vector) %>%
merge(metacritic, all.x = TRUE)
return(metacritic)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment