-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDownloadAllData.r
executable file
·100 lines (81 loc) · 2.98 KB
/
DownloadAllData.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# This R script downloads all the data files and puts them in a folder labelled "data"
# The files are organised by node code within "data"
# This only needs to be run once, because it's a bit slow!
require(ReplicationProjectTools)
require(glue)
require(httr)
require(jsonlite)
LabNames <- c("Corballis", "Chen", "Mammarella", "Treccani",
"Javier", "Lukavský", "Lindemann", "Cipora", "Mieth",
"Hancock", "Toomarian", "Holmes", "Ocampo", "Goffin",
"Bryce", "Colling (Szűcs)", "Moeller")
Nodes <- c("yu8hp", "vzt3a", "8gdp3", "ecdmw", "xmyd9",
"6ea38", "xb6t2", "fkjbz", "t5y2h", "2t9re", "encwv",
"pqcf4", "kx4w9", "kwg95", "5hgk4", "3s4uh", "nzxwr")
system("mkdir data/unprocessed_data")
GetAllNodeFiles <- function(node) {
GetFileListFromNode <- function(node) {
token = 'token_code'# Replace token_code with your OSF token #ReplicationProjectTools::MyOSFToken()
page = 1
url = glue('https://api.osf.io/v2/nodes/{node}/files/osfstorage/?page={page}')
web.content = GET(url, add_headers(Authorization = paste("Bearer", token)))
#web.content = GET(url)
web.content.parsed = fromJSON(content(web.content, 'text', encoding = "UTF-8"))
pagesToGet = ceiling(web.content.parsed$links$meta$total / web.content.parsed$links$meta$per_page)
GetFilesFromOSF <- function(this.url, token) {
this.page = GET(this.url, add_headers(Authorization = paste("Bearer", token)))
#this.page = GET(this.url)
this.page.parsed = fromJSON(content(this.page, 'text', encoding = "UTF-8"))
this.names = this.page.parsed$data$attributes$name
this.links = this.page.parsed$data$links$download
return(tibble(
names = this.names,
links = this.links,
node = node
))
}
files = lapply(1:pagesToGet, function(x)
GetFilesFromOSF(
glue(
'https://api.osf.io/v2/nodes/{node}/files/osfstorage/?page={x}'
),
token
))
return(Reduce(function(x, y)
rbind(x, y), files))
}
this.nodeFiles = GetFileListFromNode(node)
this.nodeFiles = this.nodeFiles[which((this.nodeFiles %>% pull(names) %>% grepl(pattern = ".edf")) == FALSE),]
dataFolder = file.path(getwd(), "data/unprocessed_data")
DownloadFileFromNode <- function(name, link, node, dataFolder) {
token = ReplicationProjectTools::MyOSFToken()
bin.content <-
GET(link, add_headers(Authorization = paste("Bearer", token)))
#GET(link)
name = tolower(name) # always lower case
filename = glue('{dataFolder}/{node}/{name}')
file.obj = file(filename, "wb")
writeBin(object = content(bin.content, "raw"), con = file.obj)
close(file.obj)
msg = glue('{name} downloaded ok from {node}!')
cat(msg)
cat("\n")
return(msg)
}
system(paste("mkdir ", paste0(dataFolder, "/", node)))
pmap(
list(
this.nodeFiles$names,
this.nodeFiles$links,
this.nodeFiles$node,
dataFolder
),
DownloadFileFromNode
)
}
require(foreach)
require(doParallel)
doParallel::registerDoParallel(cores = 8)
foreach(i = 1 : length(Nodes)) %dopar% {
GetAllNodeFiles(node = Nodes[i])
}