2018-02-20 20:57:42 +00:00
|
|
|
use librespot::playback::player::PlayerEvent;
|
2020-03-12 12:01:45 +00:00
|
|
|
use librespot_playback::player::TrackMetaData;
|
2020-01-17 17:11:07 +00:00
|
|
|
use log::info;
|
2018-02-26 01:50:41 +00:00
|
|
|
use std::collections::HashMap;
|
2018-10-16 07:24:33 +00:00
|
|
|
use std::io;
|
2018-10-16 09:32:17 +00:00
|
|
|
use std::process::Command;
|
2019-10-08 09:31:18 +00:00
|
|
|
use tokio_process::{Child, CommandExt};
|
2018-02-15 23:16:38 +00:00
|
|
|
|
2018-10-16 07:24:33 +00:00
|
|
|
fn run_program(program: &str, env_vars: HashMap<&str, String>) -> io::Result<Child> {
|
2018-02-15 23:16:38 +00:00
|
|
|
let mut v: Vec<&str> = program.split_whitespace().collect();
|
2018-02-20 22:12:52 +00:00
|
|
|
info!("Running {:?} with environment variables {:?}", v, env_vars);
|
|
|
|
Command::new(&v.remove(0))
|
2018-02-15 23:16:38 +00:00
|
|
|
.args(&v)
|
2018-02-20 21:45:14 +00:00
|
|
|
.envs(env_vars.iter())
|
2018-10-16 09:32:17 +00:00
|
|
|
.spawn_async()
|
2018-02-15 23:16:38 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 12:01:45 +00:00
|
|
|
fn add_meta_data_to_env_vars(env_vars: &mut HashMap<&str, String>, track_meta_data: TrackMetaData) {
|
|
|
|
env_vars.insert("TRACK_ID", track_meta_data.track_id.to_base62());
|
|
|
|
env_vars.insert("DURATION_MS", track_meta_data.duration_ms.to_string());
|
2020-03-12 12:29:24 +00:00
|
|
|
env_vars.insert("TRACK_TITLE", track_meta_data.title);
|
2020-03-12 12:01:45 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 07:58:44 +00:00
|
|
|
pub fn run_program_on_events(event: PlayerEvent, onevent: &str) -> Option<io::Result<Child>> {
|
2018-02-20 22:09:48 +00:00
|
|
|
let mut env_vars = HashMap::new();
|
|
|
|
match event {
|
2018-02-26 01:50:41 +00:00
|
|
|
PlayerEvent::Changed {
|
|
|
|
old_track_id,
|
|
|
|
new_track_id,
|
|
|
|
} => {
|
2018-02-20 22:09:48 +00:00
|
|
|
env_vars.insert("PLAYER_EVENT", "change".to_string());
|
2018-11-04 17:41:33 +00:00
|
|
|
env_vars.insert("OLD_TRACK_ID", old_track_id.to_base62());
|
|
|
|
env_vars.insert("TRACK_ID", new_track_id.to_base62());
|
2018-02-26 01:50:41 +00:00
|
|
|
}
|
2020-01-31 21:41:11 +00:00
|
|
|
PlayerEvent::Started { track_id, .. } => {
|
2018-02-20 22:09:48 +00:00
|
|
|
env_vars.insert("PLAYER_EVENT", "start".to_string());
|
2018-11-04 17:41:33 +00:00
|
|
|
env_vars.insert("TRACK_ID", track_id.to_base62());
|
2018-02-20 21:45:14 +00:00
|
|
|
}
|
2020-01-31 21:41:11 +00:00
|
|
|
PlayerEvent::Stopped { track_id, .. } => {
|
2018-02-20 22:09:48 +00:00
|
|
|
env_vars.insert("PLAYER_EVENT", "stop".to_string());
|
2018-11-04 17:41:33 +00:00
|
|
|
env_vars.insert("TRACK_ID", track_id.to_base62());
|
2018-02-20 22:09:48 +00:00
|
|
|
}
|
2020-03-12 12:01:45 +00:00
|
|
|
PlayerEvent::Playing {
|
|
|
|
track_meta_data,
|
|
|
|
position_ms,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
env_vars.insert("PLAYER_EVENT", "playing".to_string());
|
|
|
|
add_meta_data_to_env_vars(&mut env_vars, track_meta_data);
|
|
|
|
env_vars.insert("POSITION_MS", position_ms.to_string());
|
|
|
|
}
|
|
|
|
PlayerEvent::Paused {
|
|
|
|
track_meta_data,
|
|
|
|
position_ms,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
env_vars.insert("PLAYER_EVENT", "paused".to_string());
|
|
|
|
add_meta_data_to_env_vars(&mut env_vars, track_meta_data);
|
|
|
|
env_vars.insert("POSITION_MS", position_ms.to_string());
|
|
|
|
}
|
|
|
|
PlayerEvent::VolumeSet { volume } => {
|
|
|
|
env_vars.insert("PLAYER_EVENT", "volume_set".to_string());
|
|
|
|
env_vars.insert("VOLUME", volume.to_string());
|
|
|
|
}
|
2020-02-03 07:58:44 +00:00
|
|
|
_ => return None,
|
2018-02-20 22:09:48 +00:00
|
|
|
}
|
2020-02-03 07:58:44 +00:00
|
|
|
Some(run_program(onevent, env_vars))
|
2018-02-15 23:16:38 +00:00
|
|
|
}
|