NYT Web API

Author

Rashad Long

Introduction

This project aims to construct a data frame containing the current New York Times Best Sellers List for the ‘Combined Print & E-Book Fiction’ category. The data will be retrieved by leveraging the New York Times Books API.

Imported httr2 and jsonlite

library(httr2)
library(jsonlite)
library(dplyr)

To ensure accuracy, verification of the category name used by the New York Times Best Sellers List was required. This verification was achieved by querying the names list endpoint of the New York Times Books API, which provides a comprehensive list of available categories.

# Get the book list names from the New York Times API using api_key
category_names_url <-
  paste("https://api.nytimes.com/svc/books/v3/lists/names.json?api-key=",
        api_key,
        sep = "")

# Get the category names from the New York Times API JSON
category_names <- fromJSON(category_names_url)

# Extract the list name from the JSON
category_names <- category_names$results

# Extract the category names from the list name column
category_names <- category_names$list_name_encoded

# Display the category names
category_names
 [1] "combined-print-and-e-book-fiction"   
 [2] "combined-print-and-e-book-nonfiction"
 [3] "hardcover-fiction"                   
 [4] "hardcover-nonfiction"                
 [5] "trade-fiction-paperback"             
 [6] "mass-market-paperback"               
 [7] "paperback-nonfiction"                
 [8] "e-book-fiction"                      
 [9] "e-book-nonfiction"                   
[10] "hardcover-advice"                    
[11] "paperback-advice"                    
[12] "advice-how-to-and-miscellaneous"     
[13] "hardcover-graphic-books"             
[14] "paperback-graphic-books"             
[15] "manga"                               
[16] "combined-print-fiction"              
[17] "combined-print-nonfiction"           
[18] "chapter-books"                       
[19] "childrens-middle-grade"              
[20] "childrens-middle-grade-e-book"       
[21] "childrens-middle-grade-hardcover"    
[22] "childrens-middle-grade-paperback"    
[23] "paperback-books"                     
[24] "picture-books"                       
[25] "series-books"                        
[26] "young-adult"                         
[27] "young-adult-e-book"                  
[28] "young-adult-hardcover"               
[29] "young-adult-paperback"               
[30] "animals"                             
[31] "audio-fiction"                       
[32] "audio-nonfiction"                    
[33] "business-books"                      
[34] "celebrities"                         
[35] "crime-and-punishment"                
[36] "culture"                             
[37] "education"                           
[38] "espionage"                           
[39] "expeditions-disasters-and-adventures"
[40] "fashion-manners-and-customs"         
[41] "food-and-fitness"                    
[42] "games-and-activities"                
[43] "graphic-books-and-manga"             
[44] "hardcover-business-books"            
[45] "health"                              
[46] "humor"                               
[47] "indigenous-americans"                
[48] "relationships"                       
[49] "mass-market-monthly"                 
[50] "middle-grade-paperback-monthly"      
[51] "paperback-business-books"            
[52] "family"                              
[53] "hardcover-political-books"           
[54] "race-and-civil-rights"               
[55] "religion-spirituality-and-faith"     
[56] "science"                             
[57] "sports"                              
[58] "travel"                              
[59] "young-adult-paperback-monthly"       

The category name for the ‘Combined Print & E-Book Fiction’ category was confirmed to be ‘combined-print-and-e-book-fiction’. The next step was to retrieve the current New York Times Best Sellers List for the ‘Combined Print & E-Book Fiction’ category.

# Get the current New York Times Best Sellers List for the 'Combined Print & E-Book Fiction' category
best_sellers_url <-
  paste(
    "https://api.nytimes.com/svc/books/v3/lists/current/combined-print-and-e-book-fiction.json?api-key=",
    api_key,
    sep = ""
  )

# Get the current New York Times Best Sellers List for the 'Combined Print & E-Book Fiction' category JSON
best_sellers <- fromJSON(best_sellers_url)

# Extract the best sellers list from the JSON
best_sellers <- best_sellers$results
best_sellers <- best_sellers$books


# I only need the rank, title, author, description and primary_ISBN13 columns
best_sellers <- best_sellers |>
  select(rank, title, author, description, primary_isbn13)

# Display the best sellers list
best_sellers
   rank                            title             author
1     1                        THE WOMEN     Kristin Hannah
2     2                 A TOUCH OF CHAOS Scarlett St. Clair
3     3                      FOURTH WING     Rebecca Yarros
4     4                       IRON FLAME     Rebecca Yarros
5     5                      HAPPY PLACE        Emily Henry
6     6         A COURT OF SILVER FLAMES      Sarah J. Maas
7     7                       THE INMATE    Freida McFadden
8     8         STILL SEE YOU EVERYWHERE       Lisa Gardner
9     9                      THE TEACHER    Freida McFadden
10   10                    THE HOUSEMAID    Freida McFadden
11   11                       ICEBREAKER       Hannah Grace
12   12                       THE HUNTER        Tana French
13   13                            BRIDE      Ali Hazelwood
14   14 THE HEAVEN & EARTH GROCERY STORE      James McBride
15   15                   FIRST LIE WINS      Ashley Elston
                                                                                                                                         description
1                                   In 1965, a nursing student follows her brother to serve during the Vietnam War and returns to a divided America.
2  The seventh book in the Hades X Persephone series. The Goddess of Spring must come to terms with her new identity as the Queen of the Underworld.
3                   Violet Sorrengail is urged by the commanding general, who also is her mother, to become a candidate for the elite dragon riders.
4                  The second book in the Empyrean series. Violet Sorrengail’s next round of training might require her to betray the man she loves.
5                                         A former couple pretend to be together for the sake of their friends during their annual getaway in Maine.
6                 The fifth book in the Court of Thorns and Roses series. Nesta Archeron is forced into close quarters with a warrior named Cassian.
7                            A nurse practitioner at a maximum-security prison gave testimony against her former boyfriend that put him behind bars.
8    The third book in the Frankie Elkin series. Frankie must find the sister, who was kidnapped over a decade ago, of a serial killer on death row.
9                                 A math teacher at Caseham High suspects there is more going on behind a scandal involving a teacher and a student.
10                                           Troubles surface when a woman looking to make a fresh start takes a job in the home of the Winchesters.
11                                  Anastasia might need the help of the captain of a college hockey team to get on the Olympic figure skating team.
12                                 The life that a Chicago P.D. retiree has built in Ireland with a local woman and her daughter comes under threat.
13                                Issues of trust arise when an alliance is made between a Vampyre named Misery Lark and a Were named Lowe Moreland.
14                        Secrets held by the residents of a dilapidated neighborhood come to life when a skeleton is found at the bottom of a well.
15                                                 A woman who works for a mysterious boss takes on a new identity to dig up information on someone.
   primary_isbn13
1   9781250178633
2   9781728259734
3   9781649374042
4   9781649374172
5   9780593441190
6   9781635577990
7   9781728296173
8   9781538765098
9   9781728296210
10  9781538742570
11  9781668026038
12  9780593493434
13  9780593550403
14  9780593422946
15  9780593492925

Conclusion

To ensure accurate category selection, the New York Times Books API was initially queried to retrieve a list of available categories. Following verification, the category name ‘combined-print-and-e-book-fiction’ was confirmed for the target data set. Subsequently, the current New York Times Best Sellers List for this specific category was retrieved via the API. The extracted data was then stored in a structured data frame, encompassing the following attributes for each book: rank, title, author, description, and primary ISBN13. Furthermore, I would like to try and make this interactive where the user can chose which best seller category they would like a list of.

# Confirm the data type of best_sellers
class(best_sellers)
[1] "data.frame"