R/VideoDev.R

Defines functions bc_oauth bc_pull yt_oauth yt_get_statistics yt_get_all_statistics yt_get_search yt_get_channel_info yt_get_channel_video_stats yt_most_recent_videos yt_get_playlist_stats

Documented in bc_oauth bc_pull yt_get_all_statistics yt_get_channel_info yt_get_channel_video_stats yt_get_playlist_stats yt_get_search yt_get_statistics yt_most_recent_videos yt_oauth

library(dplyr)
library(httr)
library(jsonlite)

#  BRIGHTCOVE DATA PULL
#
#
#   Get Brightcove Token
bc_oauth <- function(key, secret){
  r <- httr::POST("https://oauth.brightcove.com/v3/access_token", body = list(grant_type = "client_credentials"),
            httr::authenticate(key, secret), encode = "form")

  token <- httr::content(r)$access_token
  return(token)
}

# Pull Data From Brightcove
bc_pull <- function(ids, account = NULL, key = NULL,
                    secret = NULL, from=Sys.Date()-30, to=Sys.Date()){

  # Check for missing data
  if(is.null(ids)) { stop("Missing Brightcove IDs") }
  if(is.null(account)) { stop("Missing Account ID") }
  if(is.null(key) | is.null(secret)) { stop("Missing Key/Secret") }


  ids <- unique(trimws(ids))
  bcData <- data.frame()

  #Loop Through All IDS
  for(i in 1:length(ids)){

    #Get New Token
    if(i %% 100 == 1){
      token <- bc_oauth(key,secret)
    }

    url <- paste0("https://analytics.api.brightcove.com/v1/data?accounts=",account,"&dimensions=video&where=video==",
                  ids[i],"&access_token=",token,"&from=",from,"&to=",to,"&fields=video_view,video_hours:video_seconds_viewed/3600")

    response <- httr::GET(url,encode="form")


    if(httr::content(response)$item_count != 0){
      dat <- as.data.frame(httr::content(response)$items[[1]])
      bcData <- dplyr::bind_rows(bcData,dat)
    }


  }
  return(bcData)
}


#  YOUTUBE DATA PULL
#
#
#

yt_oauth <- function(appKey,AppSecret){
  myapp <- httr::oauth_app("google", key, secret)
  token <- httr::oauth2.0_token(oauth_endpoints("google"),
                          myapp,
                          scope=c("https://www.googleapis.com/auth/youtube.readonly",
                                  "https://www.googleapis.com/auth/yt-analytics.readonly"))
  return(token)
}

yt_get_statistics <- function(videoID) {
  url <- paste0("https://www.googleapis.com/youtube/v3/videos?part=statistics&id=",videoID)
  res <- httr::GET(url, token)
  fin <- as.data.frame(content(res)$items)
  fin$etag <- NULL
  fin$kind <- sub("youtube#","", fin$kind)
  colnames(fin) <- gsub("id\\.|snippet.","",colnames(fin))
  return(fin)
}

yt_get_all_statistics <- function(videoIDs) {
  allStats <- data.frame()
  for(i in 1:length(videoIDs)){
    temp <- yt_get_statistics(videoIDs[i])
    allStats <- dplyr::bind_rows(allStats,temp)
    rm(temp)
  }
  return(allStats)
}

yt_get_search <- function(term, maxResults = 10){
  term <- trimws(term)
  term <- tolower(term)
  term <- gsub(" ","+",term)

  url <- paste0("https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=",maxResults,"&q=",term)
  req <- httr::GET(url, token)
  fin <- jsonlite::fromJSON(paste0(rawToChar(req$content)))$items
  fin <- do.call(data.frame,fin)

  fin[,c("kind","etag","snippet.thumbnails")] <- list(NULL)
  if("id.kind" %in% colnames(fin)){
    fin$id.kind <- sub("youtube#","",fin$id.kind)
  }
  colnames(fin) <- gsub("id\\.|snippet.","",colnames(fin))
  return(fin)
}

yt_get_channel_info <- function(IDS) {
  IDS <- trimws(IDS)
  fantastisk <- data.frame()
  for(i in 1:length(IDS)){
    url <- paste0("https://www.googleapis.com/youtube/v3/channels?part=snippet,statistics&id=",IDS[i])
    req <- httr::GET(url, token)
    result <- paste(rawToChar(req$content),collapse = "")
    document <- jsonlite::fromJSON(result)
    df <- document$items$statistics
    df$channelId <- IDS[i]
    fantastisk <- dplyr::bind_rows(fantastisk,df)
  }
  return(fantastisk)
}

yt_get_channel_video_stats <- function(channelID, playlists) {
  vidIDs <- character()
  for(i in 1:length(playlists)){
    url <- paste0("https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&playlistId=",playlists[i],"&maxResults=50")
    req <- httr::GET(url, token)
    fin <- jsonlite::fromJSON(paste0(rawToChar(req$content)))
    vidIDs <- c(vidIDs,fin$items$contentDetails$videoId)
    pageToken <- fin$nextPageToken

    while(!is.null(pageToken)){
      url <- paste0("https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&playlistId=",playlists[i],"&maxResults=50&pageToken=",pageToken)
      req <- httr::GET(url, token)
      fin <- jsonlite::fromJSON(paste0(rawToChar(req$content)))
      vidIDs <- c(vidIDs,fin$items$contentDetails$videoId)
      pageToken <- fin$nextPageToken
    }
  }
  return(vidIDs)
}

yt_most_recent_videos <- function(channelID, maxResults=1){
  channelID <- trimws(channelID)
  fantastisk <- data.frame()
  for(i in 1:length(channelID)){
    url <- paste0("https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=",channelID[i],"&maxResults=",maxResults,"&order=date&type=video")
    req <- httr::GET(url,token)
    fin <- jsonlite::fromJSON(paste0(rawToChar(req$content)))
    if(!is.null(fin$item$snippet$publishedAt)){
      df <- fin$item$snippet[,1:2]
      fantastisk <- dplyr::bind_rows(fantastisk,df)
    }
  }
  return(fantastisk)
}

yt_get_playlist_stats <- function(channelID){
  playlistIDs <- character()
  url <- paste0("https://www.googleapis.com/youtube/v3/playlists?part=contentDetails&channelId=", channelID, "&maxResults=50")
  req <- httr::GET(url,token)
  fin <- jsonlite::fromJSON(paste0(rawToChar(req$content)))
  pageToken <- fin$nextPageToken
  playlistIDs <- c(playlistIDs,fin$items$id)
  while(!is.null(pageToken)){
    url <- paste0("https://www.googleapis.com/youtube/v3/playlists?part=contentDetails&channelId=", channelID, "&maxResults=50&pageToken=",pageToken)
    req <- httr::GET(url,token)
    fin <- jsonlite::fromJSON(paste0(rawToChar(req$content)))
    pageToken <- fin$nextPageToken
    playlistIDs <- c(playlistIDs,fin$items$id)
  }
  return(playlistIDs)
}
davisj95/VideoDev documentation built on March 1, 2020, 5:46 a.m.