forked from darsa-group/ACCESS-2024
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_preprocess.R
More file actions
130 lines (100 loc) · 3.13 KB
/
Copy path_preprocess.R
File metadata and controls
130 lines (100 loc) · 3.13 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
library("googlesheets4")
library("glue")
library("dplyr")
library("stringr")
library("rjson")
ROLES <- c("keynote", "assistant", "organiser")
SOCIAL_LINKS_MAP <- list(
orcid=list(
icon="orcid",
icon_pack= "fab",
name= "ORCID",
url= "https://orcid.org/{value}"
),
github=list(
icon="github",
icon_pack= "fab",
name= "Github",
url= "https://github.com/{value}"
),
research_gate=list(
icon="researchgate",
icon_pack= "fab",
name= "RG",
url= "https://www.researchgate.net/profile/{value}"
)
)
make_links <- function(row){
links <- lapply(names(SOCIAL_LINKS_MAP), function(n){
value <- row[[n]]
if(is.na(value))
return(NULL)
l <- SOCIAL_LINKS_MAP[[n]]
l$url <- glue(l$url)
l <-rjson::toJSON(l )
l
})
links <- links[lengths(links) != 0]
links <- sprintf("[%s]", paste(links, collapse=", "))
links
}
DEFAULT_PICTURE_FILE<- "content/people/_default_pict.png"
PEOPLE_TEMPLATE_FILE <- "content/people/_people.md.template"
AUTO_PPL_DIR_PREFIX <- "auto-"
creds = Sys.getenv('GOOGLE_SERVICE_JSON_KEY')
stopifnot( creds != "") #, 'Could not find environment variable `GOOGLE_SERVICE_JSON_KEY`')
people_sheet = Sys.getenv('PEOPLE_GOOGLE_SHEET_ID')
stopifnot( people_sheet != "") #, 'Could not find environment variable `PEOPLE_GOOGLE_SHEET_ID`')
people_template = paste(readLines(PEOPLE_TEMPLATE_FILE), collapse="\n")
stopifnot(!is.null(people_template))
if(file.exists(creds)){
file.copy(from = creds, to=tmpf <- tempfile(fileext = ".json"))
}
if(!file.exists(creds))
cat(creds, file = tmpf <- tempfile(fileext = ".json"))
gs4_auth(
path=tmpf,
scopes = "https://www.googleapis.com/auth/spreadsheets.readonly",cache=FALSE)
df <- read_sheet(people_sheet, na=c("", "NA"))
df <- filter(df, !is.na(id))
print(df)
make_people <- function(id_){
d <- paste(tempdir(), id_, sep="/")
dir.create(d)
row <- filter(df, id==id_)
print(row)
themes <- select(row, starts_with("theme_"))
themes <- unlist(as.vector(themes))
themes <- names(themes[themes])
themes <- str_replace(themes,"theme_","")
tags <- sapply(ROLES,function(i) {
print(paste0("role_",i))
ifelse(row[[paste0("role_",i)]], i, NA)}
)
row$weight = row$role_organiser * 7 + row$role_keynote * 5 + row$role_assistant * 1
tags <- na.omit(tags)
#tags <- c(row$role)
#todo add an alumni tag if end date is in the past
row$tags <- glue('[{paste(tags, collapse=", ")}]')
row$social_links <- make_links(row)
content <-glue_data(row,people_template)
cat(content, file= paste(d,"index.md", sep="/"))
dst_pict_file <- paste(d,'featured.jpg',sep='/')
if(!is.na(row$picture_url)){
picture_file <- paste("assets", "image", row$picture_url, sep="/")
if(file.exists(picture_file)){
file.copy(picture_file, dst_pict_file)
}
else{
system(glue("curl '{row$picture_url}' > {dst_pict_file}"))
}
}
else{
warning(glue('No picture for member `{id_}`'))
file.copy(DEFAULT_PICTURE_FILE, dst_pict_file)
}
final_dir <- paste("content", "people",paste0(AUTO_PPL_DIR_PREFIX, id_), sep="/")
cmd = glue("rm {final_dir} -rf && mv {d} {final_dir}")
system(cmd)
}
o <- lapply(df$id, make_people)