Last active
December 8, 2021 12:42
-
-
Save beatrizmilz/69906dadb5093d7279d3b5f4898a8438 to your computer and use it in GitHub Desktop.
Código em R para buscar vídeos de um canal na twitch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# AUTENTICAR NA TWITCH VIA R -------------------------------------------- | |
# O que está na documentação: | |
# GET https://id.twitch.tv/oauth2/authorize | |
# ?client_id=<your client ID> | |
# &redirect_uri=<your registered redirect URI> | |
# &response_type=<type> | |
# &scope=<space-separated list of scopes> | |
# Adaptando isso para o que precisamos... | |
# Criar um app no site da twitch dev. | |
# Salvar o client ID no renviron. | |
t_client_id <- Sys.getenv("TWITCH_CLIENT_ID") | |
t_redirect_uri <- "http://localhost" | |
t_scopes <- "chat:read" | |
t_auth <- | |
httr::GET( | |
paste0( | |
"https://id.twitch.tv/oauth2/authorize?client_id=", | |
t_client_id, | |
"&redirect_uri=", | |
t_redirect_uri, | |
"&response_type=token&scope=", | |
t_scopes | |
) | |
) | |
httr::content(t_auth) | |
# Com o link que aparece como resposta, logar via navegador. | |
# A pagina que o navegador irá direcionar contém o token na URL. | |
# Copiar o token e salvar no renviron como "TWITCH_CLIENT_TOKEN". | |
# Buscar videos de um user ------------------ | |
library(magrittr) | |
login <- "gmhikaru" #nome do canal que queremos buscar | |
t_get_user <- | |
httr::GET( | |
url = paste0('https://api.twitch.tv/helix/users?login=', login), | |
httr::add_headers( | |
Authorization = paste("Bearer", Sys.getenv("TWITCH_CLIENT_TOKEN")), | |
"Client-Id" = Sys.getenv("TWITCH_CLIENT_ID") | |
) | |
) | |
t_content_user <- httr::content(t_get_user) | |
t_user_id <- t_content_user$data[[1]]$id | |
t_get_videos_user <- | |
httr::GET( | |
url = paste0( | |
'https://api.twitch.tv/helix/videos?user_id=', | |
t_user_id, | |
"&first=100" | |
), | |
httr::add_headers( | |
Authorization = paste("Bearer", Sys.getenv("TWITCH_CLIENT_TOKEN")), | |
"Client-Id" = Sys.getenv("TWITCH_CLIENT_ID") | |
) | |
) | |
t_content_video_user <- httr::content(t_get_videos_user) | |
t_videos <- t_content_video_user %>% | |
purrr::pluck("data") %>% | |
purrr::map(unlist, recursive = TRUE) %>% | |
purrr::map(tibble::enframe) %>% | |
purrr::map_dfr(~ dplyr::mutate(.x, dplyr::across(.fns = as.character)), | |
.id = "ordem") %>% | |
tidyr::pivot_wider( | |
names_from = name, | |
values_from = value, | |
names_repair = "unique", | |
values_fn = list | |
) %>% | |
janitor::clean_names() %>% | |
dplyr::mutate(dplyr::across(.fns = as.character)) | |
dplyr::glimpse(t_videos) | |
# dplyr::glimpse(t_videos) | |
# Rows: 100 | |
# Columns: 17 | |
# $ ordem <chr> "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12… | |
# $ id <chr> "1227547482", "1226632577", "1226157471", "1224778769", "122… | |
# $ stream_id <chr> "44716352285", "44704729645", "44697909533", "44681837965", … | |
# $ user_id <chr> "103268673", "103268673", "103268673", "103268673", "1032686… | |
# $ user_login <chr> "gmhikaru", "gmhikaru", "gmhikaru", "gmhikaru", "gmhikaru", … | |
# $ user_name <chr> "GMHikaru", "GMHikaru", "GMHikaru", "GMHikaru", "GMHikaru", … | |
# $ title <chr> "World Champs Game 10 | !merch !shaker ", "World Champs Game… | |
# $ description <chr> "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", … | |
# $ created_at <chr> "2021-12-08T12:08:15Z", "2021-12-07T12:06:18Z", "2021-12-06T… | |
# $ published_at <chr> "2021-12-08T12:08:15Z", "2021-12-07T12:06:18Z", "2021-12-06T… | |
# $ url <chr> "https://www.twitch.tv/videos/1227547482", "https://www.twit… | |
# $ thumbnail_url <chr> "", "https://static-cdn.jtvnw.net/cf_vods/d1m7jfoe9zdc1j/dc3… | |
# $ viewable <chr> "public", "public", "public", "public", "public", "public", … | |
# $ view_count <chr> "0", "304895", "206229", "389308", "136973", "456973", "2456… | |
# $ language <chr> "en", "en", "en", "en", "en", "en", "en", "en", "en", "en", … | |
# $ type <chr> "archive", "archive", "archive", "archive", "archive", "arch… | |
# $ duration <chr> "33m18s", "8h3m10s", "3h4m40s", "8h11m0s", "3h48m10s", "8h46… |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment