#|label: setup
#|echo: FALSE
library(httr)
library(jsonlite)
library(dplyr)
library(readr)OMDb API Project
OMDb API Overview
The Open Movie Database is an open access version of the IMDB that is built around the API functionality. The OMDb is useful because it allows a user to pull movies into a data frame using different criteria, such as the title, the year released, and the plot of the movie. A user can filter however they please so they can obtain the exact results they’re looking for. This gives a user the opportunity to discover new movies to watch based on certain criteria that they are expecting to have, which is what will be demonstrated in this project.
OMDb API Setup
This API requires a relatively simple setup. There is only one required piece of the request and that is either the movie’s title or the movie’s IMDb ID.
API Request
# Start with an endpoint and API Key
omdb_endpoint <-"http://www.omdbapi.com/?apikey="
api_key <- "insert_your_key"The website asks a user to request their own personal key, which can be inserted above
# Creating function for a GET request
get_movie <- function(title, plot = "short") {
movie_response <- GET(
omdb_endpoint,
query = list(
apikey = api_key,
t = title,
type = "movie",
plot = plot
)
)
movie_data <- movie_response |>
content(as = "text", encoding = "UTF-8") |>
fromJSON()
return(movie_data)
}#Pulling movie data into a data frame
get_movie("Inception")$Response
[1] "False"
$Error
[1] "Invalid API key!"
inception_shortplot <- get_movie("Inception")
inception_longplot <- get_movie("Inception", plot = "full")
Inception<- data.frame(
imdb_id = inception_shortplot$imdbID,
title = inception_shortplot$Title,
year = inception_shortplot$Year,
genre = inception_shortplot$Genre,
director = inception_shortplot$Director,
short_plot = inception_shortplot$Plot,
long_plot = inception_longplot$Plot
)Any movie in the IMDb can be requested using this code, the user would input any movie title they want into the “get_movie” function and create a data frame from there.
# Example of the movie request not being valid
fake_movie<- get_movie("blah blah blah blah blah")
#R will create a list of 2 for a false request giving an error that the movie was not found. This makes it easy for a user to spot an error quickly rather than trying to create a data frame that won't work.