6060# ' Takes one of three values: \code{'any'} (return all videos; Default),
6161# ' \code{'creativeCommon'} (return videos with Creative Commons
6262# ' license), \code{'youtube'} (return videos with standard YouTube license).
63- # ' @param relevance_language Character. Default is "en".
6463# ' @param simplify Boolean. Return a data.frame if \code{TRUE}.
6564# ' Default is \code{TRUE}.
6665# ' If \code{TRUE}, it returns a list that carries additional information.
@@ -132,28 +131,28 @@ yt_search <- function(term = NULL, max_results = 50, channel_id = NULL,
132131 if (! (video_license %in% c(" any" , " creativeCommon" , " youtube" ))) {
133132 stop(" video_license can only take values: any, creativeCommon, or youtube." )
134133 }
135-
134+
136135 if (! (video_syndicated %in% c(" any" , " true" ))) {
137136 stop(" video_syndicated can only take values: any or true." )
138137 }
139-
138+
140139 if (! (video_type %in% c(" any" , " episode" , " movie" ))) {
141140 stop(" video_type can only take values: any, episode, or movie." )
142141 }
143142 } else {
144143 # Set these to NULL if type is not "video" to avoid sending them in the API call
145- video_caption <- video_license <- video_definition <-
144+ video_caption <- video_license <- video_definition <-
146145 video_type <- video_syndicated <- NULL
147146 }
148147
149148 # Validate date formats
150149 validate_rfc339_date <- function (date_str ) {
151- if (is.character(date_str ) &&
150+ if (is.character(date_str ) &&
152151 is.na(as.POSIXct(date_str , format = " %Y-%m-%dT%H:%M:%SZ" ))) {
153152 stop(" The date is not properly formatted in RFC 339 Format (YYYY-MM-DDTHH:MM:SSZ)." )
154153 }
155154 }
156-
155+
157156 validate_rfc339_date(published_after )
158157 validate_rfc339_date(published_before )
159158
@@ -193,7 +192,7 @@ yt_search <- function(term = NULL, max_results = 50, channel_id = NULL,
193192 if (length(res_items ) == 0 ) {
194193 return (data.frame ())
195194 }
196-
195+
197196 if (item_type == " video" ) {
198197 simple_res <- lapply(res_items , function (x ) {
199198 if (is.null(x $ id $ videoId )) {
@@ -204,58 +203,58 @@ yt_search <- function(term = NULL, max_results = 50, channel_id = NULL,
204203 } else {
205204 simple_res <- lapply(res_items , function (x ) unlist(x $ snippet ))
206205 }
207-
206+
208207 # Remove NULL entries and convert to data frame
209208 simple_res <- simple_res [! sapply(simple_res , is.null )]
210209 if (length(simple_res ) == 0 ) {
211210 return (data.frame ())
212211 }
213-
212+
214213 return (ldply(simple_res , rbind ))
215214 }
216215
217216 # Make initial API call
218217 res <- tuber_GET(" search" , querylist , ... )
219-
218+
220219 # If get_all is FALSE or there are no results, process and return
221220 if (! identical(get_all , TRUE ) || res $ pageInfo $ totalResults == 0 ) {
222221 if (! identical(simplify , TRUE )) {
223222 return (res )
224223 }
225224 return (process_results(res $ items , type ))
226225 }
227-
226+
228227 # Process all pages for get_all=TRUE
229228 all_results <- process_results(res $ items , type )
230229 page_token <- res $ nextPageToken
231230 page_count <- 1
232-
231+
233232 # Get all pages up to max_pages limit
234233 while (! is.null(page_token ) && page_count < max_pages ) {
235234 querylist $ pageToken <- page_token
236235 a_res <- tuber_GET(" search" , querylist , ... )
237-
236+
238237 next_results <- process_results(a_res $ items , type )
239238 all_results <- rbind(all_results , next_results )
240239 page_token <- a_res $ nextPageToken
241240 page_count <- page_count + 1
242-
241+
243242 # Check if we've reached YouTube's limit (around 500-600 items)
244243 if (nrow(all_results ) > = 500 && is.null(page_token )) {
245244 warning(" Reached YouTube API search result limit (approximately 500 items)" )
246245 break
247246 }
248247 }
249-
248+
250249 # Add warning if we hit the max_pages limit but there are still more results
251250 if (! is.null(page_token ) && page_count > = max_pages ) {
252251 warning(sprintf(" Only retrieved %d pages of results. Set max_pages higher to get more results." , max_pages ))
253252 }
254-
253+
255254 # Add additional information as attributes
256255 attr(all_results , " total_results" ) <- res $ pageInfo $ totalResults
257256 attr(all_results , " actual_results" ) <- nrow(all_results )
258257 attr(all_results , " api_limit_reached" ) <- nrow(all_results ) > = 500
259-
258+
260259 return (all_results )
261260}
0 commit comments