Assignment6

Author

Samantha Krah

API Overview:

The service OMDb API is an alternative for IMDb. It allows users to retrieve detailed info about movies, tv shows, and actors. Ideal for movie buffs and data analysts, which would be great for genre trends or building recommendations.

for all the things:

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(httr)
library(magrittr)

Attaching package: 'magrittr'

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

    set_names

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

    extract

API key needed: I just used my school email and they sent me a code. In the email, it also gave me the API url that I was supposed to use with my key in it already.

omdb_endpoint <- "http://www.omdbapi.com/?i=tt3896198&apikey=478964ff"

fields <- paste0("&fields=Title",
                "Year",
                "Genre",
                "Director",
                "imdbRating",
                sep = ",")

Making the url with the endpoint and fields.

omdb_api_url <- 
  paste(omdb_endpoint,fields,sep = "")
omdb_api_url
[1] "http://www.omdbapi.com/?i=tt3896198&apikey=478964ff&fields=TitleYearGenreDirectorimdbRating,"

using GET

omdb_response <- GET(omdb_endpoint) 

getting the data from the GET response

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

I wanted to only see the ratings for the movie it provided so I pulled the rating data frame

omdb_api_data %>% 
  use_series(Ratings) %>% 
  view()

omdb_df <- 
  omdb_api_url %>% 
  GET() %>% 
  content(as = "text",
          encoding = "UTF-8") %>% 
  fromJSON() %>% 
  use_series(Ratings)
ratings_df <- omdb_api_data %>% use_series(Ratings)

write_csv(ratings_df, "omdb_ratings.csv")

This shows how to connect the OMDb API using R, retrieve movie data, and extract the ratings.