library(tidyverse) # All the tidy things
library(jsonlite) # Converting json data into data frames
library(magrittr) # Extracting items from list objects using piping grammar
library(httr) # Interacting with HTTP verbs
#| label: Setup endpoint and API key
<- "api_key=[insert your API key]"
api_key <- "insert_movie_name"
movie_title <- paste0("http://www.omdbapi.com/?apikey=[yourkey]", api_key, "&t=", movie_title) omdb_endpoint
Assignment 5: Data from APIs
Overview
The Open Movie Database is an API used to obtain movie/tv information. It includes information about the movie title, year of release, director, genre, and what it is rated.
“Title” - name of movie
“Year” - the year the movie released
“Director” - the director of the movie
“Genre” - the type of movie
“Rated” - the parental guidance suggested
Someone might use this API to provide access to movie data and could be used for creating reviews and recommendations, an easy way to access and integrate information about movies into their own apps/website, or to find more information about the movies in general that you cannot easily generate on your own.
Format
Below are the steps for the OMDb API to capture movie data.
Go to https://www.omdbapi.com/. You can create either a free account where you get a 1,000 daily limit or pay to become a patron to receive an API key and extract data using a JSON file.
First, load all necessary packages. Then, set up the end point and API key. Make sure to replace your API key when using the service. I will be showing a tutorial through movie title, so set up the movie title as well.
Endpoint format:
http://www.omdbapi.com/?apikey=[yourkey]&t=
Below is the code to creating the endpoint format:
Demonstration
<- c("Pretty Woman", "Happy Gilmore", "Top Gun", "Forrest Gump", "Rocky")
movie_titles
#| label: Create a loop to get data
for (title in movie_titles) {
# Encode the movie title
<- URLencode(title, reserved = TRUE)
encoded_title
# Construct endpoint
<- paste0("http://www.omdbapi.com/?apikey=96242ab2", "&t=", encoded_title)
omdb_endpoint
# Get information and format
<-
omdb_data GET(omdb_endpoint) %>%
content(as = "text", encoding = "UTF-8") %>%
fromJSON()
# Generate status messages for each key movie detail
print(paste("Title:", omdb_data$Title))
print(paste("Year:", omdb_data$Year))
print(paste("Director:", omdb_data$Director))
print(paste("Genre:", omdb_data$Genre))
print(paste("Rated:", omdb_data$Rated))
print("------")
# Add a sleep function to accomodate any rate limits
Sys.sleep(5)
}
[1] "Title: Pretty Woman"
[1] "Year: 1990"
[1] "Director: Garry Marshall"
[1] "Genre: Comedy, Romance"
[1] "Rated: R"
[1] "------"
[1] "Title: Happy Gilmore"
[1] "Year: 1996"
[1] "Director: Dennis Dugan"
[1] "Genre: Comedy, Sport"
[1] "Rated: PG-13"
[1] "------"
[1] "Title: Top Gun"
[1] "Year: 1986"
[1] "Director: Tony Scott"
[1] "Genre: Action, Drama"
[1] "Rated: PG"
[1] "------"
[1] "Title: Forrest Gump"
[1] "Year: 1994"
[1] "Director: Robert Zemeckis"
[1] "Genre: Drama, Romance"
[1] "Rated: PG-13"
[1] "------"
[1] "Title: Rocky"
[1] "Year: 1976"
[1] "Director: John G. Avildsen"
[1] "Genre: Drama, Sport"
[1] "Rated: PG"
[1] "------"
This loop ran through the movie titles showing the title, year, director, genre, and what the movie is rated. You are able to change the title to extract information from other movies as well. You can also change the sleep time if you would like, but I spaced the searches into 5 second increments for 5 total searches.
Conclusion
I hope you enjoyed tracking movie information from The Open Movie Database and learned more about movies.