Mainline Code
Retrieve data and convert the json into a list.
# the api allows many queries. This query is on the current best sellers in the category of Hardcover Fiction
json_file<-sprintf("https://api.nytimes.com/svc/books/v3/lists/current/%s.json?api-key=%s", nyt_list_type, nyt_api)
# to get all list names
# https://api.nytimes.com/svc/books/v3/lists/names
# we could make this a shiny app with selectInput("ListType", "ListType:", c("business-books","hardcover-fiction" , "e-book-fiction" etc..
destfile<-paste0(CURR_PATH,"/nyt_books.json")
download.file(json_file, destfile)
json_data <- fromJSON(file = "nyt_books.json")Note json_data is a list of 5.
Within it is results which is a list of 12.
Only then do we get to the list of books.
The first 4 elements of json_data and the first 11 elements of results only occur once. They are Meta details.
We will print out some key details now.
meta_detail_df<-as.data.frame(cbind(list_name=json_data$results$list_name,
published=json_data$results$published_date,
list_date=json_data$results$bestsellers_date,
update_freq=json_data$results$updated
))
kable(meta_detail_df , caption="Meta Details",row.names = FALSE, booktabs=TRUE, table.attr = "style='width:80%;'") %>%
kable_styling(font_size = 12)| list_name | published | list_date | update_freq |
|---|---|---|---|
| Hardcover Fiction | 2021-10-31 | 2021-10-16 | WEEKLY |
Save off the Books list
books_list<-json_data$results$booksThe actual list of books still contains sub-lists of isbns and buy site urls.
So we will create 3 tables as a normalized database.
- Book Details
- Book ISBNs
- Book Buy Links
book_details_df <- data.frame(
rank =integer(),
rank_last_week =integer(),
weeks_on_list =integer(),
asterisk =integer(),
dagger =integer(),
primary_isbn10 =character(),
primary_isbn13 =character(),
publisher =character(),
description =character(),
price =character(),
title =character(),
author =character(),
contributor =character(),
contributor_note =character(),
book_image =character(),
book_image_width =integer(),
book_image_height =integer(),
amazon_product_url =character(),
age_group =character(),
book_review_link =character(),
first_chapter_link =character(),
sunday_review_link =character(),
article_chapter_link = character(),
buy_uri= character()
)
book_isbns_df <- data.frame(
title =character(),
isbn10 =character(),
isbn13 =character()
)
book_buy_links_df <- data.frame(
title =character(),
name =character(),
url =character()
)
Parse data into data frames.
for (item in books_list) {
book_details_df<-rbind(book_details_df,as.data.frame(item[-c(24, 25)]))
for (isbn_item in item$isbns){
book_isbns_df<-rbind(book_isbns_df,as.data.frame(cbind(title=item$title, isbn10=isbn_item$isbn10,isbn13=isbn_item$isbn13 )))
}
for (buy_link_item in item$buy_links){
book_buy_links_df<-rbind(book_buy_links_df,as.data.frame(cbind(title=item$title, name=buy_link_item$name,url=buy_link_item$url )))
}
}Print out some of the book details.
some_of_the_book_details<-book_details_df[c("rank","title","author","weeks_on_list")]
kable(some_of_the_book_details, caption="Best Seller List",row.names = FALSE, booktabs=TRUE) %>%
kable_styling(font_size = 10)| rank | title | author | weeks_on_list |
|---|---|---|---|
| 1 | STATE OF TERROR | Hillary Rodham Clinton and Louise Penny | 1 |
| 2 | THE WISH | Nicholas Sparks | 3 |
| 3 | THE LINCOLN HIGHWAY | Amor Towles | 2 |
| 4 | CLOUD CUCKOO LAND | Anthony Doerr | 3 |
| 5 | APPLES NEVER FALL | Liane Moriarty | 5 |
| 6 | SILVERVIEW | John Le Carré | 1 |
| 7 | THE LAST THING HE TOLD ME | Laura Dave | 24 |
| 8 | THE BOOK OF MAGIC | Alice Hoffman | 1 |
| 9 | HARLEM SHUFFLE | Colson Whitehead | 5 |
| 10 | THE BUTLER | Danielle Steel | 2 |
| 11 | BILLY SUMMERS | Stephen King | 11 |
| 12 | CROSSROADS | Jonathan Franzen | 2 |
| 13 | THE MIDNIGHT LIBRARY | Matt Haig | 45 |
| 14 | BEAUTIFUL WORLD, WHERE ARE YOU | Sally Rooney | 6 |
| 15 | THE JAILHOUSE LAWYER | James Patterson and Nancy Allen | 4 |
Print out the buy link details.
kable(book_buy_links_df , caption="Best Seller Buy Links",row.names = FALSE, booktabs=TRUE, table.attr = "style='width:80%;'") %>%
kable_styling(font_size = 10)Print out the ISBN details.
kable(book_isbns_df , caption="Best Seller ISBN list",row.names = FALSE, booktabs=TRUE, table.attr = "style='width:50%;'") %>%
kable_styling(font_size = 12)| title | isbn10 | isbn13 |
|---|---|---|
| STATE OF TERROR | 198217367X | 9781982173678 |
| STATE OF TERROR | 1982173696 | 9781982173692 |
| THE WISH | 1538728621 | 9781538728628 |
| THE LINCOLN HIGHWAY | 0735222355 | 9780735222359 |
| THE LINCOLN HIGHWAY | 0735222371 | 9780735222373 |
| CLOUD CUCKOO LAND | 1982168439 | 9781982168438 |
| CLOUD CUCKOO LAND | 1982168455 | 9781982168452 |
| APPLES NEVER FALL | 1250220254 | 9781250220257 |
| APPLES NEVER FALL | 1250220262 | 9781250220264 |
| APPLES NEVER FALL | 1250810698 | 9781250810694 |
| SILVERVIEW | 0593490592 | 9780593490594 |
| SILVERVIEW | 0593490606 | 9780593490600 |
| THE LAST THING HE TOLD ME | 1501171348 | 9781501171345 |
| THE LAST THING HE TOLD ME | 1501171364 | 9781501171369 |
| THE LAST THING HE TOLD ME | 1797124749 | 9781797124742 |
| THE BOOK OF MAGIC | 198215148X | 9781982151485 |
| THE BOOK OF MAGIC | 1982151501 | 9781982151508 |
| HARLEM SHUFFLE | 0385545134 | 9780385545136 |
| HARLEM SHUFFLE | 0385545142 | 9780385545143 |
| THE BUTLER | 1984821520 | 9781984821522 |
| THE BUTLER | 1984821539 | 9781984821539 |
| BILLY SUMMERS | 1982173610 | 9781982173616 |
| BILLY SUMMERS | 1982173637 | 9781982173630 |
| BILLY SUMMERS | 179712269X | 9781797122694 |
| CROSSROADS | 0374181179 | 9780374181178 |
| CROSSROADS | 0374719799 | 9780374719791 |
| THE MIDNIGHT LIBRARY | 0525559477 | 9780525559474 |
| THE MIDNIGHT LIBRARY | 0525559485 | 9780525559481 |
| THE MIDNIGHT LIBRARY | 0655697071 | 9780655697077 |
| BEAUTIFUL WORLD, WHERE ARE YOU | 0374602603 | 9780374602604 |
| BEAUTIFUL WORLD, WHERE ARE YOU | 0374602611 | 9780374602611 |
| THE JAILHOUSE LAWYER | 0316276626 | 9780316276627 |
| THE JAILHOUSE LAWYER | 0316280011 | 9780316280013 |