To install the latest version from CRAN:
The latest development version of the package will always be on GitHub. Instructions for installing the package from Github are provided below.
Next, load the package:
To get going, get the application id and password from Google
Developer Console (see 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.
yt_oauth("998136489867-5t3tq1g7hbovoj46dreqd6k5kd35ctjn.apps.googleusercontent.com", "MbOSt6cQhhFkwETXKur-L9rN")
## Waiting for authentication in browser...
## Press Esc/Ctrl + C to abort
## Authentication complete.
## No. of Views 525112
## No. of Likes 5576
## No. of Dislikes 3564
## No. of Favorites 0
## No. of Comments 5329
## 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
## 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
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:
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