|
| 1 | +#!/usr/bin/env Rscript |
| 2 | +# Written by Candace Savonen Jan 2022 |
| 3 | + |
| 4 | +if (!('devtools' %in% installed.packages())) { |
| 5 | + # install.packages("remotes", repos = "http://cran.us.r-project.org") |
| 6 | +} |
| 7 | + |
| 8 | +if (!('optparse' %in% installed.packages())) { |
| 9 | + # install.packages("optparse", repos = "http://cran.us.r-project.org") |
| 10 | +} |
| 11 | + |
| 12 | +# Find .git root directory |
| 13 | +root_dir <- rprojroot::find_root(rprojroot::has_dir(".git")) |
| 14 | + |
| 15 | + |
| 16 | +Sys.setenv("CHROMOTE_CHROME" = "/usr/bin/vivaldi") |
| 17 | + |
| 18 | +library(optparse) |
| 19 | +library(magrittr) |
| 20 | + |
| 21 | +option_list <- list( |
| 22 | + optparse::make_option( |
| 23 | + c("--repo"), |
| 24 | + type = "character", |
| 25 | + default = NULL, |
| 26 | + help = "GitHub repository name, e.g. jhudsl/OTTR_Template", |
| 27 | + ), |
| 28 | + optparse::make_option( |
| 29 | + c("--git_pat"), |
| 30 | + type = "character", |
| 31 | + default = NULL, |
| 32 | + help = "GitHub personal access token", |
| 33 | + ), |
| 34 | + optparse::make_option( |
| 35 | + c("--output_dir"), |
| 36 | + type = "character", |
| 37 | + default = "resources/chapt_screen_images", |
| 38 | + help = "Output directory where the chapter's screen images should be stored", |
| 39 | + ), |
| 40 | + optparse::make_option( |
| 41 | + c("--base_url"), |
| 42 | + type = "character", |
| 43 | + default = NULL, |
| 44 | + help = "Output directory where the chapter's screen images should be stored", |
| 45 | + ) |
| 46 | +) |
| 47 | + |
| 48 | +# Read the arguments passed |
| 49 | +opt_parser <- optparse::OptionParser(option_list = option_list) |
| 50 | +opt <- optparse::parse_args(opt_parser) |
| 51 | + |
| 52 | +output_folder <- file.path(opt$output_dir) |
| 53 | +if (!dir.exists(output_folder)) { |
| 54 | + dir.create(output_folder, recursive = TRUE) |
| 55 | +} |
| 56 | + |
| 57 | +if (is.null(opt$base_url)) { |
| 58 | + base_url <- cow::get_pages_url(repo_name = opt$repo, git_pat = opt$git_pat) |
| 59 | + base_url <- gsub("/$", "", base_url) |
| 60 | +} |
| 61 | + |
| 62 | +# Collect all the chapter pages for the url given |
| 63 | +chapt_df <- ottrpal::get_chapters(html_page = file.path(root_dir, "docs", "index.html"), |
| 64 | + base_url = base_url) |
| 65 | + |
| 66 | +# Now take screenshots for each |
| 67 | +file_names <- lapply(chapt_df$url, function(url) { |
| 68 | + file_name <- gsub(".html", ".png", file.path(output_folder, basename(url))) |
| 69 | + |
| 70 | + # Get rid of special characters because leanpub no like |
| 71 | + file_name <- gsub(":|?|!|\\'", "", file_name) |
| 72 | + |
| 73 | + # Take the screenshot |
| 74 | + webshot2::webshot(url, file = file_name) |
| 75 | + |
| 76 | + return(file_name) |
| 77 | + |
| 78 | +}) |
| 79 | + |
| 80 | +# Save file of chapter urls and file_names |
| 81 | +chapt_df <- chapt_df %>% |
| 82 | + dplyr::mutate(img_path = unlist(file_names)) |
| 83 | + |
| 84 | +chapt_df %>% |
| 85 | + readr::write_tsv(file.path(output_folder, "chapter_urls.tsv")) |
| 86 | + |
| 87 | +message(paste("Image Chapter key written to: ", file.path(output_folder, "chapter_urls.tsv"))) |
0 commit comments