R/utils.R

Defines functions qf_find_list_id

Documented in qf_find_list_id

#' Fin the ID of Twitter lists. 
#'
#' @param n An integer, number of new tweets to request per users, defaults to 200.
#' @param cache_lists Logical, defaults to TRUE. If TRUE, stores locally the lists owned by a given user, in a subfolder `lists_by_user`. If list has already been downloaded, it just loads it. To overwrite, set `overwrite` to TRUE.
#' @param overwrite_lists Logical, defaults to FALSE. If TRUE, it overwrites previously downloaded lists.
#' @param twitter_token A twitter token generated by rtweet.
#' @examples
#' 
#' @export
#' 

qf_find_list_id <- function(slug,
                            owner_user,
                            cache_lists = TRUE,
                            overwrite_lists = FALSE,
                            twitter_token = NULL) {
  if (cache_lists == TRUE) {
    fs::dir_create(path = "lists_by_user")
    cached_list_location <- fs::path("lists_by_user", paste0(owner_user, ".rds"))
    if (fs::file_exists(cached_list_location)==TRUE) {
      list_users <- readRDS(file = cached_list_location)
    } else {
      list_users <- rtweet::lists_users(user = owner_user,
                                        reverse = TRUE,
                                        token = NULL,
                                        parse = TRUE)
      saveRDS(object = list_users, file = cached_list_location)
    }
  }
  list_id <- list_users %>% 
    dplyr::rename(slug_l = slug) %>% 
    dplyr::filter(slug_l == slug) %>% 
    dplyr::pull(list_id)
  return(list_id)
}
giocomai/edjnetquotefinder documentation built on Feb. 11, 2022, 12:51 p.m.