R wrapper for YouTube API
Пакет rytstat
предназначен для работы с YouTube API, на данный момент поддерживает работу со следующими API интерфесами:
The rytstat
package for authorization uses the gargle package, the credentials obtained during authorization are stored exclusively on your local PC, you can find out the folder into which the credentials are cached using the ryt_auth_cache_path()
function.
For loading data from your YouTube channel rytstat
needs next scopes:
For more details see Official YouTube API documentation.
The package does not transfer your credentials or data obtained from your advertising accounts to third parties, however, the responsibility for information leakage remains on the side of the package user. The author does not bear any responsibility for their safety, be careful when transferring cached credentials to third parties.
For more details, I recommend that you read the following articles from the official documentation of the gargle package:
You run gads_auth('me@gmail.com')
and start OAuth Dance in the browser:
Upon success, you see this message in the browser:
Authentication complete. Please close this page and return to R.
And you credentials cached locally on your PC in the form of RDS files.
You can use own OAuth app:
app <- httr::oauth_app(appname = "app name", key = "app id", secret = "app secret")
ryt_auth_configure(app = app)
# or from json file
ryt_auth_configure(path = 'D:/ga_auth/app.json')
# run authorization
ryt_auth('me@gmail.com')
Вы можете установить пакет rytstat
из CRAN:
install.packages("rytstat")
или GitHub:
devtools::install_github('selesnow/rytstat')
Для работы с API YouTube вам необходимо создать OAuth клиент в Google Console, и включить все связанные с YouTube API.
Далее для прохождения авторизации используйте приведённый ниже пример:
library(rytstat)
library(httr)
# авторизация
app <- oauth_app(
appname = 'my app',
key = 'ключ вашего приложения',
secret = 'секрет вашего приложения')
ryt_auth_configure(app = app)
ryt_auth(email = 'me@gmail.com')
# запрос списка видео
videos <- ryt_get_video_list()
library(rytstat)
# список видео
videos <- ryt_get_video_list()
# функция для запроса статистики по конкретному видео
get_videos_stat <- function(video_id) {
data <- ryt_get_analytics(
metrics = c('views', 'likes', 'dislikes', 'comments', 'shares'),
filters = str_glue('video=={video_id}')
)
if ( nrow(data) > 0 ) {
data <- mutate(data, video_id = video_id)
}
}
# применяем функцию к каждому видео
video_stat <- purrr::map_df(videos$id_video_id, get_videos_stat)
# объединяем статистику с данными о видео
video_stat <- left_join(video_stat,
videos,
by = c("video_id" = "id_video_id")) %>%
select(video_id,
title,
day,
views,
likes,
dislikes,
comments,
shares)
# auth
ryt_auth('me@gmail.com')
# get reporting data
## create job
ryt_reports_create_job('channel_basic_a2')
## get job list
jobs2 <- ryt_reports_get_job_list()
## get job report list
reports <- ryt_reports_get_report_list(
job_id = jobs$id[1],
created_after = '2021-10-20T15:01:23.045678Z'
)
# get report data
data <- ryt_get_report(
download_url = reports$downloadUrl[1]
)
# delete job
ryt_reports_delete_job(jobs$id[1])
Alexey Seleznev, Head of analytics dept. at Netpeak
Telegram Channel: @R4marketing
email: selesnow@gmail.com
facebook: facebook.com/selesnow
blog: alexeyseleznev.wordpress.com