A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
23
[[bin]]24
name = "import"25
path = "src/import.rs"27
[[bin]]28
name = "test"29
path = "src/test.rs"
1
mod station_adapter;3
pub use station_adapter::StationAdapter;5
pub trait Adapter<T> {6
fn populate(self, target: T) -> T;7
}
1
use serde::{Deserialize};2
use regex::Regex;4
use crate::common::Station;5
use crate::adapter::Adapter;7
#[derive(Debug, Deserialize, Clone)]8
pub struct StationAdapter {9
pub stationuuid: String,10
pub name: String,11
pub url: String,12
pub votes: i64,13
pub favicon: String,14
pub homepage: String,15
pub tags: String,16
pub clicktrend: i64,17
pub clickcount: i64,18
pub countrycode: String,19
pub languagecodes: String,20
pub state: String,21
}23
impl Adapter<Station> for StationAdapter {24
fn populate(self, mut target: Station) -> Station {25
target.id = self.stationuuid;26
target.name = self.name;27
target.stream_url = upgrade_to_https(self.url);28
target.votes = self.votes;29
target.favicon = self.favicon;30
target.homepage = self.homepage;31
target.tags = to_array(self.tags);32
target.click_trend = self.clicktrend;33
target.click_count = self.clickcount;34
target.country = self.countrycode;35
target.languages = to_array(self.languagecodes);36
target.state = self.state;37
target38
}39
}41
fn to_array(value: String) -> Vec<String> {42
value.split(",")43
.map(str::to_string)44
.filter(|tag| tag.trim().len() > 0)45
.collect::<Vec<String>>()46
}48
fn upgrade_to_https(url: String) -> String {49
if Regex::new("^http://")50
.unwrap()51
.is_match(&url)52
{53
/* TODO: include in config struct */54
return format!("https://api.radio.typable.dev/pass?redirect={}", &url);55
}56
url57
}
1
pub mod api;
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
}
1
mod station;3
pub use station::Station;
1
use serde::{Deserialize, Serialize};3
#[derive(Debug, Default, Deserialize, Serialize)]4
pub struct Station {5
pub id: String,6
pub name: String,7
pub stream_url: String,8
pub votes: i64,9
pub favicon: String,10
pub homepage: String,11
pub tags: Vec<String>,12
pub click_trend: i64,13
pub click_count: i64,14
pub country: String,15
pub languages: Vec<String>,16
pub state: String,17
}
1
use std::fmt;3
use serde::Serialize;5
pub type Result<T> = std::result::Result<T, Error>;7
#[derive(Debug, Clone, Serialize)]8
pub enum ErrorKind {9
Identity,10
Arguments,11
Query,12
Fetch,13
Parse,14
}16
impl fmt::Display for ErrorKind {17
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {18
write!(f, "{:?}", self)19
}20
}22
#[derive(Debug, Clone, Serialize)]23
pub struct Error {24
kind: ErrorKind,25
message: String,26
}28
impl Error {29
pub fn new(kind: ErrorKind, message: &str) -> Self {30
Self { kind, message: message.into() }31
}32
}34
impl fmt::Display for Error {35
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {36
write!(f, "[{}] {}", self.kind, self.message)37
}38
}
1
use surf;2
use sqlx::postgres::PgPool;4
use verbal::adapter::Adapter;4
mod common;5
mod adapter;6
mod models;8
use adapter::Adapter;9
use models::{Station, StationAdapter};11
trait IntoInsert {
1
use std::fmt::Display;2
use serde::{ Serialize};2
use serde::Serialize;4
mod config;6
pub mod model;7
pub mod adapter;7
pub mod middleware;9
pub use config::Config;
31
}33
#[derive(Debug, Serialize)]35
pub enum ErrorKind {36
Identity,37
Arguments,38
Query,39
Fetch,40
Parse,41
}43
#[derive(Debug, Serialize)]44
pub struct Error {45
kind: ErrorKind,46
message: String,47
}49
impl Error {50
pub fn new(kind: ErrorKind, message: &str) -> Self {51
Self { kind, message: message.into() }52
}53
}55
#[derive(Debug, Serialize)]34
pub struct Response<T, E> {35
pub ok: bool,36
pub data: Option<T>,
9
use verbal::Config;10
use verbal::model::Model;11
use verbal::adapter::Adapter;11
use verbal::middleware::CorsMiddleware;13
use verbal::{State, Response, Error, ErrorKind};12
use verbal::{State, Response};14
mod common;15
mod error;16
mod adapter;17
mod models;19
use error::{Error, ErrorKind};20
use adapter::Adapter;21
use models::{Account, Favorite, Search, Code, Station, StationAdapter};23
#[async_std::main]
5
use rand::{distributions::Alphanumeric, Rng};7
use verbal::model::Query;8
use verbal::adapter::Adapter;8
use verbal::Utils;10
use crate::adapter::Adapter;12
#[derive(Debug, Deserialize, Serialize, FromRow)]13
pub struct Account {14
pub username: Option<String>,
1
mod common;2
mod error;3
mod backend;4
mod adapter;6
use backend::api::{RadioBrowserApi, Search};8
#[async_std::main]9
async fn main() {10
let mut search = Search::default();11
search.name = "bbc".into();12
match RadioBrowserApi::search(search).await {13
Ok(result) => println!("{:#?}", result),14
Err(err) => println!("{}", err),15
}16
}