R/downloaders.R

Defines functions download_sam_duns completed_duns

Documented in completed_duns download_sam_duns

#' Completed DUNs Numbers
#'
#' @param folder_path folder path location
#'
#' @return
#' @export
#'
#' @examples
completed_duns <-
  function(folder_path = "Desktop/r_packages/govtrackRdata/data/affwerx/sam_duns") {
    oldwd <- getwd()
    setwd("~")
    .build_folder(folder_path)
    setwd(folder_path)

    duns  <- list.files() %>% str_remove_all("duns_|\\.rda") %>% readr::parse_number()

    if (getwd() != oldwd) {
      setwd(oldwd)
    }

    tibble(idDUNS = duns)
  }

#' Download SAM DUNs numbers
#'
#' @param duns vector of DUNS
#' @param sleep_time sleep time
#' @param return_message if \code{TRUE} returns a messaqge
#' @param folder_path folder path location
#' @param api_key data.gov API key
#'
#' @return
#' @export
#'
#' @examples
download_sam_duns <-
  function(duns = NULL , sleep_time = 3, return_message = T,
           folder_path = "Desktop/r_packages/govtrackRdata/data/affwerx/sam_duns", api_key = "DEMO_KEY") {
    if (length(duns) == 0) {
      "Provide DUNS" %>% message()
      return(invisible())
    }
    oldwd <- getwd()
    setwd("~")
    .build_folder(folder_path)
    setwd(folder_path)

    duns %>%
      walk(function(x){
        df <-
          sam_duns(
            duns = x,
            sleep_time = sleep_time,
            api_key = api_key,
            return_message = return_message
          )

        if (length(df) == 0) {
          glue::glue("No SAM data for DUNS: {x}") %>% message()
          return(invisible())
        }
        file <- glue::glue("duns_{x}.rda")
        df %>% save(file = file)
      })

    if (getwd() != oldwd) {
      setwd(oldwd)
    }
  }
abresler/govtrackR documentation built on July 11, 2020, 12:30 a.m.