-
Notifications
You must be signed in to change notification settings - Fork 17
/
get_google_slides_images.R
85 lines (54 loc) · 1.93 KB
/
get_google_slides_images.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
## Download Google Slides Images
library(tidyverse)
library(stringr)
files <- dir(pattern = glob2rx("0*.Rmd"))
text <- lapply(files, read_lines) %>%
unlist(use.names = FALSE)
links <- grep("docs.google.com/presentation", text, fixed = TRUE,
value = TRUE)
urls <- str_match(links, "(http.*)\\)")
infiles <- urls[, 2]
outfiles <- file.path("gslides",
paste(formatC(seq_along(infiles),
flag = "0", width = 3),
"png", sep = "."))
for(i in seq_along(infiles)) {
message(infiles[i])
download.file(infiles[i], outfiles[i])
Sys.sleep(1)
}
## Edit URLs in Rmd files
files <- dir(pattern = glob2rx("0*.Rmd"))
dict <- cbind(url = infiles, newurl = outfiles)
for(i in seq_along(files)) {
message(files[i])
text <- read_lines(files[i])
for(j in seq_len(nrow(dict))) {
idx <- grep(dict[j, 1], text, fixed = TRUE)
text[idx] <- sub(dict[j, 1], dict[j, 2], text[idx], fixed = TRUE)
}
write_lines(text, files[i])
}
## Replace GitHub images
text <- read_lines("05-prediction.Rmd")
links <- grep("camo.githubusercontent.com", text, fixed = TRUE,
value = TRUE)
urls <- str_match(links, "(http.*)\\)")
infiles <- urls[, 2]
outfiles <- file.path("ghimage",
paste(formatC(seq_along(infiles),
flag = "0", width = 3),
"png", sep = "."))
for(i in seq_along(infiles)) {
message(infiles[i])
download.file(infiles[i], outfiles[i])
Sys.sleep(1)
}
## Replace links
dict <- cbind(url = infiles, newurl = outfiles)
text <- read_lines("05-prediction.Rmd")
for(j in seq_len(nrow(dict))) {
idx <- grep(dict[j, 1], text, fixed = TRUE)
text[idx] <- sub(dict[j, 1], dict[j, 2], text[idx], fixed = TRUE)
}
write_lines(text, "05-prediction.Rmd")