Data 607: Week 9 - Working with Web APIs

1. Using GET & content()

library(httr)
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
library(jsonlite)

#Search for movie reviews. Supports filtering by Critics' Pick = yes
nyt_url <- "https://api.nytimes.com/svc/movies/v2/reviews/search.json?critics-pick=Y&api-key=ymXgsVwAmDUoqwyh8GBv0Y07qINjVGh1"
nyt_movie_reviews <- GET(nyt_url)
nyt_movie_reviews
## Response [https://api.nytimes.com/svc/movies/v2/reviews/search.json?critics-pick=Y&api-key=ymXgsVwAmDUoqwyh8GBv0Y07qINjVGh1]
##   Date: 2022-04-03 20:37
##   Status: 200
##   Content-Type: application/json; charset=utf-8
##   Size: 15.4 kB
movie_reviews <- content(nyt_movie_reviews, "text")
review_results <- fromJSON(movie_reviews)
#this returned data frames within my data frame which gave some trouble removing columns
review_results <- review_results$results
head(review_results,2)
##        display_title mpaa_rating critics_pick              byline
## 1 You Won't Be Alone           R            1 Jeannette Catsoulis
## 2  Babi Yar. Context                        1          A.O. Scott
##                                                            headline
## 1                  ‘You Won’t Be Alone’ Review: Season of the Witch
## 2 ‘Babi Yar: Context’ Review: Unearthing Footage of a Nazi Massacre
##                                                                                                                             summary_short
## 1                                       A supernaturally altered young woman learns how to be human in this mesmerizing folk-horror tale.
## 2 Sergei Loznitsa’s new documentary, about the mass murder of Ukrainian Jews in 1941, arrives in theaters with a grim context of its own.
##   publication_date opening_date        date_updated link.type
## 1       2022-03-31   2022-04-01 2022-03-31 14:22:05   article
## 2       2022-03-31         <NA> 2022-03-31 11:06:02   article
##                                                                  link.url
## 1 https://www.nytimes.com/2022/03/31/movies/you-wont-be-alone-review.html
## 2  https://www.nytimes.com/2022/03/31/movies/babi-yar-context-review.html
##                               link.suggested_link_text     multimedia.type
## 1 Read the New York Times Review of You Won't Be Alone mediumThreeByTwo210
## 2  Read the New York Times Review of Babi Yar. Context mediumThreeByTwo210
##                                                                                                                           multimedia.src
## 1 https://static01.nyt.com/images/2022/04/01/arts/youwont1/merlin_204493155_5e8ea473-28fd-4fc4-9473-76f85bb6707d-mediumThreeByTwo440.jpg
## 2                          https://static01.nyt.com/images/2022/04/01/arts/31babi-yar-context/31babi-yar-context-mediumThreeByTwo440.jpg
##   multimedia.height multimedia.width
## 1               140              210
## 2               140              210

2. Using fromJSON() directly

##using fromJSON() directly, no GET()
movie_reviews_results <- fromJSON(nyt_url,flatten=TRUE) %>%
    data.frame()
movie_reviews_results <- movie_reviews_results[-c(1:4,7,14,16:17,19:20)]
colnames(movie_reviews_results) <- c("Movie_Name","MPAA_Rating", "Review_Author","Review_Headline","Short_Summary_Review","Review_Date","Movie_OpeningDate","Review_Last_Modified","Review_URL","Image_URL")
movie_reviews_results$Review_Date <- as.Date(movie_reviews_results$Review_Date)
movie_reviews_results$Movie_OpeningDate <- as.Date(movie_reviews_results$Movie_OpeningDate)
movie_reviews_results$Review_Last_Modified <- as.Date(movie_reviews_results$Review_Last_Modified)
head(movie_reviews_results,2)
##           Movie_Name MPAA_Rating       Review_Author
## 1 You Won't Be Alone           R Jeannette Catsoulis
## 2  Babi Yar. Context                      A.O. Scott
##                                                     Review_Headline
## 1                  ‘You Won’t Be Alone’ Review: Season of the Witch
## 2 ‘Babi Yar: Context’ Review: Unearthing Footage of a Nazi Massacre
##                                                                                                                      Short_Summary_Review
## 1                                       A supernaturally altered young woman learns how to be human in this mesmerizing folk-horror tale.
## 2 Sergei Loznitsa’s new documentary, about the mass murder of Ukrainian Jews in 1941, arrives in theaters with a grim context of its own.
##   Review_Date Movie_OpeningDate Review_Last_Modified
## 1  2022-03-31        2022-04-01           2022-03-31
## 2  2022-03-31              <NA>           2022-03-31
##                                                                Review_URL
## 1 https://www.nytimes.com/2022/03/31/movies/you-wont-be-alone-review.html
## 2  https://www.nytimes.com/2022/03/31/movies/babi-yar-context-review.html
##                                                                                                                                Image_URL
## 1 https://static01.nyt.com/images/2022/04/01/arts/youwont1/merlin_204493155_5e8ea473-28fd-4fc4-9473-76f85bb6707d-mediumThreeByTwo440.jpg
## 2                          https://static01.nyt.com/images/2022/04/01/arts/31babi-yar-context/31babi-yar-context-mediumThreeByTwo440.jpg