Working with OMDB API

Assignment 5

Author

Meredith Briggs

OMDB API Overview

This document will set up and use the API for the Open Movie Database (OMDB). This can be used to pull information about movies like ratings, year released, director and more from the database.

Setting Up the API

Begin by applying for an api key at https://www.omdbapi.com/. You should be able to receive a free key without creating any accounts.

Now I will create a function that will generate a URL using my API key that will pull information from the database. I have created an object defining my API key separate from the URL. Below are the packages used in this demo.

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
library(knitr)     # Publishing on rpubs 

The structure of the endpoint includes your API key and a search of the movie title. You could also include type (movies, series, episode) and year made but I am just going to focus on the titles. The below block creates a function that will generate a URL based on your API and movie title input.

create_omdb_url <- function(api_key,movie_title){
  omdb_endpoint <- "http://www.omdbapi.com/"
  omdb_api_key <- paste("?apikey=", api_key, sep = "") 
  omdb_title <- paste("&t=", URLencode(title, reserved = TRUE), sep = "") # safely encodes titles
  omdb_url <- paste(omdb_endpoint, omdb_api_key, omdb_title, sep = "")
  return(omdb_url)
}

Using the API

Now that we can generate a URL we are able to pull information in order to create a data table. Given the current season, I want to know how long each HArry Potter movie is so I can know when to start my annual binge of these movies. Using the API, I will be looking up a list of the Harry Potter movies and import that into a data frame.

movie_hp <- c(
  "Harry Potter and the Sorcerer's Stone",
  "Harry Potter and the Chamber of Secrets",
  "Harry Potter and the Prisoner of Azkaban",
  "Harry Potter and the Goblet of Fire",
  "Harry Potter and the Order of the Phoenix",
  "Harry Potter and the Half-Blood Prince",
  "Harry Potter and the Deathly Hallows – Part 1",
  "Harry Potter and the Deathly Hallows – Part 2"
)

for (title in movie_hp) {
  # Create URL
  omdb_url <- create_omdb_url(my_api_key, title)
  
  # Check URL created
  #print(omdb_url)
  
  # Retreive information and create data table
  omdb_data <- 
  GET(omdb_url) %>% 
  content(as = "text", encoding = "UTF-8") %>% 
  fromJSON()
  
   # Generate status messages for each movie
  print(paste("Title:", omdb_data$Title))
  print(paste("Year:", omdb_data$Year))
  print(paste("Run_Time:", omdb_data$Runtime))
  print(paste("........"))
  
  # Add a sleep function to avoid any rate limits
  Sys.sleep(5)
}
[1] "Title: Harry Potter and the Sorcerer's Stone"
[1] "Year: 2001"
[1] "Run_Time: 152 min"
[1] "........"
[1] "Title: Harry Potter and the Chamber of Secrets"
[1] "Year: 2002"
[1] "Run_Time: 161 min"
[1] "........"
[1] "Title: Harry Potter and the Prisoner of Azkaban"
[1] "Year: 2004"
[1] "Run_Time: 142 min"
[1] "........"
[1] "Title: Harry Potter and the Goblet of Fire"
[1] "Year: 2005"
[1] "Run_Time: 157 min"
[1] "........"
[1] "Title: Harry Potter and the Order of the Phoenix"
[1] "Year: 2007"
[1] "Run_Time: 138 min"
[1] "........"
[1] "Title: Harry Potter and the Half-Blood Prince"
[1] "Year: 2009"
[1] "Run_Time: 153 min"
[1] "........"
[1] "Title: Harry Potter and the Deathly Hallows: Part 1"
[1] "Year: 2010"
[1] "Run_Time: 146 min"
[1] "........"
[1] "Title: Harry Potter and the Deathly Hallows: Part 2"
[1] "Year: 2011"
[1] "Run_Time: 130 min"
[1] "........"

As we can see, the result is a list of each movie, the year it was released, and the runtime. There are many other variables and movies that the database provides that we can look into but these were the relevant variables to my interests.