#Pulling in the URL for OMDb
omdb_url <- "http://img.omdbapi.com/"
movie_title <- URLencode("Pulp Fiction") # You can change it to whatever
# API Key
#api_key <- "api_key" # You never want to leave your API visable The Open Movie Database API
Introduction
I am using the IMBD API because I want to pull information about movies and see what was the trend in certain years. Someone would use this API because it could help analysts and data enthusiasts. They can use it to gather movie data for research, such as trends in genres, box office success, or ratings distribution. An example of this could be a data project analyzing IMDb scores across different genres or decades.
Overview of the API’s Website
We need to start and go to the website which is found here: https://www.omdbapi.com.
This gives us good information about the API and how to set it up along with getting your API key. Setting up the API key is very easy. You’ll need to do the following:
Register for a free API key. The free key provides up to 1,000 requests per day. If you would like to pay for more requests, there are paid versions as well.
Once you are registered, you will receive an API key via email. You will need to go to your email and verify and authenticate the key.
Setting up the API within R
The first step is to bring the appropriate packages and pull in the URL, which “httr” package is the one we need.
Then you need to pull in the URL and your api key. Remember to replace “api_key” with your own.
Here is an example that you could test http://www.omdbapi.com/?t=Inception&r=json&apikey=“your_api_key”. Replace with your API key and test it in a web browser. This is a JSON response containing details about the movie, such as its title, year, director, and plot.
Then if that works we want to start and create an endpoint. The endpoint should look like this: http://www.omdbapi.com/?apikey=%yourkey%&
With that we can pull in the data.
#Endpoint layout
omdb_endpoint <- paste0("http://www.omdbapi.com/?t=", movie_title, "&apikey=", api_key)Make the API Request
The next thing you do is make a request with the API. We do this with one of the most important functions which is the GET function. When interacting with an API, use “GET” from the HTTR package. This will bring in the data from the API.
Next, once you pull it in we can test the status code. The status code 200 indicates successful which is what we want to make sure it is running.
#Then get and pull in the data
omdb_data <-
GET(omdb_endpoint) %>%
content(as = "text", encoding = "UTF-8") %>%
fromJSON()
#print(paste("Status Code:", status_code(omdb_endpoint)))Demonstration of How It Works
movie_title <- URLencode("Pulp Fiction")
omdb_endpoint <- paste0("http://www.omdbapi.com/?t=", movie_title, "&apikey=", api_key)
#Then get and pull in the data
omdb_data <-
GET(omdb_endpoint) %>%
content(as = "text", encoding = "UTF-8") %>%
fromJSON()
print(paste("Title:", omdb_data$Title))[1] "Title: Pulp Fiction"
print(paste("Year:", omdb_data$Year))[1] "Year: 1994"
print(paste("Plot:", omdb_data$Plot))[1] "Plot: The lives of two mob hitmen, a boxer, a gangster and his wife, and a pair of diner bandits intertwine in four tales of violence and redemption."
This gives basic information about the title, the year it came out, and little about the plot. I picked Pulp Fiction as the movie, but you could choose any movie you want.
This calls the API to bring in information about Pulp Fiction, but you can do this for any movie and can do multiple movies at a time if you would like by doing a loop and creating a function.
Example of Function and Looping
# Define a list of movie titles
movie_titles <- c("Pulp Fiction", "Inception", "The Matrix")
# Initialize your API key and create a function for API requests
#api_key <- "api_key"
# Function to get movie data for each title
get_movie_data <- function(title) {
encoded_title <- URLencode(title) # Handle spaces and special characters
omdb_endpoint <- paste0("http://www.omdbapi.com/?t=", encoded_title, "&apikey=", api_key)
response <- GET(omdb_endpoint)
if (status_code(response) == 200) { # Check if the request was successful
movie_data <- content(response, as = "parsed", type = "application/json")
if (movie_data$Response == "True") { # Check if the movie was found
print(paste("Title:", movie_data$Title))
print(paste("Year:", movie_data$Year))
print(paste("Plot:", movie_data$Plot))
} else {
print(paste("Movie not found:", title))
}
} else {
print(paste("Failed to retrieve data for:", title))
}
}
# Loop through the list of movie titles and call the API for each
for (title in movie_titles) {
get_movie_data(title)
}[1] "Title: Pulp Fiction"
[1] "Year: 1994"
[1] "Plot: The lives of two mob hitmen, a boxer, a gangster and his wife, and a pair of diner bandits intertwine in four tales of violence and redemption."
[1] "Title: Inception"
[1] "Year: 2010"
[1] "Plot: A thief who steals corporate secrets through the use of dream-sharing technology is given the inverse task of planting an idea into the mind of a C.E.O., but his tragic past may doom the project and his team to disaster."
[1] "Title: The Matrix"
[1] "Year: 1999"
[1] "Plot: When a beautiful stranger leads computer hacker Neo to a forbidding underworld, he discovers the shocking truth--the life he knows is the elaborate deception of an evil cyber-intelligence."
This loops through the movies in the movie_titles and find the title, the year, and a little about the plot in each one. That is a demonstration of how to loop and create a function by using an API