#!/usr/bin/env Rscript


## ---- Load packages ----

message("Load packages")
suppressMessages(library(jsonlite))
suppressMessages(library(curl))


## ---- Read API credentials ----

message("Read Kaggle API credentials")
kaggle_conf_dir <- Sys.getenv("KAGGLE_CONFIG_DIR")
if (!nzchar(kaggle_conf_dir)) {
    kaggle_conf_dir <- path.expand("~/.kaggle")
}
kaggle_conf_file <- file.path(kaggle_conf_dir, "kaggle.json")
if (file.exists(kaggle_conf_file)) {
    kaggle_opts <- fromJSON(kaggle_conf_file)
    names(kaggle_opts) <- toupper(paste0("kaggle_", names(kaggle_opts)))
    do.call(Sys.setenv, kaggle_opts)
}


## ---- Fetch competition files info ----

message("Fetch competition files information")
h <- new_handle(
    proxy = Sys.getenv("KAGGLE_PROXY"),
    httpauth = 1L,
    userpwd = paste(Sys.getenv("KAGGLE_USERNAME"), Sys.getenv("KAGGLE_KEY"), sep = ":")
)
base_url <- "https://www.kaggle.com/api/v1"
slug <- "ga-customer-revenue-prediction"
data_list_path <- "competitions/data/list"
data_dl_path <- "competitions/data/download"
resp <- curl_fetch_memory(url = paste(base_url, data_list_path, slug, sep = "/"), handle = h)
txt <- rawToChar(resp$content)
files <- fromJSON(txt)


## ---- Download data ----

outdir <- "data"
if (!dir.exists(outdir)) {
    message("Create ", shQuote(outdir), "directory")
    dir.create(outdir)
}
for (f in files$ref) {
    outfile <- paste0(file.path(outdir, f), ".zip")
    message("Download ", shQuote(outfile))
    curl_fetch_disk(url = paste(base_url, data_dl_path, slug, f, sep = "/"), path = outfile, handle = h)
}
