BAIS-462 Assignment 5

API: The Open Movie Database (OMDb)

Author

Sadie Liptak

Overview

For this assignment, we will be taking a look at the open move database API. This API is web service that collects movie information like content titles, year released, director, genre, and film rating. We will use the API to take a deep dive in movies and TV shows. I will show you how to set it up, making a call to the API, and save the results as a cvs file as well as knit the report as an html file.

Set up

  1. First, go to the OMDb API website (https://www.omdbapi.com/) and look towards the top grey bar at the top of the webpage.
  2. On the left side of the page, there are six tabs. Click the one furthest to the right labeled “API Key”.
  3. Once here, sign up for a free API key by clicking “FREE! (1,000 daily limit)” and typing in your email before hitting submit.
  4. An email will be sent to you with an API key that you can use. Once your key has been delivered, you’re ready to go!
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

movie_api_key <- "your_api_key"
movie_title <- "insert_movie_title"
omdb_api_endpoint <- paste0("http://www.omdbapi.com/?t=", movie_title, "&apikey=", movie_api_key)

After downloading the packages, creating the URL and defining the API key you can get the information from the API.

omdb_api_response <- 
  omdb_api_endpoint %>%
  GET() %>%
  content(as = "text",
              encoding = "UTF-8") %>% 
      fromJSON() 

Demonstration

movie_titles <- c("Legally Blonde", "High School Musical", "Hello Dolly", "Camp Rock")

#| label: Create a loop to get data
for (title in movie_titles) {
  encoded_title <- URLencode(title, reserved = TRUE)
  
  omdb_api_endpoint <- paste0("http://www.omdbapi.com/?apikey=96242ab2", "&t=", encoded_title)
  
  omdb_api_data <- 
  GET(omdb_api_endpoint) %>% 
  content(as = "text", encoding = "UTF-8") %>% 
  fromJSON()


print(paste("Title:", omdb_api_data$Title))
print(paste("Year:", omdb_api_data$Year))
print(paste("Director:", omdb_api_data$Director))
print("------")

Sys.sleep(5)

}
[1] "Title: Legally Blonde"
[1] "Year: 2001"
[1] "Director: Robert Luketic"
[1] "------"
[1] "Title: High School Musical"
[1] "Year: 2006"
[1] "Director: Kenny Ortega"
[1] "------"
[1] "Title: Hello Dolly"
[1] "Year: 2005"
[1] "Director: N/A"
[1] "------"
[1] "Title: Camp Rock"
[1] "Year: 2008"
[1] "Director: Matthew Diamond"
[1] "------"

Summary

Hopefully this is helping in guiding you along the way to setting up and using the OMDb API. This format can be transferred to many other API’s. Although, many have different processes. The demonstration shows the selected movies and their title, release year, and director.