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

Supply google slide notes to fig.alt #122

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export(get_gs_pptx)
export(get_image_from_slide)
export(get_image_link_from_slide)
export(get_object_id_notes)
export(get_presentation_id)
export(get_slide_id)
export(get_slide_page)
export(good_quiz_path)
export(gs_id_from_slide)
export(gs_png_download)
Expand Down
11 changes: 11 additions & 0 deletions R/auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ get_token <- function() {
.tokenEnv$Token
}


### Declare all the scopes
scopes_list <- c(
"https://www.googleapis.com/auth/drive",
Expand Down Expand Up @@ -123,3 +124,13 @@ app_set_up <- function() {

return(list(app = app, endpoint = endpoint))
}

set_default_creds <- function() {
decrypted <- openssl::aes_cbc_decrypt(
readRDS(encrypt_creds_user_path()),
key = readRDS(key_encrypt_creds_path())
)
auth_from_secret(access_token = unserialize(decrypted)$access_token,
refresh_token = unserialize(decrypted)$refresh_token)

}
1 change: 1 addition & 0 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ encrypt_creds_user_path <- function() {
full.names = TRUE
)
}

40 changes: 25 additions & 15 deletions R/gs_png.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Get Slide ID from URL
#' Get Presentation ID from URL
#'
#' @param x URL of slide
#'
Expand All @@ -11,8 +11,8 @@
#' "1Tg-GTGnUPduOtZKYuMoelqUNZnUp3vvg_7TtpUPL7e8",
#' "/edit#slide=id.g154aa4fae2_0_58"
#' )
#' get_slide_id(x)
get_slide_id <- function(x) {
#' get_presentation_id(x)
get_presentation_id <- function(x) {
x <- sub(".*presentation/", "", x)
x <- sub("/d/e", "/d", x) # if you publish by accident
x <- sub("^(d|e)/", "", x)
Expand All @@ -35,15 +35,16 @@ get_slide_id <- function(x) {
#' "12DPZgPteQBwgal6kSPP58zhPhjZ7QSPZLe3NkA8M3eo/edit",
#' "#slide=id.gc8648f14c3_0_397&t=4"
#' )
#' id <- get_slide_id(url)
#' id <- get_presentation_id(url)
#' gs_png_url(url)
gs_png_url <- function(url) {
id <- get_slide_id(url)
slide_id <- get_slide_page(url)
gs_png_id(id, slide_id)
presentation_id <- get_presentation_id(url)
slide_id <- get_slide_id(url)
gs_png_id(presentation_id, slide_id)
}

gs_png_id <- function(id, slide_id) {
# Get URL to download slide as PNG
gs_png_id <- function(presentation_id, slide_id) {
if (any(grepl("^id[.]", slide_id))) {
warning(
"slide ids usually don't have format of id.gc*, ",
Expand All @@ -52,15 +53,16 @@ gs_png_id <- function(id, slide_id) {
}
paste0(
"https://docs.google.com/presentation/d/",
id,
"/export/png?id=", id,
presentation_id,
"/export/png?id=", presentation_id,
"&pageid=", slide_id
)
}

#' Extract Slide ID from URL
#' @export
#' @rdname gs_png_url
get_slide_page <- function(url) {
get_slide_id <- function(url) {
parsed <- httr::parse_url(url)
slide_id <- parsed$query$pageid
if (length(slide_id) == 0 || nchar(slide_id) == 0) {
Expand All @@ -73,16 +75,17 @@ get_slide_page <- function(url) {
slide_id
}

#' Download a slide from Google Slides
#' @export
#' @rdname gs_png_url
#' @param output_dir path to output png
#' @param overwrite should the slide PNG be overwritten?
gs_png_download <- function(url, output_dir = ".", overwrite = TRUE) {
id <- get_slide_id(url)
slide_id <- get_slide_page(url)
presentation_id <- get_presentation_id(url)
slide_id <- get_slide_id(url)
url <- gs_png_url(url)
dir.create(output_dir, recursive = TRUE, showWarnings = FALSE)
outfile <- file.path(output_dir, paste0(id, "_", slide_id, ".png"))
outfile <- file.path(output_dir, paste0(presentation_id, "_", slide_id, ".png"))
if (!file.exists(outfile) || overwrite) {
curl::curl_download(url, destfile = outfile, quiet = FALSE)
}
Expand All @@ -97,7 +100,14 @@ gs_png_download <- function(url, output_dir = ".", overwrite = TRUE) {
include_slide <- function(url,
output_dir = knitr::opts_chunk$get("fig.path"),
overwrite = TRUE, ...) {
get_gs_pptx(url)
# Get speaker notes for ALL slides
all_speaker_notes <- get_object_id_notes(url)

# Get slide speaker notes
slide_id <- get_slide_id(url)
slide_speaker_notes <- all_speaker_notes[all_speaker_notes$id == slide_id, "notes"]

alt_text <<- slide_speaker_notes
outfile <- gs_png_download(url, output_dir, overwrite = overwrite)
knitr::include_graphics(outfile, ...)
}
33 changes: 16 additions & 17 deletions R/notes_to_fig_alt.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#' Download Google Slides pptx file
#' Download Google Slides as PPTX
#'
#' @param id Identifier of Google slides presentation, passed to
#' \code{\link{get_slide_id}}
#' \code{\link{get_presentation_id}}
#'
#' @note This downloads presentations if they are public and also try to make
#' sure it does not fail on large files
#' @return Downloaded file (in temporary directory)
#' @export
get_gs_pptx <- function(id) {
id <- as.character(id)
pres_id <- get_slide_id(id)
url <- export_url(id = pres_id)
presentation_id <- get_presentation_id(id)
url <- export_url(presentation_id = presentation_id)

pptx_file <- file.path(paste0(pres_id, ".pptx"))
pptx_file <- file.path(paste0(presentation_id, ".pptx"))

# Only download it if it isn't yet present
if (!file.exists(pptx_file)) {
Expand All @@ -21,7 +21,7 @@ get_gs_pptx <- function(id) {
fr_header <- result$headers$`x-frame-options`
if (!is.null(fr_header)) {
if (all(fr_header == "DENY")) {
warn_them <- TRUE
warn_them <- TRUE
}
}
if (httr::status_code(result) >= 300) {
Expand All @@ -38,7 +38,7 @@ get_gs_pptx <- function(id) {
}
if (warn_them) {
warning(
paste0(
paste0(
"This presentation may not be available, ",
"did you turn link sharing on?"
)
Expand All @@ -48,13 +48,12 @@ get_gs_pptx <- function(id) {
pptx_file
}


export_url <- function(id, page_id = NULL, type = "pptx") {
export_url <- function(presentation_id, slide_id = NULL, type = "pptx") {
url = paste0(
"https://docs.google.com/presentation/d/",
id, "/export/", type, "?id=", id)
if (!is.null(page_id)) {
url = paste0(url, "&pageid=", page_id)
presentation_id, "/export/", type, "?id=", presentation_id)
if (!is.null(slide_id)) {
url = paste0(url, "&pageid=", slide_id)
}
url
}
Expand Down Expand Up @@ -227,14 +226,14 @@ unzip_pptx <- function(file) {
props_dir <- file.path(tdir, "docProps")
props_file <- file.path(props_dir, "core.xml")
ari_core_file <- system.file("extdata", "docProps",
"core.xml",
package = "ariExtra"
"core.xml",
package = "ariExtra"
)
# copy core.xml from ariExtra to props_file
if (!dir.exists(props_file)) {
dir.create(props_dir, recursive = TRUE)
file.copy(ari_core_file, props_file,
overwrite = TRUE
overwrite = TRUE
)
}

Expand Down Expand Up @@ -302,10 +301,10 @@ xml_notes <- function(file, collapse_text = TRUE, xpath = "//a:r//a:t") {
#' }
extract_object_id = function(slide_url, token = NULL, access_token = NULL, refresh_token = NULL) {
# Get Slide ID from URL
id <- get_slide_id(slide_url)
id <- get_presentation_id(slide_url)
# Using Slide ID, create url that we'll send to GET
get_url <- gsub("{presentationId}", id,
"https://slides.googleapis.com/v1/presentations/{presentationId}", fixed=TRUE)
"https://slides.googleapis.com/v1/presentations/{presentationId}", fixed=TRUE)

# if token not provided, fetch token
if (is.null(token)) {
Expand Down
4 changes: 2 additions & 2 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.onAttach <- function(libname, pkgname) {
packageStartupMessage("Use the authorize() function to begin. This gives the package the proper credentials to run.")
.onLoad <- function(libname, pkgname) {
set_default_creds()
}
Binary file modified inst/extdata/tmp/encrypted_default_user_creds.rds
Binary file not shown.
6 changes: 3 additions & 3 deletions man/get_gs_pptx.Rd

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

12 changes: 6 additions & 6 deletions man/get_slide_id.Rd → man/get_presentation_id.Rd

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

10 changes: 7 additions & 3 deletions man/gs_png_url.Rd

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

Loading