diff --git a/src/utils/json_reader.rs b/src/utils/json_reader.rs index 616d3714..4d87f07d 100644 --- a/src/utils/json_reader.rs +++ b/src/utils/json_reader.rs @@ -3,36 +3,41 @@ use std::fs::File; use std::path::Path; use crate::utils::Config; -use crate::utils::{get_date, modified_time}; +use crate::utils::{get_date, get_sec, modified_time}; #[derive(Debug, Serialize, Deserialize)] pub struct Playlist { pub date: String, + pub start_index: Option, pub modified: Option, pub program: Vec, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct Program { + pub begin: Option, + pub index: Option, #[serde(rename = "in")] - pub seek: f32, - pub out: f32, - pub duration: f32, + pub seek: f64, + pub out: f64, + pub duration: f64, pub category: String, pub source: String, + pub cmd: Option>, + pub filter: Option>, } -pub fn read_json(config: &Config) -> Playlist { +pub fn read_json(config: &Config, seek: bool) -> Playlist { let mut playlist_path = Path::new(&config.playlist.path).to_owned(); let start = &config.playlist.day_start; + let t: Vec<&str> = start.split(':').collect(); + let h: f64 = t[0].parse().unwrap(); + let m: f64 = t[1].parse().unwrap(); + let s: f64 = t[2].parse().unwrap(); + let mut start_sec = h * 3600.0 + m * 60.0 + s; + let mut seek_first = seek; if playlist_path.is_dir() { - let t: Vec<&str> = start.split(':').collect(); - let h: f64 = t[0].parse().unwrap(); - let m: f64 = t[1].parse().unwrap(); - let s: f64 = t[2].parse().unwrap(); - let start_sec = h * 3600.0 + m * 60.0 + s; - let date = get_date(true, start_sec, 0.0); let d: Vec<&str> = date.split('-').collect(); playlist_path = playlist_path @@ -56,5 +61,36 @@ pub fn read_json(config: &Config) -> Playlist { playlist.modified = Some(modify.unwrap().to_string()); } + let time_sec = get_sec(); + + for (i, item) in playlist.program.iter_mut().enumerate() { + item.begin = Some(start_sec); + item.index = Some(i); + + if seek_first && item.begin.unwrap() + (item.out - item.seek) > time_sec { + seek_first = false; + playlist.start_index = Some(i); + item.seek = time_sec - item.begin.unwrap(); + } + + if item.seek > 0.0 { + item.cmd = Some(vec![ + "-ss".to_string(), + format!("{}", item.seek).to_string(), + "-i".to_string(), + item.source.clone(), + ]) + } else { + item.cmd = Some(vec![ + "-i".to_string(), + item.source.clone(), + ]) + } + + start_sec += item.out - item.seek; + } + + println!("{:#?}", playlist); + playlist } diff --git a/src/utils/playlist.rs b/src/utils/playlist.rs index 5cb524e2..f62de67c 100644 --- a/src/utils/playlist.rs +++ b/src/utils/playlist.rs @@ -11,10 +11,11 @@ pub struct CurrentProgram { impl CurrentProgram { fn new() -> Self { let config = get_config(); - let program: Vec = read_json(&config).program; + let json = read_json(&config, true); + let program: Vec = json.program; Self { nodes: program, - idx: 0, + idx: json.start_index.unwrap(), } } } @@ -31,7 +32,7 @@ impl Iterator for CurrentProgram { } else { // play first item from next playlist let config = get_config(); - let program: Vec = read_json(&config).program; + let program: Vec = read_json(&config, false).program; self.nodes = program; self.idx = 1;