OMDB API

About the OMDB API

The OMDB Api is a list of movies and their attributes that is helpful for those looking to attain information on movies. I will be using this website and API to create a function that creates a data frame of this information to use for further analysis.

The Steps

The First Step is to load the libraries required to use the API.

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

After loading the libraries, the user must obtain an API key from the following website:https://www.omdbapi.com/apikey.aspx. Enter your email address, First Name, Last Name, and Use case. It will be sent to the entered email and should be used in future steps for identification.

Once you have your API key, start building the endpoint to scrape information. This is also where you would want to think of a movie to get information on.

end<-"https://www.omdbapi.com/?t="
key<-"&apikey=<YOUR API KEY>"
movie<-"<Your Movie Title>"
url<-
  paste(end,movie,key, sep ="")

After establishing an endpoint, selecting a movie, identifying yourself with your API key, and generating a url, you must create a function to translate the text to JSON format.

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

Once in text, we need to condense it into a nice data frame. This is where you can decide which information you want to receive about the movie. In this example, I decided I wanted the IMDB score, the title of the movie, the important actors, and information about awards.

Movie_dataframe<-data.frame(IMDB=Movie_pull$imdbRating,
                            Title=Movie_pull$Title,
                            Actors=Movie_pull$Actors,
                            Awards=Movie_pull$Awards)

Other information you could choose include:

  • Year

  • Rated (like R, PG-13, etc)

  • Released (Release Date)

  • Runtime (in minutes)

  • Genre

  • Director

  • Plot

  • Language

  • Country

  • Poster (link to the poster)

  • Metascore

  • imdbVotes

  • imdbID

  • BoxOffice (in USD)

Demonstration

For this demonstration, I decided on the movie Inception. I entered my api key and movie in the respective codes.

movie_json<-
  paste(end,movie,key, sep ="")
movie_json
[1] "https://www.omdbapi.com/?t=Inception&apikey=533bdea6"

After entering the information, I decided on selecting the IMDB, the title of the movie, the Actors, and the Awards.

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

Movie_dataframe<-data.frame(IMDB = Movie_pull$imdbRating,
                            Title = Movie_pull$Title,
                            Actors = Movie_pull$Actors,
                            Awards = Movie_pull$Awards)
Movie_dataframe %>% select(IMDB,Title, Actors, Awards)
  IMDB     Title                                               Actors
1  8.8 Inception Leonardo DiCaprio, Joseph Gordon-Levitt, Elliot Page
                                          Awards
1 Won 4 Oscars. 160 wins & 220 nominations total