extract_vids.R (1860B)
1 chapter_numbers <- 3:25 2 3 purrr::walk(chapter_numbers, \(chapter_number) { 4 chapter_number_wide <- stringr::str_pad(chapter_number, 2, pad = "0") 5 chapter <- readLines(glue::glue("slides/{chapter_number_wide}.Rmd")) 6 vids_start <- stringr::str_which(chapter, "^## Meeting Videos") 7 if (length(vids_start)) { 8 fs::dir_create("videos", chapter_number_wide) 9 txt <- paste(chapter[vids_start:length(chapter)], collapse = "\n") 10 vid_sections <- stringr::str_split_1(txt, "\\n###\\s*") |> 11 stringr::str_subset("^## Meeting Videos", negate = TRUE) 12 if (length(vid_sections)) { 13 purrr::walk(vid_sections, \(vid_section) { 14 section_lines <- stringr::str_split_1(vid_section, "\\n") |> 15 purrr::keep(~ nchar(.x) > 0) 16 cohort <- stringr::str_extract(section_lines[[1]], "\\d+") 17 cohort_wide <- stringr::str_pad(cohort, 2, pad = "0") 18 youtube_codes <- purrr::discard( 19 stringr::str_extract( 20 section_lines, 21 "https://www\\.youtube\\.com/embed/([^\"]+)", 22 1 23 ), 24 is.na 25 ) 26 youtube_embeds <- glue::glue( 27 "{{< video https://www.youtube.com/embed/[youtube_codes] >}}", 28 .open = "[", 29 .close = "]" 30 ) 31 chat_log <- stringr::str_subset( 32 section_lines[-1], 33 "https://www\\.youtube\\.com/embed/", 34 negate = TRUE 35 ) |> 36 paste(collapse = "\n") 37 38 cat( 39 c( 40 "---", 41 glue::glue("title: Cohort {cohort}"), 42 "---", 43 youtube_embeds, 44 chat_log 45 ) |> 46 purrr::keep(~ nchar(.x) > 0), 47 sep = "\n", 48 file = fs::path( 49 "videos", 50 chapter_number_wide, 51 cohort_wide, 52 ext = "qmd" 53 ) 54 ) 55 }) 56 } 57 } 58 })