0.1.2: fix track picking

Signed-off-by: Ivan Bushchik <ivabus@ivabus.dev>
This commit is contained in:
Ivan Bushchik 2024-01-29 08:45:22 +03:00
parent d32aceb82c
commit 5bfe7364c2
No known key found for this signature in database
GPG key ID: 2F16FBF3262E090C
3 changed files with 18 additions and 6 deletions

2
Cargo.lock generated
View file

@ -320,7 +320,7 @@ checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "lonelyradio"
version = "0.1.1"
version = "0.1.2"
dependencies = [
"chrono",
"clap",

View file

@ -1,7 +1,7 @@
[package]
name = "lonelyradio"
description = "TCP radio for singles"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
license = "MIT"
authors = [ "Ivan Bushchik <ivabus@ivabus.dev>" ]

View file

@ -33,6 +33,21 @@ fn is_not_hidden(entry: &DirEntry) -> bool {
entry.file_name().to_str().map(|s| entry.depth() == 0 || !s.starts_with('.')).unwrap_or(false)
}
// Recursively finding music file
fn pick_track(tracklist: &Vec<PathBuf>) -> &PathBuf {
let mut track = tracklist.choose(&mut thread_rng()).unwrap();
while !track.metadata().unwrap().is_file() {
track = pick_track(tracklist)
}
// Skipping "images" (covers)
while "jpgjpegpngwebp"
.contains(&track.extension().unwrap().to_str().unwrap().to_ascii_lowercase())
{
track = pick_track(tracklist)
}
track
}
async fn stream(mut s: TcpStream) {
let tracklist = walkdir::WalkDir::new(Args::parse().dir)
.into_iter()
@ -41,10 +56,7 @@ async fn stream(mut s: TcpStream) {
.map(|x| x.into_path())
.collect::<Vec<PathBuf>>();
'track: loop {
let mut track = tracklist.choose(&mut thread_rng()).unwrap();
while !track.metadata().unwrap().is_file() {
track = tracklist.choose(&mut thread_rng()).unwrap()
}
let track = pick_track(&tracklist);
println!(
"[{}] {} to {}",
Local::now().to_rfc3339(),