-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.R
66 lines (55 loc) · 1.99 KB
/
utils.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
library(googledrive)
library(stringr)
# Helpful utility to template an arbitrary list of string arguments and then dump them to the console with a terminating newline
wg <- function (...) {
args <- list(...)
line <- paste(sapply(args, str_glue, .envir = parent.frame()), collapse = "")
# Output the result using writeLines
writeLines(line)
}
# Other incarnation at https://github.com/usda-nifa-b-team/b-team/blob/main/scripts/utils.R
downloadGdrive <- function (id, file_path, overwrite = FALSE) {
if (overwrite || !file.exists(file_path)) {
drive_download(as_id(id), path = file_path, overwrite = TRUE)
}
}
idToDrib <- function (id) {
path <- str_glue("https://drive.google.com/drive/folders/{id}")
drib <- drive_get(as_id(path))
}
# Adapted from https://stackoverflow.com/a/64687628
downloadGdriveFolder <- function (id, file_path, skip_if_exists = TRUE) {
exists <- file.exists(file_path)
if (!exists || !skip_if_exists) {
if (!exists) {
dir.create(file_path)
}
# folder link to id
folder_drib = idToDrib(id)
# find files in folder
files = drive_ls(folder_drib)
cat("Fetching ", nrow(files), " files in folder ", folder_drib$name, "\n")
# loop dirs and download files inside them
for (i in seq_along(files$name)) {
resource <- files$drive_resource[[i]]
target <- str_c(file_path, "/", resource$name)
if (resource$mimeType == "application/vnd.google-apps.folder") {
cat (resource$name, " is a folder\n")
downloadGdriveFolder(resource$id, target, skip_if_exists)
# If there were subfolders, this would list them:
# i_dir = drive_ls(files[i, ])
}
else {
try({
if (file.exists(target)) {
wg("File {target} already exists, skipping download")
} else {
drive_download(as_id(files$id[i]), path = target)
}
})
}
}
} else {
wg("Path {file_path} already exists, skipping download\n")
}
}