/R/get_video_details.R

https://github.com/soodoku/tuber · R · 131 lines · 64 code · 18 blank · 49 comment · 11 complexity · 96439fc2241911889085cce888c991b1 MD5 · raw file

  1. # helpers for data frame conversion in `get_video_details()`
  2. conditional_unnest_wider <- function(data_input, var) {
  3. if (var %in% names(data_input)) {
  4. tidyr::unnest_wider(data_input, var, names_sep = "_")
  5. } else {
  6. data_input
  7. }
  8. }
  9. json_to_df <- function(res) {
  10. intermediate <- res %>%
  11. tibble::enframe() %>%
  12. tidyr::pivot_wider() %>%
  13. tidyr::unnest(cols = c(kind, etag)) %>%
  14. # reflect level of nesting in column name
  15. dplyr::rename(response_kind = kind, response_etag = etag) %>%
  16. tidyr::unnest(items) %>%
  17. tidyr::unnest_wider(col = items) %>%
  18. # reflect level of nesting in column name for those that may not be unique
  19. dplyr::rename(items_kind = kind, items_etag = etag) %>%
  20. tidyr::unnest_wider(snippet)
  21. intermediate_2 <- intermediate %>%
  22. # fields that may not be available:
  23. # live streaming details
  24. conditional_unnest_wider(var = "liveStreamingDetails") %>%
  25. # region restriction (rental videos)
  26. conditional_unnest_wider(var = "regionRestriction") %>%
  27. conditional_unnest_wider(var = "regionRestriction_allowed") %>%
  28. # statistics
  29. conditional_unnest_wider(var = "statistics") %>%
  30. # status
  31. conditional_unnest_wider(var = "status") %>%
  32. # player
  33. conditional_unnest_wider(var = "player") %>%
  34. # contentDetails
  35. conditional_unnest_wider(var = "contentDetails") %>%
  36. conditional_unnest_wider(var = "topicDetails") %>%
  37. conditional_unnest_wider(var = "localized") %>%
  38. conditional_unnest_wider(var = "pageInfo") %>%
  39. # thumbnails
  40. conditional_unnest_wider(var = "thumbnails") %>%
  41. conditional_unnest_wider(var = "thumbnails_default") %>%
  42. conditional_unnest_wider(var = "thumbnails_standard") %>%
  43. conditional_unnest_wider(var = "thumbnails_medium") %>%
  44. conditional_unnest_wider(var = "thumbnails_high") %>%
  45. conditional_unnest_wider(var = "thumbnails_maxres")
  46. intermediate_2
  47. }
  48. #' Get Details of a Video or Videos
  49. #'
  50. #' Get details such as when the video was published, the title, description,
  51. #' thumbnails, category etc.
  52. #'
  53. #' @param video_id Comma separated list of IDs of the videos for which
  54. #' details are requested. Required.
  55. #' @param part Comma-separated vector of video resource properties requested.
  56. #' Options include:
  57. #' \code{contentDetails, fileDetails, id, liveStreamingDetails,
  58. #' localizations, player, processingDetails,
  59. #' recordingDetails, snippet, statistics, status, suggestions, topicDetails}
  60. #' @param \dots Additional arguments passed to \code{\link{tuber_GET}}.
  61. #' @param as.data.frame Logical, returns the requested information as data.frame.
  62. #' Does not work for:
  63. #' \code{fileDetails, suggestions, processingDetails}
  64. #'
  65. #' @return list. If part is snippet, the list will have the following elements:
  66. #' \code{id} (video id that was passed), \code{publishedAt, channelId,
  67. #' title, description, thumbnails,
  68. #' channelTitle, categoryId, liveBroadcastContent, localized,
  69. #' defaultAudioLanguage}
  70. #'
  71. #' @export
  72. #' @references \url{https://developers.google.com/youtube/v3/docs/videos/list}
  73. #' @examples
  74. #' \dontrun{
  75. #'
  76. #' # Set API token via yt_oauth() first
  77. #'
  78. #' get_video_details(video_id = "yJXTXN4xrI8")
  79. #' get_video_details(video_id = "yJXTXN4xrI8", part = "contentDetails")
  80. #' # retrieve multiple parameters
  81. #' get_video_details(video_id = "yJXTXN4xrI8", part = c("contentDetails", "status"))
  82. #' # get details for multiple videos as data frame
  83. #' get_video_details(video_id = c("LDZX4ooRsWs", "yJXTXN4xrI8"), as.data.frame = TRUE)
  84. #' }
  85. #'
  86. get_video_details <- function(video_id = NULL, part = "snippet", as.data.frame = FALSE, ...) {
  87. if (!is.character(video_id)) stop("Must specify a video ID.")
  88. if (!is.character(part)) stop("Parameter part must be a character vector")
  89. if (length(part) > 1) {
  90. part <- paste0(part, collapse = ",")
  91. }
  92. if (length(video_id) > 1) {
  93. video_id <- paste0(video_id, collapse = ",")
  94. }
  95. parts_only_for_video_owners <- c("fileDetails", "suggestions", "processingDetails")
  96. if (as.data.frame & part %in% parts_only_for_video_owners) {
  97. stop(
  98. paste("If as.data.frame = TRUE, then `part` may not include any of the following parts: "),
  99. paste(parts_only_for_video_owners, collapse = " ,")
  100. )
  101. }
  102. querylist <- list(part = part, id = video_id)
  103. raw_res <- tuber_GET("videos", querylist, ...)
  104. if (length(raw_res$items) == 0) {
  105. warning("No details for this video are available. Likely cause:
  106. Incorrect ID. \n")
  107. return(list())
  108. }
  109. if (as.data.frame) {
  110. raw_res <- json_to_df(raw_res)
  111. }
  112. raw_res
  113. }