/
AddVideoData.R
243 lines (216 loc) · 7.85 KB
/
AddVideoData.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#' @title Add columns of video information to network dataframes
#'
#' @description Network is supplemented with additional downloaded video information.
#'
#' @note Only supports YouTube actor networks. Refer to \code{\link{AddVideoData.actor.youtube}}.
#'
#' @param net A named list of dataframes \code{nodes} and \code{edges} generated by \code{Create}.
#' @param youtubeAuth YouTube Authenticate object.
#' @param ... Additional parameters passed to function.
#'
#' @return Network as a named list of three dataframes containing \code{$nodes}, \code{$edges} and \code{$videos} nodes
#' and edges include columns for additional video data.
#'
#' @aliases AddVideoData
#' @name AddVideoData
#' @export
AddVideoData <- function(net, youtubeAuth = NULL, ...) {
msg <- f_verbose(check_dots("verbose", ...))
msg("Adding video data to network...")
if ("voson.video" %in% class(net)) {
stop("Network already has video data attribute.")
}
if (is.null(youtubeAuth)) {
stop("AddVideoData requires Authenticate object.", call. = FALSE)
}
# searches the class list of net for matching method
UseMethod("AddVideoData", net)
}
#' @noRd
#' @export
AddVideoData.default <- function(net, youtubeAuth = NULL, ...) {
stop("Unknown network type passed to AddVideoData.", call. = FALSE)
}
#' @noRd
#' @method AddVideoData actor
#' @export
AddVideoData.actor <- function(net, youtubeAuth = NULL, ...) {
UseMethod("AddVideoData.actor", net)
}
#' @noRd
#' @export
AddVideoData.actor.default <-
function(net, youtubeAuth = NULL, ...) {
stop("Unknown social media type passed to AddVideoData.", call. = FALSE)
}
#' @title Add video information to youtube actor network dataframes
#'
#' @description YouTube actor network is supplemented with additional downloaded video information. Adds video id,
#' title, description and publish time as edge attributes. Nodes or actor references to video id's in the network are
#' substituted with the actor id (video channel id) retrieved from the video details.
#'
#' @param net A named list of dataframes \code{nodes} and \code{edges} generated by \code{Create}.
#' @param youtubeAuth YouTube Authenticate object.
#' @param videoIds List. Video id's for which to download video information.
#' @param actorSubOnly Logical. Only substitute video id's for their publishers channel id. Don't add additional video
#' data to edge list.
#' @param ... Additional parameters passed to function.
#'
#' @return Network as a named list of three dataframes containing \code{$nodes}, \code{$edges} and \code{$videos} nodes
#' and edges include columns for additional video data.
#'
#' @examples
#' \dontrun{
#' # replace video id references with actors and add video id, title, description and plublish time
#' # to an actor network
#' actorNetwork <- collectData |> Create("actor") |> AddVideoData(youtubeAuth)
#'
#' # only replace video id references with actors that published videos in network
#' actorNetwork <- collectData |> Create("actor") |> AddVideoData(youtubeAuth, actorSubOnly = TRUE)
#'
#' # network
#' # actorNetwork$nodes
#' # actorNetwork$edges
#'
#' # dataframe of downloaded video data
#' # actorNetwork$videos
#' }
#'
#' @aliases AddVideoData.actor.youtube
#' @name AddVideoData.actor.youtube
#' @export
AddVideoData.actor.youtube <-
function(net,
youtubeAuth = NULL,
videoIds = NULL,
actorSubOnly = FALSE,
...) {
if (is.null(videoIds)) {
# extract video ids from network nodes
rm_videoid_str <- function(col) {
sub("VIDEOID:", "", col)
}
videoIds <-
net$nodes |> dplyr::filter(.data$node_type == "video") |> dplyr::select(.data$id) |>
dplyr::mutate_at(.vars = "id", .funs = rm_videoid_str)
videoIds <- unlist(unname(as.list(videoIds)))
}
videoIds <- unique(videoIds)
df_video <- GetVideoData(youtubeAuth, videoIds)
net$videos <- df_video
if (nrow(df_video) == 0) {
msg("No video data could be retrieved.\n")
return(net)
}
# downloaded video data
df_video <- df_video |> dplyr::mutate(id = paste0("VIDEOID:", .data$VideoID))
ch_details <-
df_video |> dplyr::select(.data$id, .data$ChannelTitle, .data$ChannelID) |>
dplyr::rename(
video_author_title = .data$ChannelTitle,
video_author_id = .data$ChannelID
)
# replace video id values in nodes with the video publishers id (channel id) and screen name
net$nodes <- net$nodes |> dplyr::left_join(ch_details, by = c("id")) |>
dplyr::mutate(
id = dplyr::if_else(
!is.na(.data$video_author_id),
.data$video_author_id,
.data$id
),
screen_name = dplyr::if_else(
!is.na(.data$video_author_id),
.data$video_author_title,
.data$screen_name
),
node_type = dplyr::if_else(!is.na(.data$video_author_id), "actor", .data$node_type),
video_author_id = NULL,
video_author_title = NULL
) |>
dplyr::distinct(.data$id, .keep_all = TRUE)
ch_details <- ch_details |> dplyr::select(-.data$video_author_title)
# replace from and to video id values in edges with the video publishers id (channel id)
net$edges <- net$edges |> dplyr::left_join(ch_details, by = c("from" = "id")) |>
dplyr::mutate(
from = dplyr::if_else(
!is.na(.data$video_author_id),
.data$video_author_id,
.data$from
),
video_author_id = NULL
) |>
dplyr::left_join(ch_details, by = c("to" = "id")) |>
dplyr::mutate(
to = dplyr::if_else(
!is.na(.data$video_author_id),
.data$video_author_id,
.data$to
),
video_author_id = NULL
)
# change node type from actor to publisher if they published a video in network
net$nodes <- net$nodes |> dplyr::left_join(
dplyr::filter(net$edges, .data$edge_type == "self-loop") |>
dplyr::select(.data$from, .data$edge_type) |> dplyr::distinct(.keep_all = TRUE),
by = c("id" = "from")
) |>
dplyr::mutate(
node_type = dplyr::if_else(!is.na(.data$edge_type), "publisher", .data$node_type),
edge_type = NULL
)
# add video title, description and publish time
if (actorSubOnly == FALSE) {
net$edges <- net$edges |> dplyr::left_join(dplyr::select(df_video, dplyr::starts_with("Video")),
by = c("video_id" = "VideoID")) |>
dplyr::rename(
video_title = .data$VideoTitle,
video_description = .data$VideoDescription,
video_published_at = .data$VideoPublishedAt
)
}
class(net) <- union(class(net), c("voson.video"))
msg("Done.\n")
net
}
# make a single api request for video data for supplied video id's
# when id's are supplied the api request for videos does not support paging
GetVideoData <- function(youtubeAuth, videoIds) {
video_data <- list()
base_url_videos <- "https://www.googleapis.com/youtube/v3/videos"
api_opts_videos <- list(
part = "snippet",
id = paste0(videoIds, collapse = ","),
key = youtubeAuth$auth
)
req <- httr::GET(base_url_videos, query = api_opts_videos)
res <- httr::content(req)
if (req$status_code != 200) {
msg(
paste0(
"\nStatus: ",
req$status_code,
"\nError: ",
res$error$code,
"\nDetail: ",
res$error$message,
"\n"
)
)
return(tibble::tibble())
} else {
video_data <- c(video_data, res$items)
}
df_video <- lapply(video_data, function(x) {
tibble::tibble(
VideoID = x$id,
VideoTitle = x$snippet$title,
VideoDescription = x$snippet$description,
VideoPublishedAt = x$snippet$publishedAt,
ChannelID = x$snippet$channelId,
ChannelTitle = x$snippet$channelTitle
)
})
# df_video <- tibble::as_tibble(do.call("rbind", df_video))
df_video <- dplyr::bind_rows(df_video)
df_video
}