Skip to content

Instantly share code, notes, and snippets.

@adelmofilho
Created October 21, 2018 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adelmofilho/cb028fc5d6dbc8e08a1695de5bfc2a54 to your computer and use it in GitHub Desktop.
Save adelmofilho/cb028fc5d6dbc8e08a1695de5bfc2a54 to your computer and use it in GitHub Desktop.
Extração de dados dos vídeos do canal do Pirula
library(stringr)
library(dplyr)
library(RJSONIO)
library(magrittr)
isNull <- function(x){
if (is.null(x)) { y <- NA }
else { y <- x }
return(y)
}
key <- "<api_key>"
channel <- "UCdGpd0gNn38UKwoncZd9rmA"
url <- paste0("https://www.googleapis.com/youtube/v3/search?",
"channelId=", channel,
"&key=",key,
"&part=snippet",
"&maxResults=1",
"&type=video")
x <- fromJSON(url, simplify = FALSE)
videos_n <- x$pageInfo$totalResults
# Primeiro Vídeo Request ----
video_id <- x$items[[1]]$id$videoId
video_time <- substr(x$items[[1]]$snippet$publishedAt, start = 1, stop = 10)
video_title <- x$items[[1]]$snippet$title
url_video <- paste0("https://www.googleapis.com/youtube/v3/videos?",
"id=", video_id,
"&key=",key,
"&part=snippet,contentDetails,statistics,status")
y <- fromJSON(url_video, simplify = FALSE)
video_duration_raw <- y$items[[1]]$contentDetails$duration
viewCount <- isNull(y$items[[1]]$statistics$viewCount)
likeCount <- isNull(y$items[[1]]$statistics$likeCount)
dislikeCount <- isNull(y$items[[1]]$statistics$dislikeCount)
favoriteCount <- isNull(y$items[[1]]$statistics$favoriteCount)
commentCount <- isNull(y$items[[1]]$statistics$commentCount)
# video_duration <- duration(video_duration_raw)
# Geração da tabela de registro dos resultados ----
dados <- data.frame(title = video_title,
time = video_time,
duration = video_duration_raw,
viewCount = viewCount,
likeCount = likeCount,
dislikeCount = dislikeCount,
favoriteCount = favoriteCount,
commentCount = commentCount,
stringsAsFactors = FALSE)
# Acesso aos demais vídeos ----
for (i in 1:(videos_n-2)) {
url <- paste0("https://www.googleapis.com/youtube/v3/search?",
"channelId=", channel,
"&key=",key,
"&part=snippet",
"&maxResults=1",
"&pageToken=",x$nextPageToken,
"&type=video")
x <- fromJSON(url, simplify = FALSE)
video_id <- x$items[[1]]$id$videoId
video_time <- substr(x$items[[1]]$snippet$publishedAt, start = 1, stop = 10)
video_title <- x$items[[1]]$snippet$title
url_video <- paste0("https://www.googleapis.com/youtube/v3/videos?",
"id=", video_id,
"&key=",key,
"&part=snippet,contentDetails,statistics,status")
y <- fromJSON(url_video, simplify = FALSE)
video_duration_raw <- y$items[[1]]$contentDetails$duration
viewCount <- isNull(y$items[[1]]$statistics$viewCount)
likeCount <- isNull(y$items[[1]]$statistics$likeCount)
dislikeCount <- isNull(y$items[[1]]$statistics$dislikeCount)
favoriteCount <- isNull(y$items[[1]]$statistics$favoriteCount)
commentCount <- isNull(y$items[[1]]$statistics$commentCount)
# video_duration <- duration(video_duration_raw)
input <- data.frame(title = video_title,
time = video_time,
duration = video_duration_raw,
viewCount = viewCount,
likeCount = likeCount,
dislikeCount = dislikeCount,
favoriteCount = favoriteCount,
commentCount = commentCount,
stringsAsFactors = FALSE)
dados %<>% bind_rows(input)
print(paste0(round(i/videos_n,2)*100,"%"))
}
dados %<>% as_tibble()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment