Assignment – Web APIs

The New York Times web site provides a rich set of APIs, as described here: https://developer.nytimes.com/apis

You’ll need to start by signing up for an API key.

Your task is to choose one of the New York Times APIs, construct an interface in R to read in the JSON data, and transform it into an R DataFrame.

Introduction:

For this assignment, I will be using the NYT Books API, which is great for exploring bestseller lists or get reviews of books from The New York Times.

# Load Libraries

library(httr)
library(jsonlite)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# API URL to be used
# https://api.nytimes.com/svc/books/v3/lists/current/hardcover-fiction.json?api-key=YOUR_API_KEY

# API APP NAME
# BOOKS_API
usethis::edit_r_environ()
## ☐ Edit '/Users/baadigun/.Renviron'.
## ☐ Restart R for changes to take effect.
# This function is to get the NYT Best Sellers list
getting_nyt_bestsellers <- function(list_name = "hardcover-fiction", api_key) {
  # Construct API URL
  base_url <- "https://api.nytimes.com/svc/books/v3/lists/current"
  full_url <- paste0(base_url, "/", list_name, ".json?api-key=", api_key)
  
  # Make API request
  response <- GET(full_url)
  
  # Checking for errors
  if (http_error(response)) {
    stop("Request failed: ", status_code(response))
  }
  
  # Parse JSON response
  content_json <- content(response, as = "text")
  parsed <- fromJSON(content_json, flatten = TRUE)
  
  # Extract books and clean into a DataFrame
  books_df <- parsed$results$books %>%
    select(rank, title, author, description, publisher, weeks_on_list, amazon_product_url)
  
  return(books_df)
}

# Example usage
api_key <- Sys.getenv("NYT_API_KEY")

# Get current bestsellers in hardcover fiction
bestsellers_nyt <- getting_nyt_bestsellers("hardcover-fiction", api_key)

# View result
head(bestsellers_nyt)
##   rank              title            author
## 1    1         ONYX STORM    Rebecca Yarros
## 2    2 SUMMER IN THE CITY        Alex Aster
## 3    3      NOBODY'S FOOL      Harlan Coben
## 4    4             ELPHIE   Gregory Maguire
## 5    5        LETHAL PREY     John Sandford
## 6    6     BROKEN COUNTRY Clare Leslie Hall
##                                                                                                                             description
## 1 The third book in the Empyrean series. As enemies gain traction, Violet Sorrengail goes beyond the Aretian wards in search of allies.
## 2      A screenwriter seeking inspiration coincidentally moves next door to the billionaire with whom she hooked up a couple years ago.
## 3                                              The private investigator Sami Kierce tracks down clues to solve a mystery from his past.
## 4                                                Elphaba’s childhood lays the groundwork for the character she will become in “Wicked.”
## 5            The 35th book in the Prey series. True-crime bloggers complicate Lucas Davenport and Virgil Flowers’s search for a killer.
## 6                            Beth must confront her past when the man she once loved as a teenager returns to the village with his son.
##          publisher weeks_on_list
## 1        Red Tower            10
## 2           Morrow             1
## 3    Grand Central             1
## 4           Morrow             1
## 5           Putnam             1
## 6 Simon & Schuster             4
##                                          amazon_product_url
## 1 https://www.amazon.com/dp/1649374186?tag=thenewyorktim-20
## 2 https://www.amazon.com/dp/0063411660?tag=thenewyorktim-20
## 3 https://www.amazon.com/dp/1538756358?tag=thenewyorktim-20
## 4 https://www.amazon.com/dp/0063377012?tag=thenewyorktim-20
## 5 https://www.amazon.com/dp/0593718402?tag=thenewyorktim-20
## 6 https://www.amazon.com/dp/166807818X?tag=thenewyorktim-20

In Addition, I will like to analysis the data a bit

This is to show a basic summary statics of the current bestsellers in hardcover fiction

# How many books are on the list?
nrow(bestsellers_nyt)
## [1] 15
# Summary of weeks on the list
summary(bestsellers_nyt$weeks_on_list)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    1.00    1.00    4.00   22.87   38.00   90.00

Top authors with more books on the hardcover fiction

# Count authors with multiple books on the list
bestsellers_nyt %>%
  group_by(author) %>%
  summarise(count = n()) %>%
  arrange(desc(count))
## # A tibble: 13 × 2
##    author                          count
##    <chr>                           <int>
##  1 Rebecca Yarros                      3
##  2 Alex Aster                          1
##  3 Alison Espach                       1
##  4 Bob the Drag Queen                  1
##  5 Carissa Broadbent                   1
##  6 Clare Leslie Hall                   1
##  7 Gregory Maguire                     1
##  8 Harlan Coben                        1
##  9 James Patterson and J.D. Barker     1
## 10 John Sandford                       1
## 11 Kristin Hannah                      1
## 12 Liz Moore                           1
## 13 Percival Everett                    1

Visualization: Weeks on List

library(ggplot2)

ggplot(bestsellers_nyt, aes(x = reorder(title, weeks_on_list), y = weeks_on_list)) +
  geom_col(fill = "steelblue") +
  coord_flip() +
  labs(title = "NYT Hardcover Fiction Bestsellers",
       x = "Book Title",
       y = "Weeks on List")

Conclusion: Web APIs; NYT Books API; NYT Best Sellers Analysis

I was able to successfully retrieved data from the NYT Books API and analyzed the hardcover fiction bestseller list.

Key steps included:

Data Retrieval: Pulled current bestsellers and transformed the JSON data into an R data.frame.

Summary Stats: We explored the number of books and their weeks on the list.

Top Authors: Identified authors with the most books on the list.

Visualizations: Created a bar chart of books’ weeks on the list.

This analysis provides insights into bestseller trends and author popularity.