Youtube Stats

Author

Danny Neugebauer

Assignment 6: Learning a New API

Introduction:

The YouTube Data API is a tool that lets developers and researchers access data from YouTube directly in R. It can retrieve information about videos, channels, playlists, and comments. For example, you can use it to collect statistics like view counts, likes, and upload dates for any public video. This kind of data can be useful for studying trends, analyzing audience engagement, or tracking the performance of specific creators.

Setup/Walkthrough

  • Get an API Key

    • Log in/create a Google account

    • Go to https://console.cloud.google.com/welcome/new

    • Select create a new project in the top left

    • In the left menu go to “APIs & Services -> Library”

    • Search for “Youtube Data API v3” and enable

    • Next go to credentials in the left sidebar

    • Click “+ Create Credentials” -> “API key”

    • Google will generate a new key and show it in a pop up

Packages

  • After creating your own R script install these packages

    library(httr)
    library(jsonlite)
    library(dplyr)
    
    Attaching package: 'dplyr'
    The following objects are masked from 'package:stats':
    
        filter, lag
    The following objects are masked from 'package:base':
    
        intersect, setdiff, setequal, union

Making an API Call

We’ll use our API key to send a request to the YouTube Data API. The code will retrieve the statistics and basic details for a video using its unique video id. By adjusting the id, you can change what video is being analyzed.

library(httr)
library(jsonlite)

api_key <- "YOUR_API_KEY_HERE" # put your key here when testing locally
video_id <- "dQw4w9WgXcQ"      # example video

url <- paste0(
  "https://www.googleapis.com/youtube/v3/videos?",
  "part=statistics,snippet&id=", video_id,
  "&key=", api_key
)

res <- GET(url)
stop_for_status(res)
data <- fromJSON(content(res, as = "text", encoding = "UTF-8"))


video_data <- data.frame(
title    = data$items$snippet$title,
views    = as.numeric(data$items$statistics$viewCount),
likes    = as.numeric(data$items$statistics$likeCount),
comments = as.numeric(data$items$statistics$commentCount)
)

video_data # Safe placeholder so the document knits without an API key