diff --git a/src/app.rs b/src/app.rs index d9baace..4be7496 100644 --- a/src/app.rs +++ b/src/app.rs @@ -118,23 +118,30 @@ fn render_frame(state: Arc>, frame: &mut Frame) { }, frame, ); - render_line( - "Now Playing ", - &state.now_playing, - Rect { - x: size.x, - y: size.y + 2, - width: size.width, - height: 1, - }, - frame, - ); + + if !state.now_playing.is_empty() { + render_line( + "Now Playing ", + &state.now_playing, + Rect { + x: size.x, + y: size.y + 2, + width: size.width, + height: 1, + }, + frame, + ); + } + render_line( "Genre ", &state.genre, Rect { x: size.x, - y: size.y + 3, + y: match state.now_playing.is_empty() { + true => size.y + 2, + false => size.y + 3, + }, width: size.width, height: 1, }, @@ -145,7 +152,10 @@ fn render_frame(state: Arc>, frame: &mut Frame) { &state.description, Rect { x: size.x, - y: size.y + 4, + y: match state.now_playing.is_empty() { + true => size.y + 3, + false => size.y + 4, + }, width: size.width, height: 1, }, @@ -156,7 +166,10 @@ fn render_frame(state: Arc>, frame: &mut Frame) { &format!("{} kbps", &state.br), Rect { x: size.x, - y: size.y + 5, + y: match state.now_playing.is_empty() { + true => size.y + 4, + false => size.y + 5, + }, width: size.width, height: 1, }, diff --git a/src/play.rs b/src/play.rs index 223d9c9..045943a 100644 --- a/src/play.rs +++ b/src/play.rs @@ -2,18 +2,17 @@ use std::{thread, time::Duration}; use anyhow::Error; use hyper::header::HeaderValue; -use tunein::TuneInClient; use crate::{ app::{App, State}, cfg::{SourceOptions, UiOptions}, decoder::Mp3Decoder, - extract::{extract_stream_url, get_currently_playing}, provider::{radiobrowser::Radiobrowser, tunein::Tunein, Provider}, tui, }; pub async fn exec(name_or_id: &str, provider: &str) -> Result<(), Error> { + let _provider = provider; let provider: Box = match provider { "tunein" => Box::new(Tunein::new()), "radiobrowser" => Box::new(Radiobrowser::new().await), @@ -24,52 +23,15 @@ pub async fn exec(name_or_id: &str, provider: &str) -> Result<(), Error> { ))) } }; + let station = provider.get_station(name_or_id.to_string()).await?; + if station.is_none() { + return Err(Error::msg("No station found")); + } - let client = TuneInClient::new(); - let results = client - .get_station(name_or_id) - .await - .map_err(|e| Error::msg(e.to_string()))?; - let (url, playlist_type, _, id) = match results.is_empty() { - true => { - let results = client - .search(name_or_id) - .await - .map_err(|e| Error::msg(e.to_string()))?; - match results.first() { - Some(result) => { - if result.r#type != Some("audio".to_string()) { - return Err(Error::msg("No station found")); - } - let id = result.guide_id.as_ref().unwrap(); - let station = client - .get_station(id) - .await - .map_err(|e| Error::msg(e.to_string()))?; - let station = station.first().unwrap(); - ( - station.url.clone(), - station.playlist_type.clone(), - station.media_type.clone(), - id.clone(), - ) - } - None => ("".to_string(), None, "".to_string(), "".to_string()), - } - } - false => { - let result = results.first().unwrap(); - ( - result.url.clone(), - result.playlist_type.clone(), - result.media_type.clone(), - name_or_id.to_string(), - ) - } - }; - let now_playing = get_currently_playing(&id).await?; - let stream_url = extract_stream_url(&url, playlist_type).await?; - println!("{}", stream_url); + let station = station.unwrap(); + let stream_url = station.stream_url.clone(); + let id = station.id.clone(); + let now_playing = station.playing.clone().unwrap_or_default(); let (cmd_tx, cmd_rx) = tokio::sync::mpsc::unbounded_channel::(); let (frame_tx, frame_rx) = std::sync::mpsc::channel::(); diff --git a/src/provider/tunein.rs b/src/provider/tunein.rs index 5cd05ce..24b1be4 100644 --- a/src/provider/tunein.rs +++ b/src/provider/tunein.rs @@ -36,7 +36,25 @@ impl Provider for Tunein { .await .map_err(|e| Error::msg(e.to_string()))?; match stations.len() { - 0 => Ok(None), + 0 => { + let results = self.search(id.clone()).await?; + let station = results.first().cloned(); + match station { + Some(st) => { + let stations = self + .client + .get_station(&st.id) + .await + .map_err(|e| Error::msg(e.to_string()))?; + let mut station = Station::from(stations[0].clone()); + station.id = st.id.clone(); + station.name = st.name.clone(); + station.playing = st.playing.clone(); + return Ok(Some(station)); + } + None => Ok(None), + } + } _ => Ok(Some(Station::from(stations[0].clone()))), } } diff --git a/src/types.rs b/src/types.rs index ad0a661..fb58b47 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,6 +1,10 @@ +use std::thread; + use radiobrowser::ApiStation; use tunein::types::{SearchResult, StationLinkDetails}; +use crate::extract::extract_stream_url; + #[derive(Debug, Clone)] pub struct Station { pub id: String, @@ -60,11 +64,16 @@ impl From> for Station { impl From for Station { fn from(details: StationLinkDetails) -> Station { + let handle = thread::spawn(move || { + let rt = tokio::runtime::Runtime::new().unwrap(); + rt.block_on(extract_stream_url(&details.url, details.playlist_type)) + }); + let stream_url = handle.join().unwrap().unwrap(); Station { id: Default::default(), name: Default::default(), bitrate: details.bitrate, - stream_url: details.url, + stream_url, codec: details.media_type.to_uppercase(), playing: None, }