Setup of packages

knitr::opts_chunk$set(echo = TRUE)
if("jsonlite" %in% rownames(installed.packages()) == FALSE) {install.packages("jsonlite")}
library(jsonlite)
if("dplyr" %in% rownames(installed.packages()) == FALSE) {install.packages("dplyr")}
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.3.2
## 
## 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
if("stringr" %in% rownames(installed.packages()) == FALSE) {install.packages("stringr")}
library(stringr)
if("ggplot2" %in% rownames(installed.packages()) == FALSE) {install.packages("ggplot2")}
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.3.2
if("rtimes" %in% rownames(installed.packages()) == FALSE) {install.packages("rtimes")}
library(rtimes)
## Warning: package 'rtimes' was built under R version 3.3.3

Read in names of NY Times best selling lists from API

NYT_url <- "https://api.nytimes.com/svc/books/v3/lists/names.json?"
API_Key <- "fdfd28b541314f5793618f60cefbd084"
namesRaw <- fromJSON(paste(NYT_url, "api-key=",API_Key, sep=""))
namesData <- namesRaw$results
head(namesData)
##                              list_name                       display_name
## 1    Combined Print and E-Book Fiction    Combined Print & E-Book Fiction
## 2 Combined Print and E-Book Nonfiction Combined Print & E-Book Nonfiction
## 3                    Hardcover Fiction                  Hardcover Fiction
## 4                 Hardcover Nonfiction               Hardcover Nonfiction
## 5              Trade Fiction Paperback            Paperback Trade Fiction
## 6                Mass Market Paperback      Paperback Mass-Market Fiction
##                      list_name_encoded oldest_published_date
## 1    combined-print-and-e-book-fiction            2011-02-13
## 2 combined-print-and-e-book-nonfiction            2011-02-13
## 3                    hardcover-fiction            2008-06-08
## 4                 hardcover-nonfiction            2008-06-08
## 5              trade-fiction-paperback            2008-06-08
## 6                mass-market-paperback            2008-06-08
##   newest_published_date updated
## 1            2017-04-09  WEEKLY
## 2            2017-04-09  WEEKLY
## 3            2017-04-09  WEEKLY
## 4            2017-04-09  WEEKLY
## 5            2017-04-09  WEEKLY
## 6            2017-01-29  WEEKLY

Isolate fiction lists

fictionNames <- filter(namesData, str_detect(namesData$display_name, "Fiction"))

Read in Current Bestseller fiction lists

