commit 406a3f283dfafb37f5a9d3b92e80ad3370fc8b99
parent 7d74153be22a27f0e612f6afd5796686f5372f25
Author: Alex Balgavy <alex@balgavy.eu>
Date: Thu, 9 Jun 2022 17:31:03 +0200
Some refactoring
Diffstat:
9 files changed, 121 insertions(+), 121 deletions(-)
diff --git a/src/config.rs b/src/config.rs
@@ -1,8 +1,25 @@
-use crate::tunein::*;
use crate::*;
+use radio::*;
use serde::Deserialize;
use std::fs;
-use std::process::Command;
+
+#[derive(Deserialize, Clone, Copy)]
+pub enum PlaybackOptions {
+ MPC,
+ MPV,
+}
+
+#[derive(Deserialize, Clone, Copy, Debug)]
+pub enum MpcLoadOptions {
+ ADD,
+ LOAD,
+}
+
+#[derive(Clone, Copy)]
+pub struct RadioOptions {
+ pub method: PlaybackOptions,
+ pub load_options: Option<MpcLoadOptions>,
+}
#[derive(Deserialize)]
pub struct ConfigData {
@@ -11,70 +28,10 @@ pub struct ConfigData {
#[derive(Deserialize)]
pub struct Config {
- pub radios: Vec<Item>,
+ pub radios: Vec<Radio>,
pub player: PlaybackOptions,
}
-#[derive(Deserialize, Debug)]
-pub struct Item {
- pub name: String,
- pub description: Option<String>,
- pub website: Option<String>,
- #[serde(alias = "sound")]
- pub url: Option<String>,
- pub mpc_load_option: Option<MpcLoadOptions>,
- pub radios: Option<Vec<Item>>,
-}
-
-impl TuneIn for Item {
- fn play(&self, options: &RadioOptions) {
- let stream = self.url.as_ref().unwrap();
- println!("Playing {}", stream);
- match options {
- RadioOptions {
- method: PlaybackOptions::MPC,
- load_options: Some(l),
- } => {
- Command::new("mpc")
- .arg("clear")
- .stdout(std::process::Stdio::null())
- .spawn()
- .expect("MPC couldn't clear");
- match l {
- MpcLoadOptions::ADD => {
- Command::new("mpc")
- .args(&["add", stream])
- .spawn()
- .expect("MPC couldn't add stream");
- }
- MpcLoadOptions::LOAD => {
- Command::new("mpc")
- .args(&["load", stream])
- .spawn()
- .expect("MPC couldn't load stream");
- }
- }
- Command::new("mpc")
- .arg("play")
- .stdout(std::process::Stdio::null())
- .spawn()
- .expect("MPC couldn't play");
- }
- RadioOptions {
- method: PlaybackOptions::MPV,
- load_options: None,
- } => {
- let mut child = Command::new("mpv")
- .args(&[stream, "--vid=no", "--volume=50"])
- .spawn()
- .expect("MPV couldn't play");
- let _ = child.wait();
- }
- _ => panic!("Wrong combination of playback options."),
- }
- }
-}
-
pub fn read_config() -> ConfigData {
let filename = "config.toml";
let contents = match fs::read_to_string(filename) {
diff --git a/src/main.rs b/src/main.rs
@@ -1,16 +1,16 @@
mod config;
mod radio;
+mod radios;
mod screen;
-mod tunein;
-use config::*;
use radio::*;
+use crate::config::*;
+use radios::*;
use std::process::exit;
-use tunein::*;
/* TODO:
* - optionally allow playing video
* - implement additional radios from Ruby scripts
*/
-fn choose_radio(radios: &Vec<Item>) -> &Item {
+fn choose_radio(radios: &Vec<Radio>) -> &Radio {
loop {
let choices: Vec<String> = radios
.iter()
@@ -49,7 +49,7 @@ fn main() {
let mut data = config::read_config();
let top_radios = &mut data.config.radios;
- top_radios.push(sounds_of_earth::new());
+ top_radios.push(sounds_of_earth::new()); // this new should be async, it calls API
let chosen_radio = choose_radio(top_radios);
let opts = RadioOptions {
method: data.config.player,
diff --git a/src/radio.rs b/src/radio.rs
@@ -0,0 +1,63 @@
+use serde::Deserialize;
+use std::process::Command;
+use crate::config::*;
+
+#[derive(Deserialize, Debug)]
+pub struct Radio {
+ pub name: String,
+ pub description: Option<String>,
+ pub website: Option<String>,
+ #[serde(alias = "sound")]
+ pub url: Option<String>,
+ pub mpc_load_option: Option<MpcLoadOptions>,
+ pub radios: Option<Vec<Radio>>,
+}
+
+impl Radio {
+ pub fn play(&self, options: &RadioOptions) {
+ let stream = self.url.as_ref().unwrap();
+ println!("Playing {}", stream);
+ match options {
+ RadioOptions {
+ method: PlaybackOptions::MPC,
+ load_options: Some(l),
+ } => {
+ Command::new("mpc")
+ .arg("clear")
+ .stdout(std::process::Stdio::null())
+ .spawn()
+ .expect("MPC couldn't clear");
+ match l {
+ MpcLoadOptions::ADD => {
+ Command::new("mpc")
+ .args(&["add", stream])
+ .spawn()
+ .expect("MPC couldn't add stream");
+ }
+ MpcLoadOptions::LOAD => {
+ Command::new("mpc")
+ .args(&["load", stream])
+ .spawn()
+ .expect("MPC couldn't load stream");
+ }
+ }
+ Command::new("mpc")
+ .arg("play")
+ .stdout(std::process::Stdio::null())
+ .spawn()
+ .expect("MPC couldn't play");
+ }
+ RadioOptions {
+ method: PlaybackOptions::MPV,
+ load_options: None,
+ } => {
+ let mut child = Command::new("mpv")
+ .args(&[stream, "--vid=no", "--volume=50"])
+ .spawn()
+ .expect("MPV couldn't play");
+ let _ = child.wait();
+ }
+ _ => panic!("Wrong combination of playback options."),
+ }
+ }
+}
diff --git a/src/radio/sounds_of_earth.rs b/src/radio/sounds_of_earth.rs
@@ -1,33 +0,0 @@
-use crate::config::*;
-use crate::tunein::*;
-use serde_json;
-use ureq;
-fn retrieve_channels() -> Result<Vec<Item>, ureq::Error> {
- let uri = "https://soundsofearth.eco/regions.json";
- let resp: ureq::Response = ureq::get(uri).call()?;
- let mut body: serde_json::Value = resp.into_json()?;
- let mut sounds: Vec<Item> = serde_json::from_value(body["results"].take()).unwrap();
- for s in &mut sounds {
- s.mpc_load_option = Some(MpcLoadOptions::ADD);
- }
- Ok(sounds)
-}
-pub fn new() -> Item {
- match retrieve_channels() {
- Ok(radios) => {
- assert!(radios
- .iter()
- .all(|r| r.description.is_some() || r.website.is_some()));
- assert!(radios.iter().all(|r| r.url.is_some()));
- return Item {
- name: "Sounds of Earth".to_string(),
- website: Some("https://www.soundsofearth.eco/".to_string()),
- radios: Some(radios),
- mpc_load_option: None,
- url: None,
- description: None,
- };
- }
- Err(e) => panic!("error getting channels: {}", e),
- }
-}
diff --git a/src/radio/mod.rs b/src/radios/mod.rs
diff --git a/src/radio/radiogarden.rb b/src/radios/radiogarden.rb
diff --git a/src/radios/sounds_of_earth.rs b/src/radios/sounds_of_earth.rs
@@ -0,0 +1,33 @@
+use crate::radio::*;
+use crate::config::*;
+use serde_json;
+use ureq;
+fn retrieve_channels() -> Result<Vec<Radio>, ureq::Error> {
+ let uri = "https://soundsofearth.eco/regions.json";
+ let resp: ureq::Response = ureq::get(uri).call()?;
+ let mut body: serde_json::Value = resp.into_json()?;
+ let mut sounds: Vec<Radio> = serde_json::from_value(body["results"].take()).unwrap();
+ for s in &mut sounds {
+ s.mpc_load_option = Some(MpcLoadOptions::ADD);
+ }
+ Ok(sounds)
+}
+pub fn new() -> Radio {
+ match retrieve_channels() {
+ Ok(radios) => {
+ assert!(radios
+ .iter()
+ .all(|r| r.description.is_some() || r.website.is_some()));
+ assert!(radios.iter().all(|r| r.url.is_some()));
+ return Radio {
+ name: "Sounds of Earth".to_string(),
+ website: Some("https://www.soundsofearth.eco/".to_string()),
+ radios: Some(radios),
+ mpc_load_option: None,
+ url: None,
+ description: None,
+ };
+ }
+ Err(e) => panic!("error getting channels: {}", e),
+ }
+}
diff --git a/src/radio/subreddit.rb b/src/radios/subreddit.rb
diff --git a/src/tunein.rs b/src/tunein.rs
@@ -1,20 +0,0 @@
-use serde::Deserialize;
-#[derive(Deserialize, Clone, Copy)]
-pub enum PlaybackOptions {
- MPC,
- MPV,
-}
-#[derive(Deserialize, Clone, Copy, Debug)]
-pub enum MpcLoadOptions {
- ADD,
- LOAD,
-}
-#[derive(Clone, Copy)]
-pub struct RadioOptions {
- pub method: PlaybackOptions,
- pub load_options: Option<MpcLoadOptions>,
-}
-
-pub trait TuneIn {
- fn play(&self, options: &RadioOptions);
-}