#' setup del progetto: split file e setup avanzamento
#'
#' @param prj name of the project
#' @param yt_id youtube id
#' @param chunks_len_mins length in minutes of each chunk
#'
#' @export
setup_project <- function(prj, yt_id, chunks_len_mins){
chunks <- split_source(prj = prj,
yt_id = yt_id,
chunks_len_mins = chunks_len_mins)
files <- unlist(lapply(chunks, function(x) x$fn))
setup_avanzamento(prj = prj, trn_f = files)
}
setup_avanzamento <- function(prj, trn_f){
prj_dir <- subs_dir(prj)
av_f <- avanz_f(prj)
avanz <- data.frame(trn_filename = trn_f,
trn_assignee = "",
trn_assigned = FALSE,
trn_completed = FALSE,
rev1_assigned = FALSE,
rev1_completed = FALSE,
rev2_filename = "",
rev2_assignee = "",
rev2_assigned = FALSE,
rev2_completed = FALSE)
avanz_export(x = avanz, f = av_f)
}
split_source <- function(prj, yt_id, chunks_len_mins){
source_srt <- sprintf('%s/%s.srt', source_dir, prj)
output_dir <- sprintf('%s/%s', subs_dir, prj)
if (!file.exists(source_srt)) stop("File ", source_srt, " mancante.")
srt <- read_srt(source_srt)
chunks <- srt_chunks_maker(srt = srt,
chunks_len_mins = chunks_len_mins,
yt_id = yt_id)
tmp <- lapply(chunks,
srt_chunk_printer,
con_des = 'file',
output_dir = output_dir)
## have them sorted by my ls
system(sprintf("touch %s/*", output_dir))
invisible(chunks)
}
header_template <- "## --------------------------------------------------------
## Inizio spezzone: %s
## --------------------------------------------------------\n"
srt_chunks_maker <-
function(srt = NULL,
chunks_len_mins = 5L,
yt_id = "")
{
if (yt_id == '') stop("You must give the YouTube id of the video")
## generazione dei secondi dei chunk
chunks_len_secs <- chunks_len_mins * 60L
max_secs <-
floor(max(unlist(lapply(srt, function(x) x$start_secs)))) + 1L
chunks_start_secs <- seq(0L, max_secs, by = chunks_len_secs)
chunks_id <- seq_along(chunks_start_secs)
chunks_digits <- secs_to_digits(chunks_start_secs)
chunks_fname <- sprintf("subs_%s.srt", chunks_digits)
## add chunk number
srt <- lapply(srt, function(x) {
x$chunk <- max(which(x$start_secs >= chunks_start_secs))
x
})
## altri dati del singolo chunk
yt_links <- sprintf("https://youtu.be/%s?t=%d",
yt_id,
chunks_start_secs)
chunks_headers <- sprintf(header_template, yt_links)
## lista che splitta i subs in base al chunk
srt_sel <- lapply(chunks_id, function(id){
Filter(function(x) x$chunk %in% id, srt)
})
## lista dati chunks
chunk_worker <- function(dig, fn, id, yt, subs){
list("dig" = dig, "fn" = fn, "id" = id, "yt" = yt, "subs" = subs)
}
res <- Map(chunk_worker,
as.list(chunks_digits),
as.list(chunks_fname),
as.list(chunks_id),
as.list(chunks_headers),
srt_sel)
res
}
## printer per esportazione del singolo chunk
srt_chunk_printer <- function(chunk,
output_dir = ".",
con_des = c('stdout', 'file'))
{
## creazione della connessione
con_des <- match.arg(con_des)
if (con_des == 'stdout') {
con <- stdout()
} else if (con_des == 'file') {
## add subs to sec to easy identify the splitting produced files
con <- file(paste(output_dir, chunk$fn, sep = '/'), open = 'w')
}
## stampa del chunk
## header: indirizzo youtube
writeLines(chunk$yt, con)
writeLines(c("", ""), con)
## stampa dei singoli subs del chunk
av_sub_printer <- function(s, my_con = con) {
lines <- c(s$id,
s$times,
## inglese commentato
sprintf("## %s", s$text),
## linee bianche, meglio abbondare che deficere
rep("", 4))
writeLines(lines, con = my_con)
}
lapply(chunk$subs, av_sub_printer)
## chiusura della connessione
if (con_des == 'file') close(con)
invisible(NULL)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.