NYT_url <- "https://api.nytimes.com/svc/books/v3/lists.json?"
API_Key <- "fdfd28b541314f5793618f60cefbd084"
allData <- data.frame()
for(i in 1:nrow(fictionNames)){
  pageData <- fromJSON(paste(NYT_url, "&api-key=",API_Key, "&list=",fictionNames$list_name_encoded[i],sep= ""), simplifyDataFrame = TRUE)
  allData <- rbind(allData, pageData$results)
}
head(allData)
##                           list_name                    display_name
## 1 Combined Print and E-Book Fiction Combined Print & E-Book Fiction
## 2 Combined Print and E-Book Fiction Combined Print & E-Book Fiction
## 3 Combined Print and E-Book Fiction Combined Print & E-Book Fiction
## 4 Combined Print and E-Book Fiction Combined Print & E-Book Fiction
## 5 Combined Print and E-Book Fiction Combined Print & E-Book Fiction
## 6 Combined Print and E-Book Fiction Combined Print & E-Book Fiction
##   bestsellers_date published_date rank rank_last_week weeks_on_list
## 1       2017-03-25     2017-04-09    1              0             1
## 2       2017-03-25     2017-04-09    2              1             9
## 3       2017-03-25     2017-04-09    3              0             1
## 4       2017-03-25     2017-04-09    4              0             1
## 5       2017-03-25     2017-04-09    5              0             1
## 6       2017-03-25     2017-04-09    6              4            32
##   asterisk dagger
## 1        0      0
## 2        0      0
## 3        0      0
## 4        0      0
## 5        0      0
## 6        0      0
##                                                                                        amazon_product_url
## 1 https://www.amazon.com/Mississippi-Blood-Novel-Natchez-Burning-ebook/dp/B01HXJREHW?tag=thenewyorktim-20
## 2           http://www.amazon.com/The-Shack-Tragedy-Confronts-Eternity/dp/0964729237?tag=thenewyorktim-20
## 3          https://www.amazon.com/Bound-Together-Sea-Haven-Novel-ebook/dp/B01KGZVY3A?tag=thenewyorktim-20
## 4        https://www.amazon.com/Vicious-Circle-Joe-Pickett-Novel-ebook/dp/B01HYUVHIM?tag=thenewyorktim-20
## 5              https://www.amazon.com/If-Not-You-Debbie-Macomber-ebook/dp/B01G0GD14O?tag=thenewyorktim-20
## 6           http://www.amazon.com/Big-Little-Lies-Liane-Moriarty-ebook/dp/B00HDMMISA?tag=thenewyorktim-20
##                                                                                                                                                                                                                    isbns
## 1                                                                                                                                                                   0062311158, 0062311190, 9780062311153, 9780062311191
## 2                        0964729237, , 160941411X, 1455567604, 1455567612, 1455568295, 0964729296, 1455523038, 9780964729230, , 9781609414115, 9781455567607, 9781455567614, 9781455568291, 9780964729292, 9781455523030
## 3                                                                                                                                                                   0399583947, 0399583939, 9780399583940, 9780399583933
## 4                                                                                                                                                                   0399176616, 0698410076, 9780399176616, 9780698410077
## 5                                                                                                                                        0553391968, 0553391976, 1524774626, 9780553391961, 9780553391978, 9781524774622
## 6 0399167064, 0698138635, 1410472035, 0425274861, 1594139067, 0399587195, 0399587209, 1312731796, 9780399167065, 9780698138636, 9781410472038, 9780425274866, 9781594139062, 9780399587191, 9780399587207, 9781312731790
##                                                                                                                                                                                                                                                                                         book_details
## 1 MISSISSIPPI BLOOD, Penn Cage, now the mayor of Natchez, Miss., investigates the secrets of his family and the South in order to save his and his father<U+0092>s lives in the conclusion to the Natchez Burning trilogy., by Greg Iles, Greg Iles, , 0, , Morrow/HarperCollins, 9780062311191, 0062311190
## 2                                                                                                          THE SHACK, A man whose daughter was abducted is invited to an isolated shack, apparently by God., by William P. Young, William P Young, , 0, , Windblown Media, 9780964729230, 0964729237
## 3                                                                                                                          BOUND TOGETHER, Viktor returns to Sea Haven to see the wife whose stepfather he killed., by Christine Feehan, Christine Feehan, , 0, , Berkley, 9780399583940, 0399583947
## 4                                                                                        VICIOUS CIRCLE, Newly released from prison, Dallas Cates tries to exact revenge on the Wyoming game warden Joe Pickett, who put him there., by C. J. Box, C J Box, , 0, , Putnam, 9780698410077, 0698410076
## 5                                                                                                                                                    IF NOT FOR YOU, An unlikely relationship is put to the test., by Debbie Macomber, Debbie Macomber, , 0, , Ballantine, 9780553391978, 0553391976
## 6                                                                                                  BIG LITTLE LIES, Who will end up dead, and how, when three mothers with children in the same school become friends?, by Liane Moriarty, Liane Moriarty, , 0, , Berkley, 9780698138636, 0698138635
##                                                                                                         reviews
## 1                                                                                                        , , , 
## 2                                                                                                        , , , 
## 3                                                                                                        , , , 
## 4                                                                                                        , , , 
## 5                                                                                                        , , , 
## 6 https://www.nytimes.com/2014/07/25/books/in-big-little-lies-liane-moriarty-finds-new-complications.html, , ,

Extract Book Details

detailsData <- data.frame()
for(i in 1:nrow(allData)){
detailsData <- rbind(detailsData,as.data.frame(allData$book_details[i]))
}

