Last active
January 16, 2024 19:04
-
-
Save howardbaik/e95ce3c836c1871b97341333f0bb2042 to your computer and use it in GitHub Desktop.
Extract list of attendees
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(httr2) | |
library(googlesheets4) | |
library(purrr) | |
# Function that creates OAuth client for Google APIs | |
google_client <- function() { | |
httr2::oauth_client( | |
id = "[Client ID]", | |
secret = "[Client secret]", | |
token_url = "https://oauth2.googleapis.com/token", | |
name = "howard-google-docs" | |
) | |
} | |
# Create a new HTTP request | |
open_agenda_url <- "https://docs.googleapis.com/v1/documents/1sPVsWeBZKn9A8sbfM666aRYwxVq5Emrhp0ws_R5X1n4" | |
req <- request(open_agenda_url) | |
# Authenticate and perform request | |
resp <- req %>% | |
req_oauth_auth_code( | |
client = google_client(), | |
auth_url = "https://accounts.google.com/o/oauth2/v2/auth", | |
scope = "https://www.googleapis.com/auth/documents" | |
) %>% | |
req_perform() | |
# Extract content from Google Doc | |
resp_body_json <- resp %>% resp_body_json() | |
content <- resp_body_json$body$content | |
# Iterate and look for list of attendees | |
result <- c() | |
for (ii in seq(1, length(content))) { | |
if (!is.null(content[[ii]]$paragraph$elements)) { | |
for (jj in seq(1, length(content[[ii]]$paragraph$elements))) { | |
if (!is.null(content[[ii]]$paragraph$elements[[jj]]$textRun$content) && | |
(content[[ii]]$paragraph$elements[[jj]]$textRun$content == "When you join the meeting, please enter your name and institution below:\n" | | |
content[[ii]]$paragraph$elements[[jj]]$textRun$content == "When you join the meeting, please enter your name and institution below:")) { | |
kk <- ii + 1 | |
while (!is.null(content[[kk]]$paragraph$elements[[1]]$textRun$content) && content[[kk]]$paragraph$elements[[1]]$textRun$content != "\n") { | |
result <- c(result, content[[kk]]$paragraph$elements[[1]]$textRun$content) | |
kk <- kk + 1 | |
} | |
} | |
} | |
} else { | |
next | |
} | |
} | |
# Post-processing | |
result <- gsub("\n$", "", result) | |
# Final result | |
result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fetch Client ID and Client Secret from Google Cloud
Fill in the blanks as follows: