read playlist

This commit is contained in:
jb-alvarado 2022-02-15 17:27:06 +01:00
parent 117650f757
commit 92899763f8
6 changed files with 148 additions and 19 deletions

29
Cargo.lock generated
View File

@ -95,14 +95,26 @@ version = "0.1.0"
dependencies = [
"chrono",
"clap",
"ffprobe",
"notify",
"rand",
"regex",
"serde",
"serde_json",
"serde_yaml",
"walkdir",
]
[[package]]
name = "ffprobe"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f439d57070a29c93e5bdf9f98b973360aa96b1b8c203793de54482f168fc216a"
dependencies = [
"serde",
"serde_json",
]
[[package]]
name = "filetime"
version = "0.2.15"
@ -221,6 +233,12 @@ dependencies = [
"libc",
]
[[package]]
name = "itoa"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35"
[[package]]
name = "kernel32-sys"
version = "0.2.2"
@ -509,6 +527,17 @@ dependencies = [
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e8d9fa5c3b304765ce1fd9c4c8a3de2c8db365a5b91be52f186efc675681d95"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "serde_yaml"
version = "0.8.23"

View File

@ -8,14 +8,16 @@ edition = "2021"
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8"
serde_json = "1.0"
regex = "1"
chrono = "0.4"
clap = { version = "3.0.14", features = ["derive"] }
notify = "4.0.0"
walkdir = "2"
rand = "0.8.5"
# tokio = { version = "1.16.1", features = ["full"] }
ffprobe = "0.3"
# tokio = { version = "1.16.1", features = ["full"] }
[[bin]]
name = "ffplayout"
path = "src/main.rs"

View File

@ -5,7 +5,7 @@ use rand::thread_rng;
use std::ffi::OsStr;
use std::path::Path;
use std::process;
use std::sync::mpsc::{channel, Receiver};
use std::sync::mpsc::{channel, Receiver, TryRecvError};
use std::time::Duration;
use std::{thread, time};
use walkdir::WalkDir;
@ -21,9 +21,9 @@ impl Source {
for entry in WalkDir::new(path).into_iter().filter_map(|e| e.ok()) {
if entry.path().is_file() {
let ext = file_extension(entry.path()).unwrap().to_lowercase();
let ext = file_extension(entry.path());
if extensions.contains(&ext) {
if ext.is_some() && extensions.contains(&ext.unwrap().to_lowercase()) {
file_list.push(entry.path().display().to_string());
}
}
@ -78,14 +78,18 @@ fn watch_folder(source: &mut Source, receiver: &Receiver<notify::DebouncedEvent>
);
}
_ => (),
}
Err(err) => {
if err == TryRecvError::Disconnected {
println!("Folder watch error: {:?}", err)
}
},
Err(_) => (),
}
}
pub fn walk(path: &String, shuffle: bool, extensions: &Vec<String>) {
if !Path::new(path).exists() {
println!("Folder path not exists: '{}'", path);
println!("Folder path not exists: '{path}'");
process::exit(0x0100);
}
let mut source = Source::new(path, extensions);
@ -97,7 +101,7 @@ pub fn walk(path: &String, shuffle: bool, extensions: &Vec<String>) {
loop {
if shuffle {
println!("Shuffle files in folder");
println!("Shuffle files in folder: '{path}'");
source.shuffle();
} else {
source.sort();
@ -105,7 +109,7 @@ pub fn walk(path: &String, shuffle: bool, extensions: &Vec<String>) {
while index < source.files.len() {
watch_folder(&mut source, &receiver);
println!("Play file {}: {:?}", index, source.files[index]);
println!("Play file {index}: {:?}", source.files[index]);
index += 1;
thread::sleep(time::Duration::from_secs(1));

64
src/json.rs Normal file
View File

@ -0,0 +1,64 @@
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::path::Path;
use crate::config::Config;
use crate::utils::{get_date, modified_time};
#[derive(Debug, Serialize, Deserialize)]
pub struct Playlist {
pub date: String,
pub modified: Option<String>,
pub program: Vec<Program>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Program {
#[serde(rename = "in")]
pub seek: f32,
pub out: f32,
pub duration: f32,
pub category: String,
pub source: String,
}
pub fn read(config: &Config) -> Playlist {
let mut playlist_path = Path::new(&config.playlist.path).to_owned();
let start = &config.playlist.day_start;
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
.join(d[0])
.join(d[1])
.join(date)
.with_extension("json");
}
println!(
"Read Playlist: {}",
playlist_path.as_path().display().to_string()
);
let modify = modified_time(playlist_path.as_path().display().to_string());
println!("{:?}", modify);
let f = File::open(playlist_path).expect("Could not open json playlist file.");
let mut playlist: Playlist =
serde_json::from_reader(f).expect("Could not read json playlist file.");
if modify.is_some() {
playlist.modified = Some(modify.unwrap().to_string());
}
println!("{:#?}", playlist);
playlist
}

View File

@ -1,21 +1,24 @@
mod arg_parse;
mod config;
mod utils;
mod folder;
// mod folder;
mod json;
fn main() {
let config = config::get_config();
// println!("{:#?}", config);
folder::walk(&config.storage.path, config.storage.shuffle, &config.storage.extensions);
// folder::walk(&config.storage.path, config.storage.shuffle, &config.storage.extensions);
let args = arg_parse::get_args();
json::read(&config);
println!("{:#?}", args);
println!("{:#?}", args.config.is_some());
// println!("{:#?}", args.config.unwrap());
//println!("{:?}", config.general.stop_threshold);
// let args = arg_parse::get_args();
println!("{:#?}", utils::get_sec());
println!("{:#?}", utils::get_timestamp());
// println!("{:#?}", args);
// println!("{:#?}", args.config.is_some());
// // println!("{:#?}", args.config.unwrap());
// //println!("{:?}", config.general.stop_threshold);
// println!("{:#?}", utils::get_sec());
// println!("{:#?}", utils::get_timestamp());
}

View File

@ -1,4 +1,6 @@
use chrono::prelude::*;
use chrono::Duration;
use std::fs::metadata;
pub fn get_sec() -> f64 {
let local: DateTime<Local> = Local::now();
@ -7,8 +9,33 @@ pub fn get_sec() -> f64 {
) as f64 + (local.nanosecond() as f64 / 1000000000.0)
}
pub fn get_timestamp() -> i64 {
// pub fn get_timestamp() -> i64 {
// let local: DateTime<Local> = Local::now();
// local.timestamp_millis() as i64
// }
pub fn get_date(seek: bool, start: f64, next: f64) -> String {
let local: DateTime<Local> = Local::now();
local.timestamp_millis() as i64
if seek && start > get_sec() {
return (local - Duration::days(1)).format("%Y-%m-%d").to_string()
}
if start == 0.0 && next >= 86400.0 {
return (local + Duration::days(1)).format("%Y-%m-%d").to_string()
}
local.format("%Y-%m-%d").to_string()
}
pub fn modified_time(path: String) -> Option<DateTime<Local>> {
let metadata = metadata(path).unwrap();
if let Ok(time) = metadata.modified() {
let date_time: DateTime<Local> = time.into();
return Some(date_time)
}
None
}