A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: 1c945c634f546d2882f9777500228585f9f8428c (1960 bytes)[doc][blame][raw]
1
use serde::{Deserialize, Serialize};3
use crate::common::Station;4
use crate::error::{Result, Error, ErrorKind};5
use crate::adapter::{Adapter, StationAdapter};7
const API_URLS: [&str; 3] = [8
"https://de1.api.radio-browser.info",9
"https://fr1.api.radio-browser.info",10
"https://nl1.api.radio-browser.info",11
];13
#[derive(Debug, Default, Deserialize, Serialize)]14
pub struct Search {15
pub name: String,16
pub page: Option<i64>,17
}19
impl Search {21
pub fn into_query(&self) -> String {22
format!(23
"name={}&offset={}&limit=10&order=votes&reverse=true&hidebroken=true",24
&self.name,25
&self.page.unwrap_or(0) * 10,26
)27
}29
}31
pub struct RadioBrowserApi;33
impl RadioBrowserApi {35
pub async fn search(search: Search) -> Result<Vec<Station>> {36
let mut successful_response = None;37
for api_url in API_URLS {38
let url = format!(39
"{}/json/stations/search?{}",40
&api_url,41
&search.into_query(),42
);43
match surf::get(&url).await {44
Ok(response) => {45
successful_response = Some(response);46
break;47
},48
Err(err) => println!("{}", Error::new(ErrorKind::Fetch, &err.to_string())),49
}50
}51
let mut response = match successful_response {52
Some(response) => response,53
None => return Err(Error::new(ErrorKind::Fetch, "All request attempts failed!")),54
};55
let adapters = match response.body_json::<Vec<StationAdapter>>().await {56
Ok(adapters) => adapters,57
Err(err) => return Err(Error::new(ErrorKind::Parse, &err.to_string())),58
};59
let mut stations = vec![];60
for adapter in &adapters {61
stations.push(62
adapter.clone().populate(Station::default())63
);64
}65
Ok(stations)66
}68
}