Browse and listen to thousands of radio stations across the globe right from your terminal ๐ŸŒŽ ๐Ÿ“ป ๐ŸŽตโœจ
radio rust tokio web-radio command-line-tool tui
at main 3.7 kB view raw
1use std::thread; 2 3use radiobrowser::ApiStation; 4use tunein::types::{SearchResult, StationLinkDetails}; 5 6use crate::extract::extract_stream_url; 7 8#[derive(Debug, Clone)] 9pub struct Station { 10 pub id: String, 11 pub name: String, 12 pub codec: String, 13 pub bitrate: u32, 14 pub stream_url: String, 15 pub playing: Option<String>, 16} 17 18impl From<ApiStation> for Station { 19 fn from(station: ApiStation) -> Station { 20 Station { 21 id: station.stationuuid, 22 name: station.name, 23 codec: station.codec, 24 bitrate: station.bitrate, 25 stream_url: station.url_resolved, 26 playing: None, 27 } 28 } 29} 30 31impl From<SearchResult> for Station { 32 fn from(result: SearchResult) -> Station { 33 Station { 34 id: result.guide_id.unwrap_or_default(), 35 name: result.text, 36 bitrate: result 37 .bitrate 38 .unwrap_or("0".to_string()) 39 .parse() 40 .unwrap_or_default(), 41 codec: Default::default(), 42 stream_url: Default::default(), 43 playing: result.subtext, 44 } 45 } 46} 47 48impl From<Box<SearchResult>> for Station { 49 fn from(result: Box<SearchResult>) -> Station { 50 Station { 51 id: result.guide_id.unwrap_or_default(), 52 name: result.text, 53 bitrate: result 54 .bitrate 55 .unwrap_or("0".to_string()) 56 .parse() 57 .unwrap_or_default(), 58 codec: Default::default(), 59 stream_url: Default::default(), 60 playing: None, 61 } 62 } 63} 64 65impl From<StationLinkDetails> for Station { 66 fn from(details: StationLinkDetails) -> Station { 67 let handle = thread::spawn(move || { 68 let rt = tokio::runtime::Runtime::new().unwrap(); 69 rt.block_on(extract_stream_url(&details.url, details.playlist_type)) 70 }); 71 let stream_url = handle.join().unwrap().unwrap(); 72 Station { 73 id: Default::default(), 74 name: Default::default(), 75 bitrate: details.bitrate, 76 stream_url, 77 codec: details.media_type.to_uppercase(), 78 playing: None, 79 } 80 } 81} 82 83impl From<tunein::types::Station> for Station { 84 fn from(st: tunein::types::Station) -> Station { 85 Station { 86 id: st.guide_id.unwrap_or_default(), 87 name: st.text, 88 bitrate: st 89 .bitrate 90 .unwrap_or("0".to_string()) 91 .parse() 92 .unwrap_or_default(), 93 stream_url: Default::default(), 94 codec: st.formats.unwrap_or_default().to_uppercase(), 95 playing: st.playing, 96 } 97 } 98} 99 100impl From<Box<tunein::types::Station>> for Station { 101 fn from(st: Box<tunein::types::Station>) -> Station { 102 Station { 103 id: st.guide_id.unwrap_or_default(), 104 name: st.text, 105 bitrate: st 106 .bitrate 107 .unwrap_or("0".to_string()) 108 .parse() 109 .unwrap_or_default(), 110 stream_url: Default::default(), 111 codec: st.formats.unwrap_or_default().to_uppercase(), 112 playing: st.playing, 113 } 114 } 115} 116 117impl From<tunein::types::CategoryDetails> for Station { 118 fn from(ct: tunein::types::CategoryDetails) -> Station { 119 Station { 120 id: ct.guide_id.unwrap_or_default(), 121 name: ct.text, 122 bitrate: 0, 123 stream_url: Default::default(), 124 codec: Default::default(), 125 playing: None, 126 } 127 } 128}