rewrite get_random_track

This commit is contained in:
Julien Rabier 2021-12-30 11:43:18 +01:00
parent 606ec5b90b
commit 9ee6533135
3 changed files with 32 additions and 209 deletions

177
Cargo.lock generated
View File

@ -1,177 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "autocfg"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
[[package]]
name = "bufstream"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40e38929add23cdf8a366df9b0e088953150724bcbe5fc330b0d8eb3b328eec8"
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "chrono"
version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c74d84029116787153e02106bf53e66828452a4b325cc8652b788b5967c0a0b6"
dependencies = [
"num-integer",
"num-traits",
"time",
]
[[package]]
name = "getrandom"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.72"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9f8082297d534141b30c8d39e9b1773713ab50fdbe4ff30f750d063b3bfd701"
[[package]]
name = "mpd"
version = "0.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57a20784da57fa01bf7910a5da686d9f39ff37feaa774856b71f050e4331bf82"
dependencies = [
"bufstream",
"rustc-serialize",
"time",
]
[[package]]
name = "mpdyn"
version = "0.1.0"
dependencies = [
"chrono",
"mpd",
"rand",
]
[[package]]
name = "num-integer"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611"
dependencies = [
"autocfg",
]
[[package]]
name = "ppv-lite86"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea"
[[package]]
name = "rand"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [
"getrandom",
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
name = "rand_chacha"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core",
]
[[package]]
name = "rustc-serialize"
version = "0.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda"
[[package]]
name = "time"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438"
dependencies = [
"libc",
"winapi",
]
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View File

@ -2,11 +2,11 @@
name = "mpdyn" name = "mpdyn"
version = "0.1.0" version = "0.1.0"
authors = ["Julien Rabier <taziden@flexiden.org>"] authors = ["Julien Rabier <taziden@flexiden.org>"]
edition = "2018" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
mpd = "0.0.12" mpdrs = "0.1.0"
chrono = "0.4.13" chrono = "0.4.13"
rand = "0.7.3" rand = "0.8.4"

View File

@ -1,5 +1,6 @@
use chrono::Local; use chrono::Local;
use mpd::{Client, Query, Song}; use mpdrs::lsinfo::LsInfoResponse;
use mpdrs::{Client, Song};
use rand::prelude::*; use rand::prelude::*;
use std::{thread, time}; use std::{thread, time};
@ -23,7 +24,7 @@ fn main() {
populate_playlist(10u32 - ql); populate_playlist(10u32 - ql);
}; };
let refresh_delay = time::Duration::from_secs(20); let refresh_delay = time::Duration::from_secs(10);
thread::sleep(refresh_delay); thread::sleep(refresh_delay);
let _ = c.close(); let _ = c.close();
@ -53,34 +54,33 @@ fn populate_playlist(a: u32) {
fn add_track() { fn add_track() {
let mut c = Client::default(); let mut c = Client::default();
let res: Song = get_random_track(); if let Some(res) = get_random_track() {
println!("adding: {:?}", res.file); println!("adding: {:?}", res.file);
let _ = c.push(res); let _ = c.push(&res.file);
let _ = c.close(); let _ = c.close();
}
fn get_random_track() -> Song {
let mut c = Client::default();
let alphavec: Vec<char> = "abcdefghijklmnopqrstuvwxyz".chars().collect();
let mut rng = rand::thread_rng();
let chars: String = alphavec.choose_multiple(&mut rng, 2).cloned().collect();
let mut query = Query::new();
let query = query.and(mpd::Term::Any, chars);
let songs = c.search(query, None).unwrap_or_default();
if songs.is_empty() {
return get_random_track();
} else { } else {
println!("could not add track")
}
}
fn get_random_track() -> Option<Song> {
let mut c = Client::default();
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let track = match songs.choose(&mut rng) {
Some(song) => song, match c.lsinfo("/") {
None => panic!("Impossible de choisir un morceau"), Ok(albums) => match albums.choose(&mut rng) {
}; Some(LsInfoResponse::Directory { path, metadata: _ }) => match c.lsinfo(path) {
track.clone() Ok(songs) => match songs.choose(&mut rng) {
Some(LsInfoResponse::Song(song)) => Some(song.clone()),
_ => None,
},
Err(_) => None,
},
Some(LsInfoResponse::Song(_)) => None,
Some(LsInfoResponse::Playlist { .. }) => None,
None => None,
},
Err(_) => None,
} }
} }