Movies API

Author

Billy Jackson

Introduction to OMDb API

The Open Movie Database (OMDb) API is a RESTful web service. It allows you to get information about different movies, TV shows, and video games.

OMDb provides its return data in JSON format, and has a paid and free version of its API.

This service makes it easy to avoid having to manually find this information about whatever type of entertainment you are trying to find, and allows you to get quick results to display wanted output. In this example, I wanted information about one of my favorite movies, No Country For Old Men.

Getting Individual API Key

To get your API key and the wanted movie link click here -> https://www.omdbapi.com/

In the top ribbon you will see a tab labeled “API Key” takes you to the page that you can get your free API key. Input your email address. Your personal API key will be sent your email.

Save that email, as you will need to copy your API key for your own API call.

Utilizing API in R

This section pulls together the URL creation into one function with our putting of the data into the data frame. The output is the title, rating, date of release, genre, headline actors, the plot and the awards won. Within the code you can see further explanation.

# Packages needed 
library(tidyverse)
library(jsonlite)
library(magrittr)
library(httr)
library(knitr)
library(kableExtra)
library(dplyr)


movie_request <- function(api_key) {
  
# empty data frame
  movie_df <- data.frame()
  
# On the OMDb page you can put in the movie you want to search for along with the  year it came out. That is how I got the movie portion. Input your personal API.
  
  url <- paste("https://www.omdbapi.com/?apikey=", api_key,"&t=No+Country+For+Old+Men&y=2007", sep = "")

# To do the movie_info portion, you need your url above. To do this, all you need to do is put your API key into the link as done above. Copy this link into Firefox to see the information about the movie, and you can change the wanted information as you want and as I have done below. 
  
  movie <-
    url %>% 
    GET() %>%
    content(as = "text", encoding = "UTF-8") %>%
    fromJSON()
    
# I only want to see this information about the movie. Depending on what you want to see, change as needed.  
   movie_info <- data.frame(
    Title = movie$Title,
    Rated = movie$Rated,
    Released = movie$Released,
    Genre = movie$Genre,
    Actors = movie$Actors,
    Plot = movie$Plot,
    Awards = movie$Awards)
  
    
  movie_df <- bind_rows(movie_df, movie_info)
  
  return(movie_df)
}

movie_info <- movie_request(api_key)

movie_info %>% 
   kable(caption = "Movie Information: No Country for Old Men") %>% 
  kable_styling() %>%
  column_spec(1:ncol(movie_info), color = "red")  # Apply red color to all columns. Did for no reason other than in RStudio the output was black on black.
Movie Information: No Country for Old Men
Title Rated Released Genre Actors Plot Awards
No Country for Old Men R 21 Nov 2007 Crime, Drama, Thriller Tommy Lee Jones, Javier Bardem, Josh Brolin Violence and mayhem ensue after a hunter stumbles upon the aftermath of a drug deal gone wrong and over two million dollars in cash near the Rio Grande. Won 4 Oscars. 165 wins & 139 nominations total