library(tidyverse)
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'dplyr' was built under R version 4.2.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.1 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
library(httr2)
library(jsonlite)
##
## Attaching package: 'jsonlite'
##
## The following object is masked from 'package:purrr':
##
## flatten
We were given the New York Times Developers API and tasked with trying to gather data from the website and turning that data into a Dataframe. My Goal was to just find all the movies with “Sword” in the title from 01/01/1980 - 01/01/2021.
First step is to obtain the API key that was given to me through the website. For future practice I decided to put the key in a .txt file as these API keys are not typically known to everyone in the public setting. After I created 2 variables, “Keyword” for what information they are accessing and “TimeFrame” being the period from the begin to end date that I want to restrict the search too.
#API Key in order to access the NYTimes Database
api_key <- read_lines("APIWeek9Hw.txt")
#What keyword is going to in the title of the Movie
keyword <- "Sword"
#Time Frame has to be formatted YYYY-MM-DD:YYYY-MM-DD Start to End Date
timeframe <-"1980-01-01:2021-01-01"
#Basis on how to search through the database:
#Link to the Keywords and API Overview for Movie Reviews: #https://developer.nytimes.com/docs/movie-reviews-api/1/routes/reviews/search.json/get
search_1 <- paste0("http://api.nytimes.com/svc/movies/v2/reviews/search.json?query=",keyword,"&opening-date=",timeframe,"&api-key=",api_key)
#Importing the Data into a List
result <- fromJSON(search_1)
Basic Transformation is going to happen since the data recieved is in list form and placing the wanted information into a dataframe is easier for analyzation and data manipulation/cleaning.
movies <- as.data.frame(result$results)
movies %>% select(display_title, opening_date)
## display_title opening_date
## 1 Sword of Trust 2019-07-19
## 2 King Arthur: Legend of the Sword 2017-05-12
## 3 Sword Master 2016-12-09
## 4 Memories of the Sword 2015-08-28
## 5 The Butcher, the Chef, and the Swordsman 2011-03-18
## 6 Constantine's Sword 2008-04-18
## 7 Swordfish 2001-06-08
## 8 The Secret of the Sword 1985-03-22
## 9 The Sword and the Sorcerer 1982-04-30