This commit is contained in:
Julien Rabier 2022-01-02 16:17:26 +01:00
parent 9ee6533135
commit 9150dc5bcd
2 changed files with 72 additions and 32 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "mpdyn"
version = "0.1.0"
version = "0.1.1"
authors = ["Julien Rabier <taziden@flexiden.org>"]
edition = "2021"
@ -8,5 +8,5 @@ edition = "2021"
[dependencies]
mpdrs = "0.1.0"
chrono = "0.4.13"
chrono = "0.4.19"
rand = "0.8.4"

View File

@ -1,56 +1,94 @@
use chrono::Local;
//use chrono::Local;
use mpdrs::lsinfo::LsInfoResponse;
use mpdrs::{Client, Song};
use rand::prelude::*;
use std::{thread, time};
use std::{error::Error, thread, time};
fn main() {
fn main() -> Result<(), Box<dyn Error>> {
// We enable consume mode here
let _ = Client::default().consume(true);
Client::default().consume(true)?;
// We launch an infinite loop
loop {
let mut c = Client::default();
println!("{} checking playlist ", Local::now().format("%T"));
let s = match c.status() {
Ok(status) => status,
Err(_) => panic!("blah"),
};
let ql = s.queue_len;
//println!("{} checking playlist ", Local::now().format("%T"));
let ql = Client::default().status()?.queue_len;
if ql < 10u32 {
populate_playlist(10u32 - ql);
populate_playlist(10u32 - ql)?;
};
let refresh_delay = time::Duration::from_secs(10);
let refresh_delay = time::Duration::from_secs(3);
thread::sleep(refresh_delay);
let _ = c.close();
}
}
fn populate_playlist(a: u32) {
fn populate_playlist(a: u32) -> Result<(), Box<dyn Error>> {
if a == 1 {
println!("need to add one song");
} else {
println!("need to add {} songs", a);
}
let mut x: u32 = 0;
while x < a {
add_track();
x += 1;
let remaining = a - x;
match remaining {
0 => println!("playlist refilled!"),
1 => println!("need to add 1 more song"),
_ => println!("need to add {} more songs", remaining),
for song in get_random_tracks(a)? {
match Client::default().push(&song.file) {
Ok(_) => println!(
"adding: {:} by {:}",
song.title.unwrap(),
song.artist.unwrap()
),
Err(_) => println!("unable to add {:?}", song.title),
}
}
Ok(())
}
fn get_random_tracks(n: u32) -> Result<Vec<Song>, Box<dyn Error>> {
let mut rng = rand::thread_rng();
let query_albums = Client::default().lsinfo("/")?;
let albums: Vec<&str> = query_albums
.choose_multiple(&mut rng, n.try_into()?)
.filter(|&x| match_directory(x).is_ok())
.map(|x| find_path(x))
.collect();
let tracks: Vec<Song> = albums
.into_iter()
.filter_map(|x| Some(get_random_track(x)).unwrap())
.collect();
return Ok(tracks);
}
fn match_directory(response: &LsInfoResponse) -> Result<i32, i32> {
match response {
LsInfoResponse::Directory {
path: _,
metadata: _,
} => Ok(1),
_ => Err(0),
}
}
fn get_random_track(path: &str) -> Option<Song> {
let mut rng = rand::thread_rng();
match Client::default().lsinfo(path) {
Ok(songs) => match songs.choose(&mut rng) {
Some(LsInfoResponse::Song(song)) => Some(song.clone()),
_ => None,
},
Err(_) => None,
}
}
fn find_path(response: &LsInfoResponse) -> &str {
match response {
LsInfoResponse::Directory { path, metadata: _ } => path,
_ => "",
}
}
/*
fn add_track() {
let mut c = Client::default();
@ -63,7 +101,8 @@ fn add_track() {
println!("could not add track")
}
}
*/
/*
fn get_random_track() -> Option<Song> {
let mut c = Client::default();
let mut rng = rand::thread_rng();
@ -84,3 +123,4 @@ fn get_random_track() -> Option<Song> {
Err(_) => None,
}
}
*/