Assignment 6

Author

Drew Vonder Meulen

Service & Intent

I chose to use the Open Movie Database as my demonstration for this assignment. Someone may want to use this service to find quick sums of information about movies. Whether they want to know the title, year, rating, release date, run time, IMDb rating and more, they are able to access this information and write code to find results. I find this example very interesting as you can learn about certain movies in relation to the material in the class.

Walkthrough

I began by going to the OMDB website and requesting an API key from the service. To request I gave them my email and requested the free version. I received the API key and copied the URL. To set up in R I pasted my key and used a GET function to put the URL and the API key together.

library(tidyverse) 
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.6
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.1     ✔ tibble    3.3.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.2
✔ purrr     1.2.1     
── 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(magrittr)  

Attaching package: 'magrittr'

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

    set_names

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

    extract
library(httr)
api_key <- "64bde048"

omdb <- GET(paste("https://www.omdbapi.com/?i=tt3896198&apikey=", api_key), sep = "") %>%
  content(as = "text",
          encoding = "UTF-8") %>%
  fromJSON()

Retrieval of movie information

I start with a function and title, then use the base URL, my api key, and t to put together a full URL. I then create a response using a GET to grab the information from the JSON properly. Next I have a place where the move titles can be entered and returned will be a dataframe with some interesting results about the movie. We achieve this by creating a loop where it grabs title, year, genre, director, IMDb rating, and the plot.

get_movie <- function(title) {
  
  base_url <- "https://www.omdbapi.com/?"
  apikey <- paste("apikey=", api_key, sep = "")
  t <- paste("&t=", title, sep = "")
  
  full_url <- paste(base_url, apikey, t, sep = "")
  
  response <- GET(full_url) %>%
    content(as = "text", encoding = "UTF-8") %>%
    fromJSON()
  
  return(response)
}

titles <- c("cars", "cars_2", "cars_3", "planes", "turbo")

movie_df <- data.frame()

for (title in titles) {
  
  movie <- get_movie(title)
  
  movie_row <- data.frame(
    Title = movie$Title,
    Year = movie$Year,
    Genre = movie$Genre,
    Director = movie$Director,
    Rating = movie$imdbRating,
    Plot = movie$Plot
  )
  
  movie_df <- rbind(movie_df, movie_row)
}

movie_df
   Title Year                        Genre                      Director Rating
1   Cars 2006 Animation, Adventure, Comedy      John Lasseter, Joe Ranft    7.3
2 Cars 2 2011 Animation, Adventure, Comedy John Lasseter, Bradford Lewis    6.2
3 Cars 3 2017 Animation, Adventure, Comedy                     Brian Fee    6.7
4 Planes 2013 Animation, Adventure, Comedy                     Klay Hall    5.7
5  Turbo 2013 Animation, Adventure, Comedy                   David Soren    6.4
                                                                                                                                                                                                                                       Plot
1                                                                                   On the way to the biggest race of his life, a hotshot rookie race car gets stranded in a rundown town and learns that winning isn't everything in life.
2 Star race car Lightning McQueen and his pal Mater head overseas to compete in the World Grand Prix race. But the road to the championship becomes rocky as Mater gets caught up in an intriguing adventure of his own: international e...
3                                                                                                                         Lightning McQueen sets out to prove to a new generation of racers that he's still the best race car in the world.
4                                                                                                                         A cropdusting plane with a fear of heights lives his dream of competing in a famous around-the-world aerial race.
5                                                                                                                                A freak accident might just help an everyday garden snail achieve his biggest dream: winning the Indy 500.
Title Year Genre Director Rating
Cars 2006 Animation, Adventure, Comedy John Lasseter, Joe Ranft 7.3
Cars 2 2011 Animation, Adventure, Comedy John Lasseter, Bradford Lewis 6.2
Cars 3 2017 Animation, Adventure, Comedy Brian Fee 6.7
Planes 2013 Animation, Adventure, Comedy Klay Hall 5.7
Turbo 2013 Animation, Adventure, Comedy David Soren 6.4