IMDB API Key

Author

Erin McLaughlin

IMDB API

We will be using an API that looks at movie information. This includes an ID, Title, Type (movie, series…), year, and short or full plot. We can take this API to ask questions about a movie and how to better understand differences between successful and unsuccessful movies. This specific API uses two different search tools to get two different kinds of information about movies.

Steps to Access API

This API is free via https://www.omdbapi.com/.

All you have to do is request the free API Key and give then your name, email, and reason for using the API. Then all of your requests to the API are connected to your specific API key. This key is personal and should not be published.

Using the API in R

The first step is to download the required packages required for the functions to work. We will use four packages: tidyverse, jsonlite, magrittr, and httr.

We then identify our API key, so that we can start asking questions to the OMDb

We then build a URL and use the GET function. Below is an example of the basics of accessing the api. However there are no requests being made below.

movie_endpoint<-
  paste("http://www.omdbapi.com/?", api_key, sep="")

#get the information
movie_data<-
  GET(movie_endpoint) %>% 
  content(as = "text", 
          encoding = "UTF-8") %>% 
  fromJSON()

Below are the basic steps to access an api. You create the url with whatever specific filters you so desire. (that can be done within the api’s ability)

#you could hard code values and paste them together to create a URL that could be used to make a request to the api. 
title<- "&t"
type<- "&type=movie"
year_made<- "&y=2016"
show_plot<-"&plot= full"
search<- "&s=love"
movie_api_url <- 
  paste(movie_endpoint,title,type,year_made, sep ="")


# this function allows us to create a url that then can be searched with the GET function to retrieve information like Title, year, ID, type, and poster (link)
odb_search<- 
  function(search_criteria){
    movie_endpoint<-
  paste("http://www.omdbapi.com/?", api_key, sep="")
    search<- paste("&s=",search_criteria, sep="")
    odb_search_url<- paste(movie_endpoint, search, sep="")
    return(odb_search_url)
  }

#This is us using the function to create an api where we are searching more any movie that has the word Love in the title
movies_of_love<-
  odb_search("love")

Below we are using the url we created to GET information from the api and turn it into a drata frame that can be interacted with.

love_movie<-
  GET(movies_of_love) %>% 
  content(as = "text", 
          encoding = "UTF-8") %>% 
  fromJSON() %>% 
  use_series(Search) 

#turn the request into a data frame
love_movie_frame<-
  as.data.frame(love_movie)

#Below is basic information about the first show that can then be used on the other searching parameters of the API 
print(paste("Title:", love_movie_frame$Title[1]))
[1] "Title: Crazy, Stupid, Love."
print(paste("ID:", love_movie_frame$imdbID[1]))
[1] "ID: tt1570728"

Because of the API we are using we can take the ID from the data frame we just created and get more information about a specific movie in that data frame. We get more info about their critic scores and a summary of the plot.

#look up info abount the first movie given in the dataset above

more_info_example<-
  paste(movie_endpoint, "&i=", love_movie_frame$imdbID[1],sep="")


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

#this will show you a table of information about the movie and it is separated by the three different ratings it recieved from movie critics

crazy_stupid_api_frame<-
  as.data.frame(crazy_stupid_api)

print(paste("Title:", crazy_stupid_api_frame$Title[1]))
[1] "Title: Crazy, Stupid, Love."
print(paste("Plot:", crazy_stupid_api_frame$Plot[1]))
[1] "Plot: A middle-aged husband's life changes dramatically when his wife asks him for a divorce. He seeks to rediscover his manhood with the help of a newfound friend, Jacob, learning to pick up girls at bars."

Summary

This should hopefully help you set up and use the OMDb API key. Using the basic structures we went over you should be able to create a URL that can request information from the OMDb API and get it in a database form.