A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
15
serde = { version = "*", features = ["derive"] }16
async-std = { version = "*", features = ["attributes", "tokio1"] }17
sqlx = { version = "*", features = ["runtime-async-std-rustls", "postgres"] }19
[[bin]]20
name = "main"21
path = "src/main.rs"23
[[bin]]24
name = "import"25
path = "src/import.rs"
1
/* ########## account ########## */2
CREATE TABLE account (3
id serial PRIMARY KEY,4
created_at timestamp without time zone NOT NULL,5
updated_at timestamp without time zone NOT NULL,6
username text,7
language character(2) DEFAULT 'en',8
is_playback_history boolean DEFAULT true9
);11
/* ########## device ########## */12
CREATE TABLE device (13
id serial PRIMARY KEY,14
created_at timestamp without time zone NOT NULL,15
updated_at timestamp without time zone NOT NULL,16
token text UNIQUE NOT NULL,17
account_id integer18
);20
ALTER TABLE device21
ADD CONSTRAINT device_account_id22
FOREIGN KEY (account_id)23
REFERENCES account(id);25
/* ########## station ########## */26
CREATE TABLE station (27
id serial PRIMARY KEY,28
created_at timestamp without time zone NOT NULL,29
updated_at timestamp without time zone NOT NULL,30
"name" text NOT NULL,31
stream_url text NOT NULL,32
votes integer DEFAULT 0,33
favicon text,34
homepage text,35
tags text[],36
country text,37
languages text[],38
"state" text39
);41
/* ########## favorite ########## */42
CREATE TABLE favorite (43
id serial PRIMARY KEY,44
created_at timestamp without time zone NOT NULL,45
updated_at timestamp without time zone NOT NULL,46
station_id text NOT NULL,47
account_id integer NOT NULL48
);50
ALTER TABLE favorite51
ADD CONSTRAINT favorite_account_id52
FOREIGN KEY (account_id)53
REFERENCES account(id);55
ALTER TABLE favorite56
ADD CONSTRAINT favorite_station_id57
FOREIGN KEY (station_id)58
REFERENCES station(id);60
/* ########## code ########## */61
CREATE TABLE code (62
id serial PRIMARY KEY,63
code text UNIQUE NOT NULL,64
"timestamp" timestamp without time zone NOT NULL,65
used boolean DEFAULT false NOT NULL,66
account_id integer UNIQUE NOT NULL67
);69
ALTER TABLE code70
ADD CONSTRAINT code_account_id71
FOREIGN KEY (account_id)72
REFERENCES account(id);
1
use surf;2
use sqlx::postgres::PgPool;4
use verbal::adapter::Adapter;6
mod models;7
use models::{Station, StationAdapter};9
trait IntoInsert {10
fn into_insert(&self) -> String;11
}13
impl IntoInsert for Station {14
fn into_insert(&self) -> String {15
format!(16
"17
INSERT INTO station18
(19
created_at,20
updated_at,21
name,22
stream_url,23
votes,24
favicon,25
homepage,26
tags,27
country,28
languages,29
state30
)31
VALUES32
(33
CURRENT_TIMESTAMP,34
CURRENT_TIMESTAMP,35
'{}',36
'{}',37
{},38
'{}',39
'{}',40
'{{{}}}',41
'{}',42
'{{{}}}',43
'{}'44
);45
",46
escape_sql(&self.name),47
&self.stream_url,48
&self.votes,49
&self.favicon,50
escape_sql(&self.homepage),51
&self.tags52
.iter()53
.map(|tag| escape_sql(&tag))54
.map(|tag| format!("\"{}\"", &tag))55
.collect::<Vec<String>>()56
.join(","),57
escape_sql(&self.country),58
&self.languages.join(","),59
escape_sql(&self.state),60
)61
}62
}64
fn escape_sql(text: &str) -> String {65
text.replace("'", "''")66
.replace("\"", "\\\"")67
}69
#[async_std::main]70
async fn main() {71
let limit = 500;72
let offset = 3 * limit;73
let url = format!("https://de1.api.radio-browser.info/json/stations?limit={}&offset={}&hidebroken=true&order=name", limit, offset);74
let mut response = match surf::get(&url).await {75
Ok(response) => response,76
Err(err) => panic!("{}", &err.to_string()),77
};78
let adapters = match response.body_json::<Vec<StationAdapter>>().await {79
Ok(adapters) => adapters,80
Err(err) => panic!("{}", &err.to_string()),81
};82
let pool = match PgPool::connect("postgres://typable:De24Si98@192.168.2.3:9748/verbal?sslmode=disable").await {83
Ok(pool) => pool,84
Err(err) => panic!("{}", &err.to_string()),85
};86
let mut transaction = match pool.begin().await {87
Ok(transaction) => transaction,88
Err(err) => panic!("{}", &err.to_string()),89
};90
for adapter in &adapters {91
let station = adapter.clone().populate(Station::default());92
match sqlx::query(&station.into_insert())93
.execute(&mut *transaction)94
.await95
{96
Ok(_) => (),97
Err(err) => panic!("{}", &err.to_string()),98
}99
}100
match transaction.commit().await {101
Ok(_) => println!("DONE"),102
Err(err) => panic!("{}", &err.to_string()),103
}104
}