generated from jhudsl/OTTR_Template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
make_screenshots.R
74 lines (62 loc) · 2.09 KB
/
make_screenshots.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
#!/usr/bin/env Rscript
# Written by Candace Savonen Jan 2022
if (!('devtools' %in% installed.packages())) {
# install.packages("remotes", repos = "http://cran.us.r-project.org")
}
if (!('optparse' %in% installed.packages())) {
# install.packages("optparse", repos = "http://cran.us.r-project.org")
}
webshot::install_phantomjs()
library(optparse)
library(magrittr)
option_list <- list(
optparse::make_option(
c("--repo"),
type = "character",
default = NULL,
help = "GitHub repository name, e.g. jhudsl/OTTR_Template",
),
optparse::make_option(
c("--git_pat"),
type = "character",
default = NULL,
help = "GitHub personal access token",
),
optparse::make_option(
c("--output_dir"),
type = "character",
default = "resources/chapt_screen_images",
help = "Output directory where the chapter's screen images should be stored",
),
optparse::make_option(
c("--base_url"),
type = "character",
default = NULL,
help = "Output directory where the chapter's screen images should be stored",
)
)
# Read the arguments passed
opt_parser <- optparse::OptionParser(option_list = option_list)
opt <- optparse::parse_args(opt_parser)
output_folder <- file.path(opt$output_dir)
if (!dir.exists(output_folder)) {
dir.create(output_folder, recursive = TRUE)
}
if (is.null(opt$base_url)) {
base_url <- cow::get_pages_url(repo_name = opt$repo, git_pat = opt$git_pat)
base_url <- gsub("/$", "", base_url)
}
chapt_df <- ottrpal::get_chapters(base_url = file.path(base_url, "no_toc/"))
file_names <- lapply(chapt_df$url, function(url) {
file_name <- gsub(".html", ".png", file.path(output_folder, basename(url)))
# Get rid of special characters
webshot::webshot(url, file_name)
file_name <- gsub(":|?|!|\\'", "", file_name)
message(paste("Screenshot saved:", file_name))
return(file_name)
})
# Save file of chapter urls and file_names
chapt_df %>%
dplyr::mutate(img_path = unlist(file_names)) %>%
readr::write_tsv(file.path(output_folder, "chapter_urls.tsv"))
message(paste("Image Chapter key written to: ", file.path(output_folder, "chapter_urls.tsv")))