create_OMDb_df <-
function(api_key, movie_title){
# this function only requires your API key and a movie title as input
URL_endpoint <- "http://www.omdbapi.com/?"
# This loads in the base URL
api_key <- paste("apikey=", api_key, sep = "")
# load in your specific API key
movie_title <- URLencode(movie_title, reserved = TRUE)
# makes movie titles URL safe
paramters <- paste("&t=", movie_title, sep = "")
# load in our specific search
URL <- paste(URL_endpoint, api_key, paramters, sep = "")
# combine everything to create URL
OMDb_data <-
URL %>%
GET() %>%
content(as = "text", encoding = "UTF-8") %>%
fromJSON()
# converts our URL into a data frame in R
return(OMDb_data)
}How to Use the OMDb API
The OMDb API
This API uses the OMDb databases to get information on different movies and TV shows. This API allows us to find information on movie or TV titles, year of publication, type, movie poster, description, genre, director and so much more.
This API can be very useful for film enthusiast and many more. This API can be used to find useful information about films and TV shows that can be used to make recommendations, research on different titles, and finding specific details about a title.
This document intends to give a brief overview on how to use the OMDb API, including instructions on set up both on the OMDb site, and in R programming. There will then be some examples of what can be done with this API in R.
Getting Your OMDb API Key
The OMDb API is free to use. TO use this API, you first must register to get a unique API key that will be used to call for different information from the database. Follow these simple steps to get you API key:
Navigate to the OMDb API site at https://www.omdbapi.com/
Click on the “API Key” tab on the top of the screen
Select the “FREE” account type and fill out the information required (email, name, and brief description of intended use)
You will then receive an email with your own unique API key
Setting up a Function in R to Use Data
Now that we have our API key, we need to set everything up in R so we can start making data requests. To do this, we can use the format provided to us in the OMDb API tutorial on the homepage of the website.
All data requests have start with this endpoint:
http://www.omdbapi.com/?apikey=[yourkey]&
From there, you can add different parameters with IMDb ID or movie title. You can use these parameters laid out on the website to look for one specific movie with an in depth description using the IMDb id or using the “t” parameter in your URL. You can also look for a list of movies containing a certain title using the parameter “s” in your URL. For this setup, we will use the “t” parameter to return one film.
To keep this as efficient as possible, we will create a function in R that will create a data frame with the movies that we want selected.
This function makes it easy to use to look for movies with different titles. As mentioned earlier, it can be tweaked with the parameters to find lists of movies containing that title.
Making a Call Using OMDb API
Now that this function is created. We can start using the API and making calls with the data. Since we already have a function created, this is pretty easy. This will output a list of movies containing the title that you searched for. From here, we can find information like director.
star_wars <-
create_OMDb_df(api_key = your_api_key, # use your API key
movie_title = "star wars") # title of movie
print(star_wars)$Title
[1] "Star Wars: Episode IV - A New Hope"
$Year
[1] "1977"
$Rated
[1] "PG"
$Released
[1] "25 May 1977"
$Runtime
[1] "121 min"
$Genre
[1] "Action, Adventure, Fantasy"
$Director
[1] "George Lucas"
$Writer
[1] "George Lucas"
$Actors
[1] "Mark Hamill, Harrison Ford, Carrie Fisher"
$Plot
[1] "Luke Skywalker joins forces with a Jedi Knight, a cocky pilot, a Wookiee and two droids to save the galaxy from the Empire's world-destroying battle station, while also attempting to rescue Princess Leia from the mysterious Darth ..."
$Language
[1] "English"
$Country
[1] "United States"
$Awards
[1] "Won 6 Oscars. 69 wins & 30 nominations total"
$Poster
[1] "https://m.media-amazon.com/images/M/MV5BOGUwMDk0Y2MtNjBlNi00NmRiLTk2MWYtMGMyMDlhYmI4ZDBjXkEyXkFqcGc@._V1_SX300.jpg"
$Ratings
Source Value
1 Internet Movie Database 8.6/10
2 Rotten Tomatoes 93%
3 Metacritic 90/100
$Metascore
[1] "90"
$imdbRating
[1] "8.6"
$imdbVotes
[1] "1,474,225"
$imdbID
[1] "tt0076759"
$Type
[1] "movie"
$DVD
[1] "N/A"
$BoxOffice
[1] "$460,998,507"
$Production
[1] "N/A"
$Website
[1] "N/A"
$Response
[1] "True"
Making a Recommendation Using R
We can now use the simple function we have made, and manipulate it a bit to get more useful requests. For example, we can use this API to make movie recommendations.
To do this, we will create another function that will ask for API key and favorite movie. This function will then return a list of movies that are directed by the same director as their favorite movie.
find_movies <-
function(api_key, fav_movie){
# First we use our Function from earlier to get information on that movie
fav_movie_data <-
create_OMDb_df(api_key, fav_movie)
# Next we need to identify that director
# use URL encode to make sure director is URL safe
director <- URLencode(fav_movie_data$Director, reserved = TRUE)
# Now we can search for movies with that director
# We can not use our Function from earlier, but we can do it by hand
URL_director <- paste("http://www.omdbapi.com/?s=", director, "&apikey=", api_key, sep = "") # creates URL
director_movies <-
URL_director %>%
GET() %>%
content(as = "text", encoding = "UTF-8") %>%
fromJSON() %>%
use_series(Search)
return(director_movies)
}
# Example call:
find_movies(your_api_key, "Inception") Title
1 The Director's Notebook: The Cinematic Sleight of Hand of Christopher Nolan
2 180°: Christopher Nolan Interviews Al Pacino
3 Every Christopher Nolan Movie Ranked
4 Christopher Nolan & Richard Donner: A Conversation
5 Creating Interstellar: A Discussion with Christopher Nolan, Anne Hathaway, Mathew McConaughey, and Jessica Chastain
6 Memento: Interview with Christopher Nolan
7 Christopher Nolan on Francis Bacon
8 Untitled Christopher Nolan Universal Project
9 Christopher Nolan Coffee Ads
Year imdbID Type
1 2007 tt1035445 movie
2 2002 tt0357402 movie
3 2024 tt32204353 movie
4 2013 tt3138288 movie
5 2015 tt5297444 movie
6 2004 tt5328982 movie
7 2013 tt12502076 movie
8 2026 tt33764258 movie
9 2013 tt2980668 movie
Poster
1 https://m.media-amazon.com/images/M/MV5BMWZkYWYzMGUtYzdhZS00MGQwLWE1ODAtYzcwNzc2YmE3NzNkXkEyXkFqcGdeQXVyNzQzNzQxNzI@._V1_SX300.jpg
2 N/A
3 https://m.media-amazon.com/images/M/MV5BZDA4OGU5OTEtOTAwYi00NTU4LWFiOGEtZTMxOWUzNDBjNTEwXkEyXkFqcGdeQXVyMTcwNTQ2ODE1._V1_SX300.jpg
4 https://m.media-amazon.com/images/M/MV5BZDY4YzNkOWUtN2U5NC00MTJiLWI3MmQtYmU4YjA5NzQ4MjU3L2ltYWdlL2ltYWdlXkEyXkFqcGdeQXVyNjE0OTE2MDY@._V1_SX300.jpg
5 N/A
6 N/A
7 N/A
8 N/A
9 N/A