Purpose

For this assignment I’ll be learning how to read data in from an API with the New York Times that requires a access key in order to look at data from their bestseller book lists the week of my birthday this past year.

Reading in the Data

First I’ll need to install some packages to access, manipulate, and display the data

library(httr)
library(jsonlite)
library(dplyr)
library(stringr)
library(kableExtra)

Next I’ve put my API key for NYT in “nytkey” in a code chunk that is hidden, but I reference it below while using the GET function to access the API for NY Times Books.

#path provided by NY Times 
path <- "https://api.nytimes.com/svc/books/v3/lists/overview.json"

#setting the date the bestseller list as my birthday, and a just-before the pandemic time
query <- "?published_date=2020-03-01"

#string it all together
url <- str_c(path, query, "&api-key=", nytkey, sep = "", collapse = NULL)

nyt_api <- GET(url, verbose())

View List Types

Transform what we pulled in to a dataframe where we can look at the lists available to us.

#grab the raw import of the API and set is text with proper encoding
bestseller_lists <- content(nyt_api, "text", encoding='UTF-8')

#convert from JSON
json_list <- fromJSON(bestseller_lists, flatten = TRUE)

#drill down to the lsits we want to see and display them
bestseller_lists <- json_list$results$list
kable(bestseller_lists$list_name, format = 'markdown')
x
Combined Print and E-Book Fiction
Combined Print and E-Book Nonfiction
Hardcover Fiction
Hardcover Nonfiction
Trade Fiction Paperback
Paperback Nonfiction
Advice How-To and Miscellaneous
Childrens Middle Grade Hardcover
Picture Books
Series Books
Young Adult Hardcover
Audio Fiction
Audio Nonfiction
Business Books
Mass Market Monthly
Middle Grade Paperback Monthly
Young Adult Paperback Monthly

Accessing the Book Lists

Finally, lets look at two bestseller lists from my birthday week this year. First, I’ll pull paperback nonfiction books and a description.

#the 6 as it's the 6th list in the entry above
paperback_nf <- bestseller_lists[[6, "books"]]
paperback_nf <- subset(paperback_nf, select = c("title", "author", 
                                                "description", "weeks_on_list"))
kable(paperback_nf, format = 'pipe')
title author description weeks_on_list
JUST MERCY Bryan Stevenson A civil rights lawyer and MacArthur grant recipient’s memoir of his decades of work to free innocent people condemned to death. 192
ON TYRANNY Timothy Snyder Twenty lessons from the 20th century about the course of tyranny. 62
THE BODY KEEPS THE SCORE Bessel van der Kolk How trauma affects the body and mind, and innovative treatments for recovery. 69
SAPIENS Yuval Noah Harari How Homo sapiens became Earth’s dominant species. 92
WHITE FRAGILITY Robin DiAngelo Historical and cultural analyses on what causes defensive moves by white people and how this inhibits cross-racial dialogue. 80

Next, lets look at the hardcover fiction books from my birthday week and pull slightly different variables.

#the 1 as it's the 1st list in the entry above
hardcover_fic <- bestseller_lists[[1, "books"]]
hardcover_fic <- subset(hardcover_fic, select = c("title", "author", 
                                                  "publisher", 
                                                  "book_review_link"))
kable(hardcover_fic, format = 'pipe')
title author publisher book_review_link
AMERICAN DIRT Jeanine Cummins Flatiron https://www.nytimes.com/2020/01/17/books/review-american-dirt-jeanine-cummins.html
WHERE THE CRAWDADS SING Delia Owens Putnam
GOLDEN IN DEATH JD Robb St. Martin’s
LITTLE FIRES EVERYWHERE Celeste Ng Penguin Press https://www.nytimes.com/2017/09/25/books/review/little-fires-everywhere-celeste-ng.html
THE SILENT PATIENT Alex Michaelides Celadon

Conclusion

While getting the URL correct to access the API took me some time, it was fairly intuitive to move through the file and pick out the best seller lists I wanted to see after that.

For this week’s assignment I had a hard time accessing the NY Times Books API, and found this vingette helpful in getting it to work: https://cran.r-project.org/web/packages/jsonlite/vignettes/json-apis.html