From 62c8ae65023568ff64155003a127b25d312f12b1 Mon Sep 17 00:00:00 2001 From: Kate Isaac <41767733+kweav@users.noreply.github.com> Date: Mon, 8 Jul 2024 15:03:02 -0400 Subject: [PATCH 1/8] start functionalizing make screenshots --- DESCRIPTION | 3 ++- NAMESPACE | 6 +++++ R/screenshot.R | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 R/screenshot.R diff --git a/DESCRIPTION b/DESCRIPTION index 64a520e..9921720 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -40,6 +40,7 @@ Imports: yaml Suggests: remotes, + cow, testthat, tibble, utils @@ -48,4 +49,4 @@ VignetteBuilder: ByteCompile: true Encoding: UTF-8 LazyData: true -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 diff --git a/NAMESPACE b/NAMESPACE index 68aaac3..9f8192a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -56,6 +56,9 @@ export(set_up_leanpub) export(simple_references) export(unzip_pptx) export(xml_notes) +import(bookdown_path) +import(cow) +import(dplyr) importFrom(fs,dir_copy) importFrom(httr,GET) importFrom(httr,accept_json) @@ -68,10 +71,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) diff --git a/R/screenshot.R b/R/screenshot.R new file mode 100644 index 0000000..d2a13d2 --- /dev/null +++ b/R/screenshot.R @@ -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 +#' @param output_dir default is "resources/chapt_screen_images"; Output directory where the chapter's screen images should be stored +#' @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 +#' @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? + 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"))) + +} \ No newline at end of file From 23f0fa5d74ae39c1ea16ab08e34ca189761783a5 Mon Sep 17 00:00:00 2001 From: Kate Isaac <41767733+kweav@users.noreply.github.com> Date: Tue, 9 Jul 2024 11:52:10 -0400 Subject: [PATCH 2/8] Update R/screenshot.R Co-authored-by: Candace Savonen --- R/screenshot.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/screenshot.R b/R/screenshot.R index d2a13d2..405cb63 100644 --- a/R/screenshot.R +++ b/R/screenshot.R @@ -2,7 +2,7 @@ #' @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 -#' @param output_dir default is "resources/chapt_screen_images"; Output directory where the chapter's screen images should be stored +#' @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 #' @import cow #' @import dplyr From 0aabec8daec4b20deb25f61f4e74cb2dd11cadb2 Mon Sep 17 00:00:00 2001 From: Kate Isaac <41767733+kweav@users.noreply.github.com> Date: Tue, 9 Jul 2024 13:39:55 -0400 Subject: [PATCH 3/8] address quick suggestions --- R/screenshot.R | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/R/screenshot.R b/R/screenshot.R index 405cb63..e1b7c39 100644 --- a/R/screenshot.R +++ b/R/screenshot.R @@ -1,16 +1,22 @@ #' 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 +#' @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 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){ +#' @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")) @@ -57,4 +63,6 @@ make_screenshots <- function(git_pat = NULL, repo = NULL, output_dir = "resource message(paste("Image Chapter key written to: ", file.path(output_folder, "chapter_urls.tsv"))) + return(file.path(output_folder, "chapter_urls.tsv")) + } \ No newline at end of file From 4f85a8c9ca1cb474cdb7c360bb9abea5f45bda8a Mon Sep 17 00:00:00 2001 From: Kate Isaac <41767733+kweav@users.noreply.github.com> Date: Tue, 9 Jul 2024 14:00:53 -0400 Subject: [PATCH 4/8] copy over cow function to ottrpal --- NAMESPACE | 4 +- R/get_pages_url.R | 81 +++++++++++++++++++++++++++++++++++++++++ R/screenshot.R | 3 +- man/get_pages_url.Rd | 36 ++++++++++++++++++ man/make_screenshots.Rd | 38 +++++++++++++++++++ 5 files changed, 157 insertions(+), 5 deletions(-) create mode 100644 R/get_pages_url.R create mode 100644 man/get_pages_url.Rd create mode 100644 man/make_screenshots.Rd diff --git a/NAMESPACE b/NAMESPACE index 37fec0f..a97a304 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) @@ -46,10 +47,7 @@ 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) diff --git a/R/get_pages_url.R b/R/get_pages_url.R new file mode 100644 index 0000000..d65f082 --- /dev/null +++ b/R/get_pages_url.R @@ -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_chapters("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) +} diff --git a/R/screenshot.R b/R/screenshot.R index e1b7c39..5315a77 100644 --- a/R/screenshot.R +++ b/R/screenshot.R @@ -5,7 +5,6 @@ #' @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 cow #' @import dplyr #' @importFrom webshot2 webshot #' @importFrom magrittr %>% @@ -27,7 +26,7 @@ make_screenshots <- function(git_pat, repo, output_dir = "resources/chapt_screen } 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? + 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) } diff --git a/man/get_pages_url.Rd b/man/get_pages_url.Rd new file mode 100644 index 0000000..3ee5765 --- /dev/null +++ b/man/get_pages_url.Rd @@ -0,0 +1,36 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get_pages_url.R +\name{get_pages_url} +\alias{get_pages_url} +\title{Retrieve pages url for a repo} +\usage{ +get_pages_url(repo_name, git_pat = NULL, verbose = FALSE, keep_json = FALSE) +} +\arguments{ +\item{repo_name}{The full name of the repo to get bookdown chapters from. +e.g. "jhudsl/OTTR_Template"} + +\item{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}} + +\item{verbose}{TRUE/FALSE do you want more progress messages?} + +\item{keep_json}{verbose TRUE/FALSE keep the json file locally?} +} +\value{ +a data frame with the repository with the following columns: +data_level, data_path, chapt_name, url, repository name +} +\description{ +Given an repository on GitHub, retrieve the pages URL for it. +} +\examples{ +\dontrun{ + +usethis::create_github_token() + +get_chapters("jhudsl/Documentation_and_Usability") +} +} diff --git a/man/make_screenshots.Rd b/man/make_screenshots.Rd new file mode 100644 index 0000000..d3d4c9d --- /dev/null +++ b/man/make_screenshots.Rd @@ -0,0 +1,38 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/screenshot.R +\name{make_screenshots} +\alias{make_screenshots} +\title{A function to make screenshots from an OTTR bookdown website} +\usage{ +make_screenshots( + git_pat, + repo, + output_dir = "resources/chapt_screen_images", + base_url = NULL +) +} +\arguments{ +\item{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} + +\item{repo}{required argument; GitHub repository name, e.g., jhudsl/OTTR_Template} + +\item{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.} + +\item{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} +} +\value{ +the file path for file where chapter urls are saved +} +\description{ +This function creates screenshots of course chapters that are stored in a created output directory +} +\examples{ +\dontrun{ + + make_screenshots(Sys.getenv("secrets.GH_PAT"), "jhudsl/OTTR_Template") + +} +} +\author{ +Candace Savonen +} From edddb94ed17bd1d6a7fb491cad5943dac05f3610 Mon Sep 17 00:00:00 2001 From: Kate Isaac <41767733+kweav@users.noreply.github.com> Date: Tue, 9 Jul 2024 14:36:25 -0400 Subject: [PATCH 5/8] update example for get pages and add export to screenshot --- NAMESPACE | 1 + R/get_pages_url.R | 2 +- R/screenshot.R | 7 +++++++ man/get_pages_url.Rd | 2 +- 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index a97a304..aa7ad95 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -33,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) diff --git a/R/get_pages_url.R b/R/get_pages_url.R index d65f082..1447125 100644 --- a/R/get_pages_url.R +++ b/R/get_pages_url.R @@ -23,7 +23,7 @@ #' #' usethis::create_github_token() #' -#' get_chapters("jhudsl/Documentation_and_Usability") +#' get_pages_url("jhudsl/Documentation_and_Usability") #' } get_pages_url <- function(repo_name, git_pat = NULL, diff --git a/R/screenshot.R b/R/screenshot.R index 5315a77..50e3084 100644 --- a/R/screenshot.R +++ b/R/screenshot.R @@ -1,15 +1,22 @@ #' 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") diff --git a/man/get_pages_url.Rd b/man/get_pages_url.Rd index 3ee5765..8f45fac 100644 --- a/man/get_pages_url.Rd +++ b/man/get_pages_url.Rd @@ -31,6 +31,6 @@ Given an repository on GitHub, retrieve the pages URL for it. usethis::create_github_token() -get_chapters("jhudsl/Documentation_and_Usability") +get_pages_url("jhudsl/Documentation_and_Usability") } } From 8dd642db7de50f2f1b22067c4fdef3e8bded8777 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Wed, 10 Jul 2024 14:45:13 -0400 Subject: [PATCH 6/8] Update R-CMD-check.yaml --- .github/workflows/R-CMD-check.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 091012f..ffca466 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -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 From d8f16227fca061e0f5e08b67ddc3fa4952f7579d Mon Sep 17 00:00:00 2001 From: Kate Isaac <41767733+kweav@users.noreply.github.com> Date: Wed, 10 Jul 2024 14:49:21 -0400 Subject: [PATCH 7/8] Update DESCRIPTION --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index 593fccc..0f4b8a9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -35,6 +35,7 @@ Imports: rprojroot, rvest, stringr, + webshot2, xml2, yaml Suggests: From 55005af8eea005a062180ca978991c749315b41d Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Fri, 19 Jul 2024 12:06:40 -0400 Subject: [PATCH 8/8] Fix description file for cow dependency --- DESCRIPTION | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 0f4b8a9..6d0fe56 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -39,11 +39,11 @@ Imports: xml2, yaml Suggests: - remotes, - cow, testthat (>= 3.0.0), tibble, utils +Remotes: + jhudsl/cow VignetteBuilder: knitr ByteCompile: true