Skip to content

Commit

Permalink
Partial work toward more complete automation. (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonthegeek authored May 24, 2024
1 parent 5325704 commit 43c0476
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 42 deletions.
23 changes: 13 additions & 10 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,15 @@ License: MIT + file LICENSE
URL: https://github.com/r4ds/bookclubcron,
https://r4ds.github.io/bookclubcron/
BugReports: https://github.com/r4ds/bookclubcron/issues
Suggests:
covr,
testthat (>= 3.0.0)
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Imports:
cli,
dplyr,
fs,
glue,
googlesheets4,
httr2,
lubridate,
memoise,
purrr,
rappdirs,
rlang,
Expand All @@ -33,13 +28,21 @@ Imports:
slackthreads,
stringr,
tibble,
tidyselect,
withr,
youtubeR,
zoomer
Suggests:
covr,
testthat (>= 3.0.0)
Remotes:
jonthegeek/zoomer,
kevin-m-kent/youtubeR,
yonicd/slackcalls,
yonicd/slackposts,
yonicd/slackteams,
yonicd/slackthreads,
kevin-m-kent/youtubeR,
jonthegeek/zoomer
yonicd/slackthreads
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
96 changes: 96 additions & 0 deletions R/spreadsheets.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
process_announcements <- function() {
now_utc <- lubridate::now(tzone = "UTC")
tomorrow_utc <- lubridate::date(now_utc + lubridate::days(1))
tomorrow_wday_utc <- tomorrow_utc |>
lubridate::wday(label = TRUE, abbr = FALSE) |>
as.character()
now_hour_utc <- lubridate::hour(now_utc)

clubs_tomorrow <- .active_clubs() |>
dplyr::filter(
.data$day_utc == .data$tomorrow_wday_utc,
.data$hour_utc == .data$now_hour_utc
)

clubs_tomorrow |>
dplyr::bind_cols(
purrr::map(
clubs_tomorrow$cohort_id,
.cohort_meeting_details,
meeting_date = tomorrow_utc
) |>
purrr::list_rbind()
)
stop("Sort out a function for this. Post to the appropriate Slack channel. Watch for 'SKIP', etc.")
}

.active_clubs <- function() {
.gs4_auth()
return(
purrr::quietly(googlesheets4::read_sheet)(
"1G5KjY77ONuaHj530ttzrhCS9WN4_muYxfLgP3xK24Cc",
sheet = "Club Metadata",
col_types = "c"
)$result |>
dplyr::filter(
!is.na(.data$start_date) &
.data$start_date != "TBD" &
is.na(.data$end_date)
) |>
dplyr::filter(
lubridate::as_datetime(
.data$start_date,
tz = "America/Chicago"
) < lubridate::now()
) |>
dplyr::select(
"cohort_id",
"facilitator_id",
"day_utc",
"hour_utc",
"signup_ws_id",
"book_abbrev",
"language",
"book_title",
"book_authors",
"book_url"
)
)
}

.cohort_signups <- function(cohort_id) {
.gs4_auth()

signup_ws_id <- .active_clubs() |>
dplyr::filter(.data$cohort_id == .env$cohort_id) |>
dplyr::pull(.data$signup_ws_id)

return(
purrr::quietly(googlesheets4::read_sheet)(
signup_ws_id,
col_types = "c"
)$result |>
dplyr::select(
tidyselect::any_of(c(
"topic", "chapter", "date", "presenter"
))
) |>
dplyr::mutate(
date = lubridate::as_date(date)
)
)
}

.cohort_meeting_details <- function(cohort_id, meeting_date) {
return(
.cohort_signups(cohort_id) |>
dplyr::filter(date == meeting_date)
)
}

.gs4_auth <- function() {
purrr::quietly(googlesheets4::gs4_auth)(
email = "[email protected]"
)
}

3 changes: 1 addition & 2 deletions R/youtube.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ r4ds_youtube_playlists <- function(n = 50L, refresh = FALSE) {
#' @return A character vector of playlist IDs, with titles as names.
#' @keywords internal
.fetch_r4ds_youtube_playlists <- function(n) {
# TODO: Once this is implemented in youtubeR, replace this with a call to that
# function.
# TODO: {youtubeR} endpoint
raw_playlists <- youtubeR::yt_call_api(
endpoint = "playlists",
query = list(
Expand Down
66 changes: 37 additions & 29 deletions R/zoom.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ process_zoom <- function() {
httr2::resp_body_json()

if (length(all_recordings$meetings)) {
n <- length(r4ds_active_clubs())
youtube_playlists <- r4ds_youtube_playlists(n)
# active_clubs <- .active_clubs()
# n <- length(active_clubs$cohort_id)

# It doesn't hurt to get extra, but it'd be nice to figure out exactly where
# this list *starts*. As of 2023-05-20, the first playlist returned is
# rpkgs06, which hasn't gotten a new video since 2023-04-22; many newer
# lists didn't make the cut.

# youtube_playlists <- r4ds_youtube_playlists(n + 5L) # Pad for new clubs.
youtube_playlists <- r4ds_youtube_playlists(100L)
slack_channels <- r4ds_slack_channels()

working_video_dir <- fs::path(
Expand Down Expand Up @@ -48,9 +56,9 @@ process_zoom <- function() {
FALSE, length(this_meeting$recording_files)
)

# TODO: cohort_info <- .parse_cohort_info(this_meeting$topic,
# meeting_date)
cohort_id <- this_meeting$topic
# TODO: meeting_details <- .cohort_meeting_details(cohort_id,
# meeting_date)

if (cohort_id == "do4ds01-proj01") {
meeting_date_day <- lubridate::day(meeting_date)
Expand Down Expand Up @@ -122,20 +130,20 @@ process_zoom <- function() {
}

.process_zoom_video(
mp4_files[[1]],
cohort_number,
cohort_id,
book_abbrev,
meeting_start_time,
meeting_date,
meeting_num,
"video",
youtube_playlists,
channel_name,
working_video_dir,
working_video_path,
start_time,
end_time
video_recording_file = mp4_files[[1]],
cohort_number = cohort_number,
cohort_id = cohort_id,
book_abbrev = book_abbrev,
meeting_start_time = meeting_start_time,
meeting_date = meeting_date,
meeting_num = meeting_num,
file_identifier = "video",
youtube_playlists = youtube_playlists,
channel_name = channel_name,
working_video_dir = working_video_dir,
working_video_path = working_video_path,
start_time = start_time,
end_time = end_time
)
this_meeting$ready_to_delete <- TRUE
# TODO: Log that it's edited already.
Expand All @@ -147,18 +155,18 @@ process_zoom <- function() {
switch(
this_file$file_type,
MP4 = .process_zoom_video(
this_file,
cohort_number,
cohort_id,
book_abbrev,
meeting_start_time,
meeting_date,
meeting_num,
video_recording_file = this_file,
cohort_number = cohort_number,
cohort_id = cohort_id,
book_abbrev = book_abbrev,
meeting_start_time = meeting_start_time,
meeting_date = meeting_date,
meeting_num = meeting_num,
file_identifier = stringr::str_pad(file_num, 2, pad = "0"),
youtube_playlists,
channel_name,
working_video_dir,
working_video_path
youtube_playlists = youtube_playlists,
channel_name = channel_name,
working_video_dir = working_video_dir,
working_video_path = working_video_path
),
CHAT = .process_zoom_chat(
this_file,
Expand Down
3 changes: 3 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
.onLoad <- function(lib, pkg) {
# Memoise functions that shouldn't change within a session.
.active_clubs <<- memoise::memoise(.active_clubs) # nocov

# Authenticate so the user can walk away.
# youtubeR::yt_authenticate()

Expand Down
4 changes: 3 additions & 1 deletion inst/runners/clubs.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
library(bookclubcron)

process_youtube()
# token <- youtubeR::yt_authenticate(force = TRUE)
# keyring::key_set_with_value("youtube-refresh", password = token$refresh_token)
process_zoom()
process_youtube()
22 changes: 22 additions & 0 deletions inst/setup_cron.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
library(taskscheduleR)
clubs_script <- system.file("runners", "clubs.R", package = "bookclubcron")

taskscheduler_create(
taskname = "r4ds_clubs",
rscript = clubs_script,
schedule = "HOURLY",
# schedule = "ONCE",
starttime = "15:30",
startdate = format(Sys.Date(), "%m/%d/%Y")
)
# taskscheduler_delete(taskname = "r4ds_clubs")


tasks <- taskscheduler_ls() |>
tibble::as_tibble()

tasks |>
dplyr::filter(TaskName == "r4ds_clubs") |>
dplyr::glimpse() |>
dplyr::pull("Task To Run")

0 comments on commit 43c0476

Please sign in to comment.