mirror of
https://gitlab.com/hladislav/radiobrowser-lib-rust.git
synced 2025-04-29 23:34:12 +00:00
random server select
This commit is contained in:
parent
ea788a7c01
commit
505d574c93
3 changed files with 154 additions and 41 deletions
|
@ -7,6 +7,8 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
async-std = { version = "*", features = ["attributes", "tokio1"] }
|
async-std = { version = "*", features = ["attributes", "tokio1"] }
|
||||||
|
async-std-resolver = "0.21.2"
|
||||||
|
chrono = { version = "0.4.19", features = ["serde"] }
|
||||||
|
rand = { version = "0.8.5" }
|
||||||
reqwest = { version = "0.11.10", features = ["json"] }
|
reqwest = { version = "0.11.10", features = ["json"] }
|
||||||
serde = { version = "1.0.136", features = ["derive"] }
|
serde = { version = "1.0.136", features = ["derive"] }
|
||||||
async-std-resolver = "0.21.2"
|
|
|
@ -1,13 +1,12 @@
|
||||||
use radiobrowser_lib_rust::ApiConfig;
|
use radiobrowser_lib_rust::RadioBrowserAPI;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
#[async_std::main]
|
#[async_std::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let servers = radiobrowser_lib_rust::get_servers().await?;
|
let mut api = RadioBrowserAPI::new().await?;
|
||||||
println!("Servers: {:?}", servers);
|
let config = api.get_server_config().await?;
|
||||||
for server in servers {
|
|
||||||
let config: ApiConfig = radiobrowser_lib_rust::get_server_config(server).await?;
|
|
||||||
println!("{:#?}", config);
|
println!("{:#?}", config);
|
||||||
}
|
let countries = api.get_countries().await?;
|
||||||
|
println!("{:?}", countries);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
150
src/lib.rs
150
src/lib.rs
|
@ -1,11 +1,81 @@
|
||||||
use reqwest;
|
use reqwest;
|
||||||
|
use serde::de::DeserializeOwned;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
|
use rand::seq::SliceRandom;
|
||||||
|
use rand::thread_rng;
|
||||||
|
|
||||||
use async_std_resolver::proto::rr::RecordType;
|
use async_std_resolver::proto::rr::RecordType;
|
||||||
use async_std_resolver::proto::xfer::DnsRequestOptions;
|
use async_std_resolver::proto::xfer::DnsRequestOptions;
|
||||||
use async_std_resolver::{config, resolver};
|
use async_std_resolver::{config, resolver};
|
||||||
|
|
||||||
|
use chrono::DateTime;
|
||||||
|
use chrono::Utc;
|
||||||
|
|
||||||
|
#[derive(PartialEq, Deserialize, Debug)]
|
||||||
|
pub struct Station {
|
||||||
|
pub changeuuid: String,
|
||||||
|
pub stationuuid: String,
|
||||||
|
pub serveruuid: Option<String>,
|
||||||
|
pub name: String,
|
||||||
|
pub url: String,
|
||||||
|
pub url_resolved: String,
|
||||||
|
pub homepage: String,
|
||||||
|
pub favicon: String,
|
||||||
|
pub tags: String,
|
||||||
|
pub country: String,
|
||||||
|
pub countrycode: String,
|
||||||
|
pub iso_3166_2: Option<String>,
|
||||||
|
pub state: String,
|
||||||
|
pub language: String,
|
||||||
|
pub languagecodes: Option<String>,
|
||||||
|
pub votes: i32,
|
||||||
|
pub lastchangetime: String,
|
||||||
|
pub lastchangetime_iso8601: Option<DateTime<Utc>>,
|
||||||
|
pub codec: String,
|
||||||
|
pub bitrate: u32,
|
||||||
|
pub hls: i8,
|
||||||
|
pub lastcheckok: i8,
|
||||||
|
pub lastchecktime: String,
|
||||||
|
pub lastchecktime_iso8601: Option<DateTime<Utc>>,
|
||||||
|
pub lastcheckoktime: String,
|
||||||
|
pub lastcheckoktime_iso8601: Option<DateTime<Utc>>,
|
||||||
|
pub lastlocalchecktime: String,
|
||||||
|
pub lastlocalchecktime_iso8601: Option<DateTime<Utc>>,
|
||||||
|
pub clicktimestamp: String,
|
||||||
|
pub clicktimestamp_iso8601: Option<DateTime<Utc>>,
|
||||||
|
pub clickcount: u32,
|
||||||
|
pub clicktrend: i32,
|
||||||
|
pub ssl_error: Option<u8>,
|
||||||
|
pub geo_lat: Option<f64>,
|
||||||
|
pub geo_long: Option<f64>,
|
||||||
|
pub has_extended_info: Option<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq, Eq, Deserialize, Debug)]
|
||||||
|
pub struct ApiCountry {
|
||||||
|
pub name: String,
|
||||||
|
pub iso_3166_1: String,
|
||||||
|
pub stationcount: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq, Eq, Deserialize, Debug)]
|
||||||
|
pub struct ApiLanguage {
|
||||||
|
pub name: String,
|
||||||
|
pub iso_639: Option<String>,
|
||||||
|
pub stationcount: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq, Deserialize, Debug)]
|
||||||
|
pub struct ApiStreamingServer {
|
||||||
|
pub uuid: String,
|
||||||
|
pub url: String,
|
||||||
|
pub statusurl: Option<String>,
|
||||||
|
pub status: Option<String>,
|
||||||
|
pub error: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct ApiConfig {
|
pub struct ApiConfig {
|
||||||
pub check_enabled: bool,
|
pub check_enabled: bool,
|
||||||
|
@ -32,28 +102,69 @@ pub struct ApiConfig {
|
||||||
pub language_to_code_filepath: String,
|
pub language_to_code_filepath: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_server_config<P: AsRef<str>>(server: P) -> Result<ApiConfig, Box<dyn Error>> {
|
pub struct RadioBrowserAPI {
|
||||||
let client = reqwest::Client::new();
|
servers: Vec<String>,
|
||||||
let res = client
|
current: usize,
|
||||||
.post(format!("https://{}/json/config", server.as_ref()))
|
|
||||||
.send()
|
|
||||||
.await?
|
|
||||||
.json::<ApiConfig>()
|
|
||||||
.await?;
|
|
||||||
Ok(res)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_servers() -> Result<Vec<String>, Box<dyn Error>> {
|
impl RadioBrowserAPI {
|
||||||
// Construct a new Resolver with default configuration options
|
pub async fn new() -> Result<Self, Box<dyn Error>> {
|
||||||
|
Ok(RadioBrowserAPI {
|
||||||
|
servers: RadioBrowserAPI::get_servers().await?,
|
||||||
|
current: 0,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_current_server(&mut self) -> String {
|
||||||
|
if self.servers.len() > 0 {
|
||||||
|
self.current = self.current % self.servers.len();
|
||||||
|
format!("https://{}", self.servers[self.current])
|
||||||
|
} else {
|
||||||
|
String::from("https://de1.api.radio-browser.info")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn post_api<P: DeserializeOwned, O: AsRef<str>>(
|
||||||
|
&mut self,
|
||||||
|
endpoint: O,
|
||||||
|
) -> Result<P, Box<dyn Error>> {
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
let res = client
|
||||||
|
.post(format!(
|
||||||
|
"{}{}",
|
||||||
|
self.get_current_server(),
|
||||||
|
endpoint.as_ref()
|
||||||
|
))
|
||||||
|
.send()
|
||||||
|
.await?
|
||||||
|
.json::<P>()
|
||||||
|
.await?;
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_server_config(&mut self) -> Result<ApiConfig, Box<dyn Error>> {
|
||||||
|
Ok(self.post_api("/json/config").await?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_countries(&mut self) -> Result<Vec<ApiCountry>, Box<dyn Error>> {
|
||||||
|
Ok(self.post_api("/json/countries").await?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_countries_filtered<P: AsRef<str>>(
|
||||||
|
&mut self,
|
||||||
|
filter: P,
|
||||||
|
) -> Result<Vec<ApiCountry>, Box<dyn Error>> {
|
||||||
|
Ok(self
|
||||||
|
.post_api(format!("/json/countries/{}", filter.as_ref()))
|
||||||
|
.await?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_servers() -> Result<Vec<String>, Box<dyn Error>> {
|
||||||
let resolver = resolver(
|
let resolver = resolver(
|
||||||
config::ResolverConfig::default(),
|
config::ResolverConfig::default(),
|
||||||
config::ResolverOpts::default(),
|
config::ResolverOpts::default(),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// Lookup the IP addresses associated with a name.
|
|
||||||
// This returns a future that will lookup the IP addresses, it must be run in the Core to
|
|
||||||
// to get the actual result.
|
|
||||||
let response = resolver
|
let response = resolver
|
||||||
.lookup(
|
.lookup(
|
||||||
"_api._tcp.radio-browser.info",
|
"_api._tcp.radio-browser.info",
|
||||||
|
@ -61,13 +172,14 @@ pub async fn get_servers() -> Result<Vec<String>, Box<dyn Error>> {
|
||||||
DnsRequestOptions::default(),
|
DnsRequestOptions::default(),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
let mut list: Vec<String> = response
|
||||||
// There can be many addresses associated with the name,
|
|
||||||
// this can return IPv4 and/or IPv6 addresses
|
|
||||||
let list = response
|
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|entry| entry.as_srv())
|
.filter_map(|entry| entry.as_srv())
|
||||||
.map(|entry| entry.target().to_string().trim_matches('.').to_string())
|
.map(|entry| entry.target().to_string().trim_matches('.').to_string())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
list.shuffle(&mut thread_rng());
|
||||||
|
println!("Servers: {:?}", list);
|
||||||
Ok(list)
|
Ok(list)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue