Assignment 6 Learning a New API

Author

Nathaly Munnicha

Introduction

The Open Movie Database API is an open access version of IMDB that has been built around API functionality. We can a lot of data about movies which includes the title of the movie, when it was released, the different awards it was nominated and what it has won, and even who was casted in the movie (and more!). This data is awesome for anyone who loves movies and maybe want to research their favorite movie or even find out what the next movie they should watch based on genre.

Setup for API

The first step is to obtain your own API key which was easy to do. The first step is to go onto OMDB website (https://www.omdbapi.com/) and head to the API Key section. You will then want to click on the free option (unless you want to become a patron) and sign up with your email and name. Your own API key should be emailed to you pretty quickly.

The website gave us the base url. Where it states the [your key] is where you would replace it with the API Key which you received in your email.

URL:

http://www.omdbapi.com/?apikey=[yourkey]&

The only parameter that is required is either the IMBD ID of the movie or the title of the movie because it needs to know what movie data to extract. For learning purposes (and keeping things simple) we will just be using title for the parameter which is defined as “t=” and the title of the movie afterward the equal sign. Some other parameters you can add to narrow the search includes the type (movie, series, episode) or when it was released.

api_key <- "Your personal API key"
movie_title <- "The Proposal"
endpoint <- "http://www.omdbapi.com/?"
movie_url = paste(endpoint,"apikey=",api_key,"&t=",movie_title, sep="")

Example: Extracting one Movie Data

After having our movie url ready, we first need to download some packages. After acquiring our movie url, we now want to take the data we got from the API and convert into R. We can make the url into a function which will be easier in the future if there are multiple movies you want to extract data from.

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

# Same thing as we did above but now as a function (it will be easier if there are multiple movies that you want to extract data from)

create_movie_url <- 
  function(api_key,movie_title) {
    endpoint <- "http://www.omdbapi.com/?"
    movie_url <- paste(endpoint,"apikey=",api_key,"&t=",URLencode(movie_title), sep="")
    
    return(movie_url)
    
  }

create_movie_url(api_key = "API Key",
                 movie_title= "The Proposal")

Next, we will take the URL link and extract the data and put it into R. We can also make this into a function to make it easier when there are multiple movies’ data you want.

#Taking the data and putting it into R and making it into a function 

movie_url_df <- 
  function(api_key,movie_title) {
    
    movie_url <- 
      create_movie_url(api_key,movie_title)
    
    movie_data <- 
      movie_url %>% 
      GET() %>% 
      content(as = "text",
              encoding = "UTF-8") %>% 
      fromJSON()
    
    return(movie_data)
  }

The OMDB has so much data that is waiting to be retrieved especially if you want to look a multiple movies’ data at once. It also will be easy to compare movies from one another.