Home Retrieving Western Movies with TMDB API
Post
Cancel

Retrieving Western Movies with TMDB API

Introduction

In this study, an API call will be sent to the The Movie Database (TMDB) servers to retrieve a list of Italian Western Movies. The resulting table will include th first 20 movies with the highest number of user votes.

One can access to the API details by clicking here. The API provides a variety of features to get movies and TV Shows related data such as cast, synopsis, user votes, and country based statistics.

The steps taken during the preparation of this report can be summarized as follows:

  • Receiving an API Key by following the 4 steps addressed here.

  • Setting up an R Markdown file and preparing the environment by installing the related packages.

  • Performing test calls to check whether the environment is working.

  • Calling the base URL’s and extending them with additional queries to obtain more specific results.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Installing the required R packages
# install.packages("httr")
# install.packages("jsonlite")

# Library imports

library("httr")
library("jsonlite")

# Setting the API key

api_key <- "{Insert your API key here}"

# Defining a function to concatenate URL's and query strings easily

extend_URL <- function(URL, query_string){
    
    result <- paste(URL, "&", query_string, sep ="")
    return(result)
    
}

API Call for Genres

First, the list of official genre labels used in the website should be retrieved.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# URL template for the "genre" call

genre <- "https://api.themoviedb.org/3/genre/movie/list?api_key=<<api_key>>"

# Setting the language

genre <- extend_URL(genre, "language=en-US")

# Inserting the API key to the template

genre <- gsub("<<api_key>>", api_key, genre)

# Performing the call

genre_call <- httr::GET(genre)

# Checking the status of the call

genre_call$status_code
1
## [1] 200

The status code implies that the call was successful. The next step is finding out the genre ID of “western”.

API Call for Genres - Results

1
2
3
4
5
6
7
# Converting the call results to an easily readable list

genre_char <- base::rawToChar(genre_call$content)

genre_json <- jsonlite::fromJSON(genre_char, flatten = TRUE)

knitr::kable(genre_json$genres)
idname
28Action
12Adventure
16Animation
35Comedy
80Crime
99Documentary
18Drama
10751Family
14Fantasy
36History
27Horror
10402Music
9648Mystery
10749Romance
878Science Fiction
10770TV Movie
53Thriller
10752War
37Western

API Call for Movie Discovery

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# URL template for the "discover" call

discover <- "https://api.themoviedb.org/3/discover/movie?api_key=<<api_key>>"

# Setting the language

discover <- extend_URL(discover, "language=en-US")

# Retrieve the movies with highest number of votes

discover <- extend_URL(discover, "sort_by=vote_count.desc")

# Filtering by genre (Western's genre id is 37)

discover <- extend_URL(discover, "with_genres=37")

# Filtering by original language (Italian)

discover <- extend_URL(discover, "with_original_language=it")

# Inserting the API key to the template

discover <- gsub("<<api_key>>", api_key, discover)

# Performing the call

discover_call <- httr::GET(discover)

# Checking the status of the call

discover_call$status_code
1
## [1] 200

The call was successful.

API Call for Movie Discovery - Results

1
2
3
4
5
6
7
8
9
10
11
# Converting the call results to an easily readable list

discover_char <- base::rawToChar(discover_call$content)

discover_json <- jsonlite::fromJSON(discover_char, flatten = TRUE)

results <- discover_json$results

# Checking the retrieved field names

colnames(results)
1
2
3
4
5
##  [1] "adult"             "backdrop_path"     "genre_ids"        
##  [4] "id"                "original_language" "original_title"   
##  [7] "overview"          "popularity"        "poster_path"      
## [10] "release_date"      "title"             "video"            
## [13] "vote_average"      "vote_count"

Final Results

Finally, a clean summary table consisting of basic movie information can be generated as follows:

1
2
3
table <- results[c("title", "release_date", "vote_average", "vote_count")]

knitr::kable(table)
titlerelease_datevote_averagevote_count
The Good, the Bad and the Ugly1966-12-238.57156
Once Upon a Time in the West1968-12-218.33646
A Fistful of Dollars1964-01-187.93540
For a Few Dollars More1965-12-188.03333
They Call Me Trinity1970-12-227.61110
Duck, You Sucker1971-10-207.7877
My Name Is Nobody1973-12-137.3792
Trinity Is Still My Name1971-10-217.4762
Django1966-04-067.2739
The Great Silence1968-02-217.5311
God Forgives… I Don’t!1967-10-316.6211
Troublemakers1994-11-256.1189
Ace High1968-10-026.5166
Death Rides a Horse1967-08-317.0155
A Genius, Two Friends, and an Idiot1975-12-166.4133
The Big Gundown1966-11-297.3132
Boot Hill1969-12-206.0132
Zorro1975-03-066.4117
Day of Anger1967-12-197.0115
Keoma1976-11-256.9115

Looks like my favourite one takes the sixth place :-)

IE 442 - Enterprise Information Systems
Boğaziçi University - Industrial Engineering Department GitHub Repository

This post is licensed under CC BY 4.0 by the author.