Skip to content

Commit

Permalink
Merge pull request #153 from jhudsl/ki/refactor
Browse files Browse the repository at this point in the history
start functionalizing make screenshots
  • Loading branch information
cansavvy authored Jul 19, 2024
2 parents 88a03cb + 55005af commit 3d56b9f
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
rcmdcheck::rcmdcheck(args = c("--no-manual"), check_dir = "check")
shell: Rscript {0}
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
GH_PAT: ${{ secrets.GH_PAT }}

- name: Check testthat
id: check_check
Expand Down
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ Imports:
rprojroot,
rvest,
stringr,
webshot2,
xml2,
yaml
Suggests:
remotes,
testthat (>= 3.0.0),
tibble,
utils
Remotes:
jhudsl/cow
VignetteBuilder:
knitr
ByteCompile: true
Expand Down
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export(get_gs_pptx)
export(get_image_from_slide)
export(get_image_link_from_slide)
export(get_object_id_notes)
export(get_pages_url)
export(get_slide_id)
export(get_slide_page)
export(get_yaml_spec)
Expand All @@ -32,6 +33,7 @@ export(gs_png_download)
export(gs_png_url)
export(include_slide)
export(make_embed_markdown)
export(make_screenshots)
export(output_destination)
export(parse_q_tag)
export(parse_quiz)
Expand All @@ -46,6 +48,7 @@ export(set_up_leanpub)
export(unzip_pptx)
export(website_to_embed_leanpub)
export(xml_notes)
import(dplyr)
importFrom(httr,GET)
importFrom(httr,accept_json)
importFrom(httr,config)
Expand All @@ -57,10 +60,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)
81 changes: 81 additions & 0 deletions R/get_pages_url.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#' Retrieve pages url for a repo
#'
#' Given an repository on GitHub, retrieve the pages URL for it.
#'
#' @param repo_name The full name of the repo to get bookdown chapters from.
#' e.g. "jhudsl/OTTR_Template"
#' @param git_pat If private repositories are to be retrieved, a github personal
#' access token needs to be supplied. If none is supplied, then this will attempt to
#' grab from a git pat set in the environment with usethis::create_github_token().
#' Authorization handled by \link[cow]{get_git_auth}
#' @param verbose TRUE/FALSE do you want more progress messages?
#' @param keep_json verbose TRUE/FALSE keep the json file locally?
#'
#' @return a data frame with the repository with the following columns:
#' data_level, data_path, chapt_name, url, repository name
#'
#' @importFrom magrittr %>%
#' @import dplyr
#'
#' @export
#'
#' @examples \dontrun{
#'
#' usethis::create_github_token()
#'
#' get_pages_url("jhudsl/Documentation_and_Usability")
#' }
get_pages_url <- function(repo_name,
git_pat = NULL,
verbose = FALSE,
keep_json = FALSE) {
page_url <- NA

# Try to get credentials other way
auth_arg <- get_git_auth(git_pat = git_pat, quiet = !verbose)

git_pat <- try(auth_arg$password, silent = TRUE)

if (grepl("Error", git_pat[1])) {
warning("Cannot retrieve page info without GitHub credentials. Passing an NA.")
}

# We can only retrieve pages if we have the credentials
if (!grepl("Error", git_pat[1])) {
exists <- check_git_repo(
repo_name = repo_name,
git_pat = git_pat,
verbose = FALSE
)

if (exists) {
# Get repo info
repo_info <- get_repo_info(
repo_name = repo_name,
git_pat = git_pat
)

# Declare URL
url <- paste0("https://api.github.com/repos/", repo_name, "/pages")

# Github api get
response <- httr::GET(
url,
httr::add_headers(Authorization = paste0("token ", auth_arg$password)),
httr::accept_json()
)

if (httr::http_error(response)) {
if (verbose) {
warning(paste0("url: ", url, " failed"))
}
} else {
# Get content as JSON
page_info <- httr::content(response, as = "parsed")

page_url <- page_info$html_url
}
}
}
return(page_url)
}
74 changes: 74 additions & 0 deletions R/screenshot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#' 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 required argument; a Git secret -- see https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens for more info
#' @param repo required argument; GitHub repository name, e.g., jhudsl/OTTR_Template
#' @param output_dir default is "resources/chapt_screen_images"; Output directory where the chapter's screen images should be stored. For OTTR courses, don't change this unless you've changed the downstream functions accordingly.
#' @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
#'
#' @return the file path for file where chapter urls are saved
#'
#' @import dplyr
#' @importFrom webshot2 webshot
#' @importFrom magrittr %>%
#' @importFrom rprojroot find_root has_dir
#'
#' @author Candace Savonen
#'
#' @export
#'
#' @examples \dontrun{
#'
#' make_screenshots(Sys.getenv("secrets.GH_PAT"), "jhudsl/OTTR_Template")
#'
#' }
make_screenshots <- function(git_pat, repo, 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 <- ottrpal::get_pages_url(repo_name = repo, git_pat = git_pat) #what if these arguments are still NULL/not supplied?
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")))

return(file.path(output_folder, "chapter_urls.tsv"))

}
36 changes: 36 additions & 0 deletions man/get_pages_url.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions man/make_screenshots.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3d56b9f

Please sign in to comment.