This tutorial will demonstrate how to get an API code from OMDb.com to access their API. From their API, information about movie, televisions shows,reviews, and more can be found. Here is the link to begin: https://www.omdbapi.com/
Instructions
1. Retrieve APi Key
Once on the home page, click “API key” from the black menu bar on the top of the screen.
You will be taken to a page where you will fill out information to be given your own unique API code. First change account type from Patreon to FREE!, then fill in the rest of the fields.
After you press submit, an email will be sent to your inputted email address. This email will contain your unique API key as well as a link with your API already included.
This API is unique to you and is not to be shared.
2. Create URL
To create the URL, you are going to use this outline that can be found under “Usage”:
http://www.omdbapi.com/?apikey=[yourkey]&
This link will be used as the endpoint when we start coding our function.
Scroll down and look at parameters. For this tutorial, we will be using “By Search” and you should see the table below.
These are the parameters that will be used to customize your URL to retrieve the data desired.
Now to create a function in R to output a URL.
Step 1: Load packages from library
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.5.1 ✔ tibble 3.2.1
✔ lubridate 1.9.4 ✔ tidyr 1.3.1
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(httr)library(jsonlite)
Attaching package: 'jsonlite'
The following object is masked from 'package:purrr':
flatten
library(dplyr)
Step 2: Write function using proper syntax
create_OMDb_url <-function(api_key, movie_name) {# Defining endpoint OMDb_endpoint <-"http://www.omdbapi.com/?"# Start with API key api_key <-paste("apikey=", api_key, sep="")# then add movie name movie_name <-URLencode(movie_name, reserved=TRUE)# this ensure that movie titles with spaces are placed into the URL properly movie_name_t <-paste("&t=", movie_name,sep="")# combine to make URL OMDb_api_url <-paste(OMDb_endpoint, api_key,movie_name_t, sep="")return(OMDb_api_url)}
Step 3: Test out the code to ensure it is working properly before moving on. Example test code can be seen below:
url <-create_OMDb_url(api_key, "Now You See Me")print(url)
Now that we have a function to create a URL, we can create a function to put your desired data into a data frame.
Step 1: Write function to output data using proper syntax/
request_OMDb_url_df <-function(api_key, movie_name) {# Start by using the function created above to get URL: OMDb_api_url <-create_OMDb_url(api_key, movie_name)#to create data frame and populate OMDb_data <- OMDb_api_url %>%GET() %>%content(as ="text",encoding ="UTF-8") %>%fromJSON() # to get data to be outputted return(OMDb_data) }
Step 2: Test out the function to ensure there are no mistakes in code. Example code can be seen below:
data <-request_OMDb_url_df(api_key = api_key, movie_name ="Now You See Me")
Output will look something like this but instead of reading a csv, use the print(data) function to output results.
Rows: 3 Columns: 26
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (21): Title, Rated, Released, Runtime, Genre, Director, Writer, Actors, ...
dbl (3): Year, Metascore, imdbRating
num (1): imdbVotes
lgl (1): Response
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
print(output)
# A tibble: 3 × 26
Title Year Rated Released Runtime Genre Director Writer Actors Plot Language
<chr> <dbl> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 Now … 2013 PG-13 31 May … 115 min Crim… Louis L… Ed So… Jesse… An F… English…
2 Now … 2013 PG-13 31 May … 115 min Crim… Louis L… Ed So… Jesse… An F… English…
3 Now … 2013 PG-13 31 May … 115 min Crim… Louis L… Ed So… Jesse… An F… English…
# ℹ 15 more variables: Country <chr>, Awards <chr>, Poster <chr>,
# Ratings.Source <chr>, Ratings.Value <chr>, Metascore <dbl>,
# imdbRating <dbl>, imdbVotes <dbl>, imdbID <chr>, Type <chr>, DVD <chr>,
# BoxOffice <chr>, Production <chr>, Website <chr>, Response <lgl>
How these functions can be applied
The data retrieved from the OMDb API can be used in for many applications. It can be used for the educational purpose of demonstrating how to use an API. In addition, it can be used to do analysis of movie or show reviews for specific genres or years. Lastly, this data could potentially be used by streaming services to determine which movies or televisions shows to include on their service or which ones to offer in the future.