beaumontmeteo/scripts/update_rain_db.R

66 lines
1.6 KiB
R

command_line_args <- commandArgs(trailingOnly = TRUE)
parse_cli_args <- function(args) {
parsed <- list()
for (arg in args) {
if (!grepl("^--", arg)) {
next
}
parts <- strsplit(sub("^--", "", arg), "=", fixed = TRUE)[[1]]
key <- parts[1]
value <- if (length(parts) > 1) paste(parts[-1], collapse = "=") else TRUE
parsed[[key]] <- value
}
parsed
}
script_file_arg <- grep("^--file=", commandArgs(FALSE), value = TRUE)
script_path <- normalizePath(sub("^--file=", "", script_file_arg[1]), winslash = "/")
project_root <- normalizePath(file.path(dirname(script_path), ".."), winslash = "/")
setwd(project_root)
invisible(lapply(
list.files(path = "R", pattern = "\\.R$", full.names = TRUE),
source
))
args <- parse_cli_args(command_line_args)
db_path <- if (!is.null(args[["db-path"]])) {
normalizePath(args[["db-path"]], winslash = "/", mustWork = FALSE)
} else {
default_rain_db_path()
}
location_ids <- if (!is.null(args[["location"]])) {
strsplit(args[["location"]], ",", fixed = TRUE)[[1]]
} else {
get_rain_locations()$location_id
}
initial_backfill_days <- if (!is.null(args[["days"]])) {
as.integer(args[["days"]])
} else {
21L
}
headers <- load_api_headers()
if (is.null(headers)) {
stop("No API credentials found. Add token4 to data/secrets before running the sync.")
}
results <- sync_all_weather_locations(
location_ids = location_ids,
db_path = db_path,
headers = headers,
initial_backfill_days = initial_backfill_days
)
print(results, row.names = FALSE)
if (any(results$status == "error")) {
stop("At least one location failed to sync.")
}