Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

start functionalizing make screenshots #153

Merged
merged 10 commits into from
Jul 19, 2024
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Imports:
yaml
Suggests:
remotes,
cow,
testthat (>= 3.0.0),
tibble,
utils
Expand Down
7 changes: 7 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export(set_up_leanpub)
export(unzip_pptx)
export(website_to_embed_leanpub)
export(xml_notes)
import(bookdown_path)
import(cow)
import(dplyr)
importFrom(fs,dir_copy)
importFrom(httr,GET)
importFrom(httr,accept_json)
importFrom(httr,config)
Expand All @@ -57,10 +61,13 @@ importFrom(jsonlite,fromJSON)
importFrom(magrittr,"%>%")
importFrom(openssl,aes_cbc_decrypt)
importFrom(readr,write_tsv)
importFrom(rprojroot,find_root)
importFrom(rprojroot,has_dir)
importFrom(utils,download.file)
importFrom(utils,installed.packages)
importFrom(utils,menu)
importFrom(utils,unzip)
importFrom(webshot2,webshot)
importFrom(xml2,read_xml)
importFrom(xml2,xml_find_all)
importFrom(xml2,xml_text)
60 changes: 60 additions & 0 deletions R/screenshot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#' A function to make screenshots from an OTTR bookdown website
#' @description This function creates screenshots of course chapters that are stored in a created output directory
#' @param git_pat default is NULL; required argument; a Git secret
#' @param repo default is NULL; required argument; GitHub repository name, e.g., jhudsl/OTTR_Template
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the best way to communicate that these are required arguments, but by default NULL?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just not have them be NULL if they are required because then it will automatically throw an error if no argument is provided. And docs wise the way you say its "required" is fine 👍

#' @param output_dir default is "resources/chapt_screen_images"; Output directory where the chapter's screen images should be stored
kweav marked this conversation as resolved.
Show resolved Hide resolved
#' @param base_url default is NULL; rendered bookdown URL where screenshots are taken from, if NULL, the function will use the repo_name and and git_pat to find the base_url
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to edit this description to change it from the original in the script. How is it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like all the essentials are here! Only thing you could add if you wanted to be extra nice is a link to the page in GitHub about PATs: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens

#' @import cow
#' @import dplyr
#' @importFrom webshot2 webshot
#' @importFrom magrittr %>%
#' @importFrom rprojroot find_root has_dir
#' @author Candace Savonen
make_screenshots <- function(git_pat = NULL, repo = NULL, output_dir = "resources/chapt_screen_images", base_url = NULL){

# Find .git root directory
root_dir <- find_root(has_dir(".git"))

output_folder <- file.path(output_dir)
if (!dir.exists(output_folder)) {
dir.create(output_folder, recursive = TRUE)
}

if (is.null(base_url)){
base_url <- cow::get_pages_url(repo_name = repo, git_pat = git_pat) #what if these arguments are still NULL/not supplied?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to make this cow function part of ottrpal?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! That's next up! It's mentioned in #137 but I didn't realize I didn't make it its own issue so there is one now: jhudsl/cow#27

base_url <- gsub("/$", "", base_url)
}

# Collect all the chapter pages for the url given
chapt_df <- ottrpal::get_chapters(html_page = file.path(root_dir, "docs", "index.html"),
base_url = base_url)

# Now take screenshots for each
file_names <- lapply(chapt_df$url, function(url){
file_name <- gsub(".html",
".png",
file.path(output_folder, basename(url))
)

# Get rid of special characters because leanpub no like
file_name <- gsub(":|?|!|\\'",
"",
file_name
)

# Take the screenshot
webshot(url, file = file_name)

return(file_name)
})

# Save file of chapter urls and file_names
chapt_df <- chapt_df %>%
dplyr::mutate(img_path = unlist(file_names))

chapt_df %>%
readr::write_tsv(file.path(output_folder, "chapter_urls.tsv"))

message(paste("Image Chapter key written to: ", file.path(output_folder, "chapter_urls.tsv")))

}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I need to return something from this function?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think returning the file path to chapter_urls.tsv makes sense.

Loading