head(detailsData)
##               title
## 1 MISSISSIPPI BLOOD
## 2         THE SHACK
## 3    BOUND TOGETHER
## 4    VICIOUS CIRCLE
## 5    IF NOT FOR YOU
## 6   BIG LITTLE LIES
##                                                                                                                                                                                      description
## 1 Penn Cage, now the mayor of Natchez, Miss., investigates the secrets of his family and the South in order to save his and his father<U+0092>s lives in the conclusion to the Natchez Burning trilogy.
## 2                                                                                                          A man whose daughter was abducted is invited to an isolated shack, apparently by God.
## 3                                                                                                                        Viktor returns to Sea Haven to see the wife whose stepfather he killed.
## 4                                                                     Newly released from prison, Dallas Cates tries to exact revenge on the Wyoming game warden Joe Pickett, who put him there.
## 5                                                                                                                                                   An unlikely relationship is put to the test.
## 6                                                                                             Who will end up dead, and how, when three mothers with children in the same school become friends?
##           contributor           author contributor_note price age_group
## 1        by Greg Iles        Greg Iles                      0          
## 2 by William P. Young  William P Young                      0          
## 3 by Christine Feehan Christine Feehan                      0          
## 4        by C. J. Box          C J Box                      0          
## 5  by Debbie Macomber  Debbie Macomber                      0          
## 6   by Liane Moriarty   Liane Moriarty                      0          
##              publisher primary_isbn13 primary_isbn10
## 1 Morrow/HarperCollins  9780062311191     0062311190
## 2      Windblown Media  9780964729230     0964729237
## 3              Berkley  9780399583940     0399583947
## 4               Putnam  9780698410077     0698410076
## 5           Ballantine  9780553391978     0553391976
## 6              Berkley  9780698138636     0698138635

Combine Book Details with Book Data and select interesting columns

bookData <- cbind(allData, detailsData)
analyseData <- select(bookData, title, author, publisher, rank, weeks_on_list)
head(analyseData)
##               title           author            publisher rank
## 1 MISSISSIPPI BLOOD        Greg Iles Morrow/HarperCollins    1
## 2         THE SHACK  William P Young      Windblown Media    2
## 3    BOUND TOGETHER Christine Feehan              Berkley    3
## 4    VICIOUS CIRCLE          C J Box               Putnam    4
## 5    IF NOT FOR YOU  Debbie Macomber           Ballantine    5
## 6   BIG LITTLE LIES   Liane Moriarty              Berkley    6
##   weeks_on_list
## 1             1
## 2             9
## 3             1
## 4             1
## 5             1
## 6            32

Analyse Data

Highest number of weeks on list (top 5)

analyseData_weeks <- arrange(analyseData, desc(weeks_on_list))
top_5 <- analyseData_weeks[1:5,]

ggplot(top_5, aes(title,weeks_on_list)) + geom_bar(stat="identity") +theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1))

There are duplicate books in the table indicating a repeat on the best seller list

Using rtimes api

A look at recent news items about best selling fiction. The as_search function searches the New York Times articles.

best_articles <- as_search(q="best+seller+fiction",begin_date='20170101',end_date='20170331',key=API_Key)
best_articles$meta
##   hits time offset
## 1   26   40      0
best_articles$data[1:3]
## [[1]]
## <NYTimes article>Louise Erdrich, Matthew Desmond Among Winners of National Book Critics Circle Awards
##   Type: News
##   Published: 2017-03-17T00:30:03+0000
##   Word count: 699
##   URL: https://www.nytimes.com/2017/03/16/arts/louise-erdrich-matthew-desmond-among-winners-of-national-book-critics-circle-awards.html
##   Snippet: Ms. Erdrich won the fiction award for her novel <U+0093>LaRose,<U+0094> and Mr. Desmond took home the nonfiction award for <U+0093>Evicted: Poverty and Profit in the American City.<U+0094>...
## 
## [[2]]
## <NYTimes article>Neil Gaiman<U+0092>s Myths
##   Type: News
##   Published: 2017-02-17T19:24:15+0000
##   Word count: 577
##   URL: https://www.nytimes.com/2017/02/17/books/review/neil-gaimans-myths.html
##   Snippet: Gaiman discusses <U+0093>Norse Mythology<U+0094>; Sarah Lyall talks about Ali Smith<U+0092>s <U+0093>Autumn<U+0094>; and Nick Bilton on two new books about Silicon Valley....
## 
## [[3]]
## <NYTimes article>Not Yet 30, This Fantasy Writer Is an Old Pro
##   Type: News
##   Published: 2017-03-03T17:03:46+0000
##   Word count: 541
##   URL: https://www.nytimes.com/2017/03/03/books/review/not-yet-30-this-fantasy-writer-is-an-old-pro.html
##   Snippet: <U+0093>A Conjuring of Light,<U+0094> new at No. 6 in hardcover fiction, is V.E. Schwab<U+0092>s 12th book and her third to hit the list....