library(tidyverse)
library(jsonlite)
library(magrittr)
library(httr)OMDb API
Movie Database
Data information
The data was collected from OMDb API which ia site that allows people to get their own api address and explore their film data. In order to access the data collection, you must request your on personal API key that is not to be shared with others. Once this is completed, the data can be collected and organized. This data provides information about all films produced that are considered action movies. This would benefit a person who interested in learning more about the actions movies. The data shows the title, movie id, type, and year it was produced.
First Steps
In order to run the data properly, you must download the packages.
Running Code
This code provides a data set with all the actions movies and the year they were produced.
# function creation
#| label: Action Movie
search_url_fun <- function(search_term, page, api_key = "YOUR AIP KEY", type = "movie") {
base_url <- "https://www.omdbapi.com/"
query_params <- list(
apikey = api_key,
s = search_term,
type = type,
page = page)
response <- GET(base_url, query = query_params)
content_data <- content(response, as = "text", encoding = "UTF-8") %>%
fromJSON()
if (!is.null(content_data$Search)) {
return(content_data$Search)
} else {
return(NULL)
}
}
data_set <- data.frame()
for (page_num in 1:10) {
message("DATA COLLECTION IN PROCESS: Page ", page_num)
data_collect <- search_url_fun(search_term = "action", page = page_num)
if (!is.null(data_collect)) {
data_set <- bind_rows(data_set, data_collect)
} else {
message("No data found on page ", page_num)
break
}
Sys.sleep(3) # To adhere to API rate limits
}
# Remove duplicates based on 'imdbID'
data_set <- data_set[!duplicated(data_set$imdbID), ]
# Display the collected data
print(paste("Total movies collected:", nrow(data_set)))[1] "Total movies collected: 0"
print(head(data_set, 5))data frame with 0 columns and 0 rows
Implication
This shows all information about a chosen movie. In this example, I selected Iron Man. The code provides a list of rows that provide information such as the year it was produced, the languages it was produced in, and popular actors that starred in the movie.
title_url_fun <- function(title, api_key = "YOUR API KEY", type = "movie") {
create_url <- "https://www.omdbapi.com/"
query_params <- list(
apikey = api_key,
t = title,
type = type
)
response <- GET(create_url, query = query_params)
content_data <- content(response, as = "text", encoding = "UTF-8") %>%
fromJSON()
if (!is.null(content_data) && content_data$Response == "True") {
return(content_data)
} else {
return(NULL)
}
}
title_data <- title_url_fun(title = "Iron Man")
print(title_data)NULL