WEB APIs

The New York Times web site provides a rich set of APIs, as described here: http://developer.nytimes.com/docs. Sign up for an API key.

Task: Choose one of the New York Times APIs, construct an interface in R to read in the JSON data, and transform it to an R dataframe.

Installing and Loading necessary packages

library(httr)
library(jsonlite)
library(knitr)

Books API - Provides information about books reviews and the New York Times bestsellers lists

NYT Top 5 Bestsellers for a specific month - October, 2018 (Using jsonlite)

api_key <- "&api-key=9507c7df7afe48a08c9d07886b7f97d4"
url_j <- "http://api.nytimes.com/svc/books/v2/lists/overview.json?"
pub_date <- "published_date=2018-10-01"
request_j <- fromJSON(paste0(url_j, pub_date, api_key))
bsellers_lists <- request_j$results$list
bsellers_books <- bsellers_lists[[1, "books"]]
bsellers_df <- subset(bsellers_books, select = c("title", "author", "description", "rank"))
class(bsellers_df)
## [1] "data.frame"
kable(bsellers_df,  format = "html", caption = "NYT Top 5 Bestsellers - October 2018")
NYT Top 5 Bestsellers - October 2018
title author description rank
LETHAL WHITE Robert Galbraith The fourth book in the Cormoran Strike series. Detectives Strike and Ellacott investigate a crime a young man may have witnessed as a child; by J. K. Rowling, writing pseudonymously. 1
TIME’S CONVERT Deborah Harkness During his lover’s journey to immortality, a vampire’s past returns to haunt them both. 2
ORIGIN Dan Brown After reconnecting with one of his first students, who is now a billionaire futurist, symbology professor Robert Langdon must go on a perilous quest with a beautiful museum director. 3
JUROR #3 James Patterson and Nancy Allen Ruby Bozarth defends a college football star charged in a felony case complicated by a second murder. 4
CRAZY RICH ASIANS Kevin Kwan A New Yorker gets a surprise when she spends the summer with her boyfriend in Singapore. 5

Bestsellers History for a specific author: Dan Brown (Using httr)

url_h <- "https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json?author=Dan+Brown"
request_h <- GET(url_h, add_headers(api_key="9507c7df7afe48a08c9d07886b7f97d4"))
http_status(request_h)
## $category
## [1] "Success"
## 
## $reason
## [1] "OK"
## 
## $message
## [1] "Success: (200) OK"
bsellers_hist <- content(request_h, "parsed")
num_results <- as.numeric(bsellers_hist$num_results)
bsellers_results <- bsellers_hist$results

Unnamed second level returned in the r list (after “results” json object array), Logic implemented to extract Bestseller Title and Description

bsellers_titles <- data.frame("","")
colnames(bsellers_titles) <- c("Title", "Description")
for (i in 1:num_results){
bsellers_titles_temp <- do.call("cbind", lapply(bsellers_results[[i]][1:2], data.frame, stringsAsFactors = FALSE))
colnames(bsellers_titles_temp) <- c("Title", "Description")
bsellers_titles <- rbind(bsellers_titles, bsellers_titles_temp)
}
class(bsellers_titles)
## [1] "data.frame"
kable(bsellers_titles,  format = "html", caption = "NYT Bestsellers by Dan Brown")
NYT Bestsellers by Dan Brown
Title Description
ANGELS AND DEMONS A scholar tries to save the Vatican from the machinations of an underground society.
INFERNO The symbologist Robert Langdon, on the run in Florence, must decipher a series of codes created by a Dante-loving scientist.
ORIGIN After reconnecting with one of his first students, who is now a billionaire futurist, symbology professor Robert Langdon must go on a perilous quest with a beautiful museum director.
THE LOST SYMBOL The Harvard symbologist Robert Langdon among the Masons.