--- title: "Using tuber" author: "Gaurav Sood" date: "`r Sys.Date()`" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Using tuber} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ``` ## tuber: Access YouTube from R ### Install, Load the package To install the latest version from CRAN: ```{r, eval=FALSE, install} install.packages("tuber") ``` The latest development version of the package will always be on GitHub. Instructions for installing the package from Github are provided below. ```{r, eval=FALSE, install_g} # install.packages('devtools') devtools::install_github("soodoku/tuber", build_vignettes = TRUE) ``` Next, load the package: ```{r, eval=FALSE, loadlib} library(tuber) ``` ### Using the package To get going, get the application id and password from Google Developer Console (see [https://developers.google.com/youtube/v3/getting-started](https://developers.google.com/youtube/v3/getting-started)). Enable YouTube APIs. Create OAuth credentials, being sure to select 'Other' as your Application Type. Then set the application id and password via the `yt_oauth` function. For more information about YouTube OAuth, see [YouTube OAuth Guide](https://developers.google.com/youtube/v3/guides/authentication). ```{r, eval=FALSE, auth} yt_oauth("998136489867-5t3tq1g7hbovoj46dreqd6k5kd35ctjn.apps.googleusercontent.com", "MbOSt6cQhhFkwETXKur-L9rN") ``` ```{r, eval=FALSE, prints} ## Waiting for authentication in browser... ## Press Esc/Ctrl + C to abort ## Authentication complete. ``` #### Get Statistics of a Video ```{r, eval=FALSE, getstats} get_stats(video_id="N708P-A45D0") ``` ``` ## No. of Views 525112 ## No. of Likes 5576 ## No. of Dislikes 3564 ## No. of Favorites 0 ## No. of Comments 5329 ``` #### Get Information About a Video ```{r, eval=FALSE, getdetails} get_video_details(video_id="N708P-A45D0") ``` #### Get Caption of a Video ```{r, eval=FALSE, getcaptions} get_captions(video_id="yJXTXN4xrI8") ``` ``` ## ## ## Every four seconds, ## someone is diagnosed with ## Alzheimer&#39;s disease. ## It&#39;s the most common cause of dementia, ## affecting over 40 million people worldwide, ## and yet finding a cure is something that still ## eludes researchers today. ## Dr. Alois Alzheimer, a German psychiatrist, ## first described the symptoms in 1901 ## when he noticed that a particular hospital patient ## had some peculiar problems, ## including difficulty sleeping, ## disturbed memory, drastic mood changes, ## and increasing confusion. ## When the patient passed away, ## Alzheimer was able to do an autopsy ## and test his idea that perhaps ## her symptoms were caused by irregularities ## in the brain&#39;s structure. ## What he found beneath the microscope ## were visible differences in brain tissue ## in the form of misfolded proteins ## called plaques, ## and neurofibrillary tangles. ## Those plaques and tangles work together ## to break down the brain&#39;s structure. ## Plaques arise when another protein ## in the fatty membrane surrounding nerve cells ## gets sliced up by a particular enzyme, ## resulting in beta-amyloid proteins, ## which are sticky and have a tendency ## to clump together. ## That clumping is what forms the things ## we know as plaques. ## These clumps block signaling ## and, therefore, communication ## between cells, and also seem to trigger ## immune reactions that cause the destruction ## of disabled nerve cells. ## In Alzheimer&#39;s disease, neurofibrillary tangles ## are built from a protein known as tau. ## The brain&#39;s nerve cells contain a network of tubes ## that act like a highway for food molecules ## among other things. ## Usually, the tau protein ensures that these tubes ## are straight, allowing molecules ## to pass through freely. ## But in Alzheimer&#39;s disease, ## the protein collapses into twisted strands or tangles, ## making the tubes disintegrate, ## obstructing nutrients from reaching the nerve cell ## and leading to cell death. ## The destructive pairing of plaques and tangles ## starts in a region called the hippocampus, ## which is responsible for forming memories. ## That&#39;s why short-term memory loss ## is usually the first symptom of Alzheimer&#39;s. ## The proteins then progressively invade ## other parts of the brain, ## creating unique changes that signal ## various stages of the disease. ## At the front of the brain, ## the proteins destroy the ability to process logical thoughts. ## Next, they shift to the region that controls emotions, ## resulting in erratic mood changes. ## At the top of the brain, ## they cause paranoia and hallucinations, ## and once they reach the brain&#39;s rear, ## the plaques and tangles work together ## to erase the mind&#39;s deepest memories. ## Eventually the control centers governing ## heart rate and breathing are overpowered as well ## resulting in death. ## The immensely destructive nature of this disease ## has inspired many researchers to look for a cure ## but currently they&#39;re focused on slowing its progression. ## One temporary treatment ## helps reduce the break down of acetylcholine, ## an important chemical messenger in the brain ## which is decreased in Alzheimer&#39;s patients ## due to the death of the nerve cells that make it. ## Another possible solution is a vaccine ## that trains the body&#39;s immune system to attack ## beta-amyloid plaques before they can form clumps. ## But we still need to find an actual cure. ## Alzheimer&#39;s disease was discovered ## more than a century ago, ## and yet still it is not well understood. ## Perhaps one day we&#39;ll grasp ## the exact mechanisms at work behind this threat ## and a solution will be unearthed. ## ## ``` #### Search Videos ```{r, eval=FALSE, searchvids} res <- yt_search("Barack Obama") head(res[, 1:3]) ``` ``` ## publishedAt channelId title ## 1 2016-09-06T11:20:20.000Z UC6CZwQv8cbZco3wOwtp7W2g See How Obama Ignored Xi Jinping & Welcomed PM Modi At G20 Summit ! ## 2 2016-09-05T06:01:17.000Z UC6RJ7-PaXg6TIH2BzZfTV7w PM Modi Meets Barack Obama at G20 Summit 2016 ## 3 2008-03-24T16:31:53.000Z UC4o-h3-3GhrmHB6ytgO3oIQ Top 20 Obama Pastor Comments You Never Saw ## 4 2016-09-03T06:43:38.000Z UC5aeU5hk31cLzq_sAExLVWg LIVE: Obama arrives in China for G20 summit ## 5 2016-09-20T01:30:42.000Z UCmWHDwXFvdKc8OVBbb2dMZg Buy Lot of 20 Obama, 44th U.S. President, 56th Presidential Inauguration coins! ## 6 2016-09-03T06:57:05.000Z UCgrNz-aDmcr2uuto8_DL2jg US President Obama arrives in Hangzhou for G20 Summit ``` #### Get Comments on a video ```{r, eval=FALSE, get_comments} res <- get_comment_threads(c(video_id="N708P-A45D0")) head(res) ``` ``` ## authorDisplayName authorProfileImageUrl authorChannelUrl ## 1 Tony Rx https://lh5.googleusercontent.com/-OL_t2gRZ8RI/AAAAAAAAAAI/AAAAAAAAACg/tXnqTDNYXec/photo.jpg?sz=50 http://www.youtube.com/channel/UCqKvEBPMtbmh020d56WrQXQ ## 2 Kevin Manning https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50 http://www.youtube.com/channel/UCEF-WytcLvgijQIklAbteUw ## 3 Kevin Manning https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50 http://www.youtube.com/channel/UCEF-WytcLvgijQIklAbteUw ## 4 TheDrunkenRocketMan https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50 http://www.youtube.com/channel/UCvTypSrsihX3fr2zCZivmwA ## 5 pangratata https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50 http://www.youtube.com/channel/UCETB8ILZueyFMCFWMF0p3Yg ## 6 pangratata https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50 http://www.youtube.com/channel/UCETB8ILZueyFMCFWMF0p3Yg ``` ### Get statistics of all the videos in a channel ```{r, eval=FALSE, get_channel_vids_stats} a <- list_channel_resources(filter = c(channel_id = "UCT5Cx1l4IS3wHkJXNyuj4TA"), part="contentDetails") # Uploaded playlists: playlist_id <- a$items[[1]]$contentDetails$relatedPlaylists$uploads # Get videos on the playlist vids <- get_playlist_items(filter= c(playlist_id=playlist_id)) # Video ids vid_ids <- as.vector(vids$contentDetails.videoId) # Function to scrape stats for all vids get_all_stats <- function(id) { get_stats(id) } # Get stats and convert results to data frame res <- lapply(vid_ids, get_all_stats) res_df <- do.call(rbind, lapply(res, data.frame)) head(res_df) ``` If you need to find a channel ID from a `username`, the `list_channel_resources` function can help. You can use it like: ```{r, eval=FALSE} res = list_channel_resources(filter = c(username = "GoogleDevelopers"), part="id") # Parse out channel_id if(!is.null(res$items[[1]]$id)){ channel_id <- res$items[[1]]$id } else { stop("User not found") } # channel_id should be UC_x5XG1OV2P6uZZ5FSM9Ttw ``` ``` ## id viewCount likeCount dislikeCount favoriteCount commentCount ## 1 91gZ4taDiDE 906 4 1 0 0 ## 2 bHPCvSqTxn4 706 0 0 0 0 ## 3 h2UPH87kjhc 458 1 0 0 0 ## 4 E2VtxjljZCE 391 0 0 0 0 ## 5 5Ajfk620fA0 175000 5 0 0 0 ## 6 PdI3HjulcA4 575 3 2 0 3 ```