JSON for top 3 books

library(httr)
library(jsonlite)

# Read API key ---
api_key_url <- "https://raw.githubusercontent.com/prnakyazze94/Data_607/refs/heads/main/NYT_API"
api_key <- trimws(readLines(api_key_url, warn = FALSE)[1])

# NYT API current list endpoint ---
base_url <- "https://api.nytimes.com/svc/books/v3/lists"
endpoint <- "current/hardcover-fiction.json"
url <- paste0(base_url, "/", endpoint)

# GET request ---
res <- GET(url, query = list("api-key" = api_key))

if (status_code(res) == 200) {
  # Parse full JSON
  json_data <- fromJSON(content(res, as = "text", encoding = "UTF-8"), flatten = TRUE)
  
  # Extract top 3 books
  top3_books <- json_data$results$books[1:3, ]
  
  # Convert back to JSON (pretty)
  top3_json <- toJSON(top3_books, pretty = TRUE, auto_unbox = TRUE)
  
  # Print JSON for top 3 books
  cat(top3_json)
  
  # Optionally save to file
  write(top3_json, file = "nyt_top3_books.json")
  cat("\nJSON for top 3 books saved as nyt_top3_books.json\n")
  
} else {
  cat("Failed to retrieve data. Status code:", status_code(res), "\n")
  print(content(res, as = "text"))
}
## [
##   {
##     "age_group": "",
##     "amazon_product_url": "https://www.amazon.com/dp/1464260915?tag=thenewyorktim-20",
##     "article_chapter_link": "",
##     "asterisk": 0,
##     "author": "Freida McFadden",
##     "book_image": "https://static01.nyt.com/bestsellers/images/9781464260919.jpg",
##     "book_image_height": 500,
##     "book_image_width": 394,
##     "book_review_link": "",
##     "book_uri": "nyt://book/65d085aa-5926-5741-8006-1cd6cb90849e",
##     "contributor": "by Freida McFadden",
##     "contributor_note": "",
##     "created_date": "2025-10-15T22:38:33.294Z",
##     "dagger": 0,
##     "description": "During a rough storm, Casey puts herself in danger when she lets a girl, who is covered in blood, into her cabin.",
##     "first_chapter_link": "",
##     "price": "0.00",
##     "primary_isbn10": "",
##     "primary_isbn13": "9781464260919",
##     "publisher": "Poisoned Pen",
##     "rank": 1,
##     "rank_last_week": 0,
##     "sunday_review_link": "",
##     "title": "THE INTRUDER",
##     "updated_date": "2025-10-15T22:38:33.294Z",
##     "weeks_on_list": 1,
##     "isbns": [
##       {
##         "isbn10": "",
##         "isbn13": "9781464260919"
##       }
##     ],
##     "buy_links": [
##       {
##         "name": "Amazon",
##         "url": "https://www.amazon.com/dp/1464260915?tag=thenewyorktim-20"
##       },
##       {
##         "name": "Apple Books",
##         "url": "https://goto.applebooks.apple/9781464260919?at=10lIEQ"
##       },
##       {
##         "name": "Barnes & Noble",
##         "url": "https://www.anrdoezrs.net/click-7990613-11819508?url=https%3A%2F%2Fwww.barnesandnoble.com%2Fw%2Fthe-intruder-freida-mcfadden%2F1147331363%3Fean%3D9781464260919"
##       },
##       {
##         "name": "Books-A-Million",
##         "url": "https://www.anrdoezrs.net/click-7990613-35140?url=https%3A%2F%2Fwww.booksamillion.com%2Fp%2FIntruder%2FFreida-McFadden%2F9781464260919"
##       },
##       {
##         "name": "Bookshop.org",
##         "url": "https://bookshop.org/a/3546/9781464260919"
##       }
##     ]
##   },
##   {
##     "age_group": "",
##     "amazon_product_url": "https://www.amazon.com/dp/0385546890?tag=thenewyorktim-20",
##     "article_chapter_link": "",
##     "asterisk": 0,
##     "author": "Dan Brown",
##     "book_image": "https://static01.nyt.com/bestsellers/images/9780385546898.jpg",
##     "book_image_height": 500,
##     "book_image_width": 331,
##     "book_review_link": "https://www.nytimes.com/2025/09/09/books/review/the-secret-of-secrets-dan-brown.html",
##     "book_uri": "nyt://book/49cff1bb-35bc-554b-a7d4-dbc946230c00",
##     "contributor": "by Dan Brown",
##     "contributor_note": "",
##     "created_date": "2025-09-17T22:38:11.49Z",
##     "dagger": 0,
##     "description": "As he searches for the missing noetic scientist he has been seeing, Robert Langdon discovers something regarding a secret project.",
##     "first_chapter_link": "",
##     "price": "0.00",
##     "primary_isbn10": "",
##     "primary_isbn13": "9780385546898",
##     "publisher": "Doubleday",
##     "rank": 2,
##     "rank_last_week": 4,
##     "sunday_review_link": "",
##     "title": "THE SECRET OF SECRETS",
##     "updated_date": "2025-09-17T22:38:11.49Z",
##     "weeks_on_list": 5,
##     "isbns": [
##       {
##         "isbn10": "",
##         "isbn13": "9780385546898"
##       }
##     ],
##     "buy_links": [
##       {
##         "name": "Amazon",
##         "url": "https://www.amazon.com/dp/0385546890?tag=thenewyorktim-20"
##       },
##       {
##         "name": "Barnes & Noble",
##         "url": "https://www.anrdoezrs.net/click-7990613-11819508?url=https%3A%2F%2Fwww.barnesandnoble.com%2Fw%2Fthe-secret-of-secrets-dan-brown%2F1146902400%3Fean%3D9780385546898"
##       },
##       {
##         "name": "Books-A-Million",
##         "url": "https://www.anrdoezrs.net/click-7990613-35140?url=https%3A%2F%2Fwww.booksamillion.com%2Fp%2FSecret-Secrets%2FDan-Brown%2F9780385546898"
##       },
##       {
##         "name": "Bookshop.org",
##         "url": "https://bookshop.org/a/3546/9780385546898"
##       }
##     ]
##   },
##   {
##     "age_group": "",
##     "amazon_product_url": "https://www.amazon.com/dp/1668095254?tag=thenewyorktim-20",
##     "article_chapter_link": "",
##     "asterisk": 0,
##     "author": "Jack Carr",
##     "book_image": "https://static01.nyt.com/bestsellers/images/9781668095256.jpg",
##     "book_image_height": 500,
##     "book_image_width": 329,
##     "book_review_link": "",
##     "book_uri": "nyt://book/3d807ffe-41b7-548d-af77-84fa84612a24",
##     "contributor": "by Jack Carr",
##     "contributor_note": "",
##     "created_date": "2025-10-15T22:38:33.486Z",
##     "dagger": 0,
##     "description": "During a time of war and civil unrest in 1968, the Navy SEAL Tom Reece is given an unofficial and dangerous mission.",
##     "first_chapter_link": "",
##     "price": "0.00",
##     "primary_isbn10": "",
##     "primary_isbn13": "9781668095256",
##     "publisher": "Atria/Emily Bestler",
##     "rank": 3,
##     "rank_last_week": 0,
##     "sunday_review_link": "",
##     "title": "CRY HAVOC",
##     "updated_date": "2025-10-15T22:38:33.486Z",
##     "weeks_on_list": 1,
##     "isbns": [
##       {
##         "isbn10": "",
##         "isbn13": "9781668095256"
##       }
##     ],
##     "buy_links": [
##       {
##         "name": "Amazon",
##         "url": "https://www.amazon.com/dp/1668095254?tag=thenewyorktim-20"
##       },
##       {
##         "name": "Apple Books",
##         "url": "https://goto.applebooks.apple/9781668095256?at=10lIEQ"
##       },
##       {
##         "name": "Barnes & Noble",
##         "url": "https://www.anrdoezrs.net/click-7990613-11819508?url=https%3A%2F%2Fwww.barnesandnoble.com%2Fw%2Fcry-havoc-jack-carr%2F1146323769%3Fean%3D9781668095256"
##       },
##       {
##         "name": "Books-A-Million",
##         "url": "https://www.anrdoezrs.net/click-7990613-35140?url=https%3A%2F%2Fwww.booksamillion.com%2Fp%2FCry-Havoc%2FJack-Carr%2F9781668095256"
##       },
##       {
##         "name": "Bookshop.org",
##         "url": "https://bookshop.org/a/3546/9781668095256"
##       }
##     ]
##   }
## ]
## JSON for top 3 books saved as nyt_top3_books.json