Homework 6: Explore your own API

Assignment 6: Exploring an API

The API I chose to work with in this assignment is the OMDB API, which works as a database for movies.

Explanation

Using this API, we can search for information on specific movies or shows. This will be useful for people who want to do research on all the different info, stats, or ratings that are stored on OMDB.

Packages Needed

These packages will be necessary for executing the following steps.

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.2     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.1.0     
── 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(jsonlite)

Attaching package: 'jsonlite'

The following object is masked from 'package:purrr':

    flatten
library(dplyr)
library(httr)

Setup

OMDB_api_key <- "145204e0"

Here we are storing our api key so we can easily use it if we want to use multiple requests. I just requested an API key with my school email, and they sent me one.

OMDB_url <- "https://www.omdbapi.com/?"

This is our URL for the OMDB API website before we filter it down to a specific movie/show.

In this next section, we decide what movie/series we want to see information on, and for this example, we will use Inception.

movie_title = "Inception"

Now we can set up our URL based on what we wanted to search for above.

OMDB_inception_url <- paste0(OMDB_url,"t=", movie_title,"&apikey=", OMDB_api_key)

This will produce a URL that we can use for our next steps.

Get Request

We will use GET function and then format the data from the response.

response <- GET(OMDB_inception_url)

OMDB_data <- 
  response %>% 
  content(as = "text",
          encoding = "UTF-8") %>% 
  fromJSON() 

Now we have our data, and we can see the ratings for Inception.

OMDB_data %>% 
  view()
#Those ratings arent too shabby!

It will display three unique rows for Inception because OMDB has stored reviews on Inception from 3 different movie reviewers. You can see this in the Ratings.Source column.