Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 37 additions & 63 deletions R/list-files-gh-gists.R
Original file line number Diff line number Diff line change
@@ -1,82 +1,65 @@
get_gist_pages <- function(user) {

get_gist_pages <- function(user){
req_user <- httr::GET(glue::glue("https://api.github.com/users/{user}"))

req_user <- httr::GET(
glue::glue("https://api.github.com/users/{user}")
)
# Check HTTP response first
if (httr::status_code(req_user) != 200) {
warning("Failed to fetch GitHub user info for '\", user,\"' (status ", httr::status_code(req_user), "). Returning 0 pages.")
return(0L)
}

num_gists <- httr::content(req_user)$public_gists
body <- httr::content(req_user)
num_gists <- body$public_gists

# Number of pages to do (assuming 100 per page)
num_pages <- num_gists %/% 100
remainder <- num_gists %% 100
if(remainder > 0) num_pages <- num_pages + 1
# Defensive handling: ensure we have a single non-negative integer
if (is.null(num_gists) || length(num_gists) == 0) return(0L)
num_gists <- as.integer(num_gists)
if (is.na(num_gists) || num_gists <= 0L) return(0L)

# Compute pages using ceiling to avoid remainder logic problems
num_pages <- ceiling(num_gists / 100)
num_pages
}

get_gist_content <- function(user){
get_gist_content <- function(user) {

num_pages <- get_gist_pages(user)

# If there are no pages, return an empty list
if (num_pages == 0L) return(list())

# gists themselves
req_content <- vector("list", num_pages)

for(i in seq_len(num_pages)){
for (i in seq_len(num_pages)) {

req <- httr::GET(
glue::glue("https://api.github.com/users/{user}/gists"),
query = list(per_page = 100, page = i)
)

req <- httr::GET(glue::glue("https://api.github.com/users/{user}/gists"),
query = list(per_page = 100,
page = i))
if (httr::status_code(req) != 200) {
warning("Failed to fetch gists for page ", i, " (status ", httr::status_code(req), "). Skipping page.")
req_content[[i]] <- list()
next
}

req_content[[i]] <- httr::content(req)
}

purrr::flatten(req_content)

}

#' List Github Gists of User
#'
#' Given a username, return a dataframe with paths to all the gists by that user.
#'
#' @param user Character string of username whose github gists you want to pull.
#' @param pattern Regex pattern to keep only matching files. Default is
#' `stringr::regex("(r|rmd|rmarkdown|qmd)$", ignore_case = TRUE)` which will
#' keep only R, Rmarkdown and Quarto documents. If you have a lot of .md gists
#' that can be converted to .R files you may want to edit this argument. To
#' keep all files use `"."`.
#'
#' @return Dataframe with `relative_paths` and `absolute_paths` of file paths.
#' Because gists do not exist in a folder structure `relative_paths` will
#' generally just be a file name. `absolute_paths` a url to the raw file. See
#' `unnest_results()` for helper to put into an easier to read format.
#'
#' @seealso [list_files_github_repo()], [list_files_wd()]
#' @export
#'
#' @examples
#' \donttest{
#' library(dplyr)
#' library(funspotr)
#'
#' # pulling and analyzing my R file github gists
#' gists_urls <- list_files_github_gists("brshallo", pattern = ".")
#'
#' # Will just parse the first 2 files/gists
#' # Note that is easy to hit the API limit if have lots of gists
#' contents <- filter(gists_urls, str_detect_r_docs(absolute_paths)) %>%
#' slice(1:2) %>%
#' spot_funs_files()
#'
#'
#' contents %>%
#' unnest_results()
#' }
list_files_github_gists <- function(user,
pattern = stringr::regex("(r|rmd|rmarkdown|qmd)$", ignore_case = TRUE)){
pattern = stringr::regex("(r|rmd|rmarkdown|qmd)$", ignore_case = TRUE)) {

content <- get_gist_content(user)

# If nothing returned, return an empty tibble with the same columns
if (length(content) == 0) {
return(tibble::tibble(relative_paths = character(), absolute_paths = character()))
}

raw_urls <- content %>%
map("files") %>%
map(1) %>%
Expand All @@ -85,13 +68,4 @@ list_files_github_gists <- function(user,
output <- tibble(relative_paths = fs::path_file(raw_urls), absolute_paths = raw_urls)

filter(output, str_detect_r_docs(.data$relative_paths, pattern = pattern, rmv_index = FALSE))
}


#' #' `r lifecycle::badge("deprecated")`
#' #'
#' #' This function was deprecated with updates to the API that improved
#' #' consistency of naming conventions.
#' github_gists <- function(user, drop_non_r = TRUE){
#' list_files_github_gists(user, drop_non_r)
#' }
}