-
Notifications
You must be signed in to change notification settings - Fork 55
/
get_comment_threads.R
114 lines (98 loc) · 4.41 KB
/
get_comment_threads.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#' Get Comments Threads
#'
#' @param filter string; Required.
#' named vector of length 1
#' potential names of the entry in the vector:
#' \code{video_id}: video ID.
#' \code{channel_id}: channel ID.
#' \code{thread_id}: comma-separated list of comment thread IDs
#' \code{threads_related_to_channel}: channel ID.
#'
#' @param part Comment resource requested. Required. Comma separated list
#' of one or more of the
#' following: \code{id, snippet}. e.g., \code{"id, snippet"},
#' \code{"id"}, etc. Default: \code{snippet}.
#' @param max_results Maximum number of items that should be returned.
#' Integer. Optional. Default is 100.
#' If the value is greater than 100 then the function fetches all the
#' results. The outcome is a simplified \code{data.frame}.
#' @param page_token Specific page in the result set that should be
#' returned. Optional.
#' @param text_format Data Type: Character. Default is \code{"html"}.
#' Only takes \code{"html"} or \code{"plainText"}. Optional.
#' @param simplify Data Type: Boolean. Default is \code{TRUE}. If \code{TRUE},
#' the function returns a data frame. Else a list with all the
#' information returned.
#' @param \dots Additional arguments passed to \code{\link{tuber_GET}}.
#'
#' @return
#' Nested named list. The entry \code{items} is a list of comments
#' along with meta information.
#' Within each of the \code{items} is an item \code{snippet} which
#' has an item \code{topLevelComment$snippet$textDisplay}
#' that contains the actual comment.
#'
#' If simplify is \code{TRUE}, a \code{data.frame} with the following columns:
#' \code{authorDisplayName, authorProfileImageUrl, authorChannelUrl,
#' authorChannelId.value, videoId, textDisplay,
#' canRate, viewerRating, likeCount, publishedAt, updatedAt}
#'
#' @export
#' @references \url{https://developers.google.com/youtube/v3/docs/commentThreads/list}
#'
#' @examples
#' \dontrun{
#'
#' # Set API token via yt_oauth() first
#'
#' get_comment_threads(filter = c(video_id = "N708P-A45D0"))
#' get_comment_threads(filter = c(video_id = "N708P-A45D0"), max_results = 101)
#' }
get_comment_threads <- function(filter = NULL, part = "snippet",
text_format = "html", simplify = TRUE,
max_results = 100, page_token = NULL, ...) {
if (max_results < 20) {
stop("max_results must be a value greater than or equal to 20. For values above 100, it outputs all the results.")
}
valid_formats <- c("html", "plainText")
if (!text_format %in% valid_formats) {
stop("Provide a valid value for textFormat.")
}
valid_filters <- c("video_id", "channel_id", "thread_id", "threads_related_to_channel")
if (!(names(filter) %in% valid_filters)) {
stop("filter can only take one of the following values: channel_id, video_id, thread_id, threads_related_to_channel.")
}
if (length(filter) != 1) {
stop("filter must be a vector of length 1.")
}
orig_filter <- filter
translate_filter <- c(video_id = "videoId", thread_id = "id",
threads_related_to_channel = "allThreadsRelatedToChannelId",
channel_id = "channelId", page_token = "pageToken")
names(filter) <- translate_filter[names(filter)]
querylist <- list(part = part, maxResults = ifelse(max_results > 100, 100, max_results),
textFormat = text_format, pageToken = page_token)
querylist <- c(querylist, filter)
res <- tuber_GET("commentThreads", querylist, ...)
if (simplify && part == "snippet" && max_results < 101) {
simpler_res <- lapply(res$items, function(x) unlist(x$snippet$topLevelComment$snippet))
simpler_res <- do.call(rbind, simpler_res)
return(simpler_res)
} else if (simplify && part == "snippet" && max_results > 100) {
agg_res <- lapply(res$items, function(x) unlist(x$snippet$topLevelComment$snippet))
page_token <- res$nextPageToken
while (is.character(page_token)) {
a_res <- get_comment_threads(orig_filter,
part = part,
text_format = text_format,
simplify = FALSE,
max_results = 100,
page_token = page_token,
...)
agg_res <- rbind(lapply(a_res$items, function(x) unlist(x$snippet$topLevelComment$snippet)), agg_res)
page_token <- a_res$nextPageToken
}
return(do.call(rbind, agg_res))
}
res
}