Skip to content

Commit 2577a31

Browse files
authored
Update list_channel_resources.R
1 parent 38ae02e commit 2577a31

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

R/list_channel_resources.R

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
#' potential names of the entry in the vector:
66
#' \code{category_id}: YouTube guide category that returns channels associated
77
#' with that category
8-
#' \code{username}: YouTube username that returns channel associated with that
8+
#' \code{username}: YouTube username that returns the channel associated with that
99
#' username.
1010
#' \code{channel_id}: a comma-separated list of the YouTube channel ID(s) for
1111
#' the resource(s) that are being retrieved
1212
#'
13-
#' @param part a comma separated list of channel resource properties that
14-
#' response will include string. Required.
13+
#' @param part a comma-separated list of channel resource properties that
14+
#' response will include a string. Required.
1515
#' One of the following: \code{auditDetails, brandingSettings, contentDetails,
1616
#' contentOwnerDetails, id, invideoPromotion, localizations, snippet,
1717
#' statistics, status, topicDetails}.
1818
#' Default is \code{contentDetails}.
19-
#' @param hl Language used for text values. Optional. Default is \code{en-US}.
19+
#' @param hl Language used for text values. Optional. The default is \code{en-US}.
2020
#' For other allowed language codes, see \code{\link{list_langs}}.
2121
#' @param max_results Maximum number of items that should be returned. Integer.
22-
#' Optional. Can be between 0 and 50. Default is 50.
22+
#' Optional. Can be between 0 and 50. The default is 50.
2323
#' @param page_token specific page in the result set that should be returned,
2424
#' optional
2525
#' @param \dots Additional arguments passed to \code{\link{tuber_GET}}.
@@ -42,32 +42,24 @@
4242

4343
list_channel_resources <- function(filter = NULL, part = "contentDetails",
4444
max_results = 50, page_token = NULL, hl = "en-US", ...) {
45-
4645
if (max_results < 0 | max_results > 50) {
4746
stop("max_results only takes a value between 0 and 50.")
4847
}
49-
5048
if (!(names(filter) %in% c("category_id", "username", "channel_id"))) {
5149
stop("filter can only take one of three values: category_id,
5250
username or channel_id.")
5351
}
54-
5552
if (length(filter) != 1) stop("filter must be a vector of length 1.")
56-
57-
translate_filter <- c(channel_id = "id", category_id = "categoryId",
58-
username = "forUsername")
59-
yt_filter_name <- as.vector(translate_filter[match(names(filter),
60-
names(translate_filter))])
61-
names(filter) <- yt_filter_name
62-
63-
if (is.character(filter$username)) {
64-
usernames <- filter$username
53+
54+
# Check for username BEFORE translation
55+
if ("username" %in% names(filter)) {
56+
usernames <- filter[names(filter) == "username"] # Use bracket indexing
6557
num_usernames <- length(usernames)
6658
channel_ids <- vector("list", length = num_usernames)
6759

6860
for (i in seq_along(usernames)) {
6961
querylist <- list(part = part, maxResults = max_results,
70-
pageToken = page_token, hl = hl, forUsername = usernames[i])
62+
pageToken = page_token, hl = hl, forUsername = usernames[i])
7163

7264
res <- tuber_GET("channels", querylist, ...)
7365

@@ -83,6 +75,13 @@ list_channel_resources <- function(filter = NULL, part = "contentDetails",
8375
return(channel_ids)
8476
}
8577

78+
# Translate filter names for non-username cases
79+
translate_filter <- c(channel_id = "id", category_id = "categoryId",
80+
username = "forUsername")
81+
yt_filter_name <- as.vector(translate_filter[match(names(filter),
82+
names(translate_filter))])
83+
names(filter) <- yt_filter_name
84+
8685
querylist <- list(part = part, maxResults = max_results,
8786
pageToken = page_token, hl = hl)
8887
querylist <- c(querylist, filter)

0 commit comments

Comments
 (0)