url_nc_cf_search_doc <- function() { "https://cf.ncsbe.gov/CFDocLkup/DocumentResult/" } url_nc_cf_export_search_doc <- function() { "https://cf.ncsbe.gov/CFDocLkup/ExportSearchResults/" } url_nc_cf_export_detail_results <- function() { "https://cf.ncsbe.gov/CFOrgLkup/ExportDetailResults/" } match_report_type <- function(report, collapse = TRUE) { report <- toupper(report) reports <- c("RPMYSA", "RPYESA", "RPQTR1", "RPQTR2", "RPQTR3", "RPQTR4") valid <- c( reports, sub("^RP", "", reports), gsub("^RP|SA$", "", reports), sub("RPQTR", "Q", reports) ) names(valid) <- rep(reports, 4) x <- names(valid)[match(report, valid)] if (any(is.na(x))) { report <- report[is.na(x)] cli::cli_abort(c("Invalid report {.val {report}}", i = "Valid: {.val {reports}}")) } if (!collapse) return(x) paste(paste0("'", x, "'"), collapse = ", ") } req_report_by_year <- function( year, report = c("RPMYSA", "RPYESA", "RPQTR1", "RPQTR2", "RPQTR3", "RPQTR4") ) { reports <- match_report_type(report) req <- request(url_nc_cf_search_doc()) req <- req_url_query(req, year = year, reports = reports) req } req_report_by_year_export <- function( year, report = c("RPMYSA", "RPYESA", "RPQTR1", "RPQTR2", "RPQTR3", "RPQTR4") ) { reports <- match_report_type(report) req <- request(url_nc_cf_export_search_doc()) req <- req_url_query(req, year = year, reports = reports) req } match_report_sections <- function(section) { options <- c(ALL = "all", CVR = "cover", EXP = "expenditures", REC = "receipts", EXP = "expenses") section <- arg_match(section, options) names(options)[which(section == options)] } req_report_detail <- function(report_id, section = "receipts") { section <- match_report_sections(section) req <- request(url_nc_cf_export_detail_results()) # Title is needed or the page throws an error # https://cf.ncsbe.gov/CFOrgLkup/ExportDetailResults/?ReportID=197247&Type=REC&Title=JOHN req <- req_url_query(req, ReportID = report_id, Type = section, Title = "download") req }