A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: 134572fe4a8afb48aa390d01bab293a01f696de6 (4615 bytes)[doc][blame][raw]
1
use async_std::io::ReadExt;2
use regex::Regex;3
use sqlx::Acquire;4
use sqlx::Postgres;5
use tide_sqlx::SQLxRequestExt;7
use crate::model;8
use crate::Response;9
use crate::ToSql;11
pub async fn do_prefetch(_: tide::Request<()>) -> tide::Result {12
Ok(tide::Response::new(200))13
}15
pub async fn get_account(req: tide::Request<()>) -> tide::Result {16
let account = req.ext::<model::Account>().unwrap();17
Response::with(account)18
}20
pub async fn do_search(req: tide::Request<()>) -> tide::Result {21
let query = req.query::<model::Query>()?;22
let account = req.ext::<model::Account>().unwrap();23
let sql = format!(24
r#"25
SELECT26
station.*,27
CASE28
WHEN favorite.id IS NULL OR favorite.account_id != {account_id}29
THEN false30
ELSE true31
END AS is_favorite32
FROM station33
LEFT JOIN favorite34
ON station.id = favorite.station_id35
{conditions}36
OFFSET {offset}37
LIMIT 1038
"#,39
account_id = account.id,40
offset = query.page.unwrap_or_default() * 10,41
conditions = query.to_sql().unwrap(),42
);43
let mut conn = req.sqlx_conn::<Postgres>().await;44
let result = sqlx::query_as::<_, model::Station>(&sql)45
.fetch_all(conn.acquire().await?)46
.await?;47
Response::with(result)48
}50
pub async fn get_song(req: tide::Request<()>) -> tide::Result {51
let station = req.query::<model::Station>()?;52
let mut response = surf::get(&station.url).header("icy-metadata", "1").await?;53
let meta_int = match response.header("icy-metaint") {54
Some(header) => header.as_str(),55
None => return Response::throw("no icy metadata header found!"),56
};57
let interval = match meta_int.parse::<usize>() {58
Ok(interval) => interval * 2,59
Err(_) => return Response::throw("icy metadata interval is not a number!"),60
};61
let mut total = 0;62
let mut bytes = Vec::new();63
let mut buf = [0; 10000];64
let mut title = None;65
loop {66
let len = response.read(&mut buf).await.unwrap();67
bytes.extend_from_slice(&buf);68
if total + len > interval {69
let metadata = String::from_utf8_lossy(&bytes);70
if let Some(cap) = Regex::new("StreamTitle='([^;]*)';")71
.unwrap()72
.captures_iter(&metadata)73
.next()74
{75
title = Some(cap[1].to_string());76
}77
break;78
}79
if total > interval * 2 {80
warn!("too many iterations while acquiring song title!");81
break;82
}83
total += len;84
}85
Response::with(title)86
}88
pub async fn get_favorites(req: tide::Request<()>) -> tide::Result {89
let account = req.ext::<model::Account>().unwrap();90
let sql = format!(91
r#"92
SELECT93
station.*,94
true as is_favorite95
FROM station96
LEFT JOIN favorite97
ON station.id = favorite.station_id98
WHERE favorite.account_id = {account_id}99
"#,100
account_id = account.id,101
);102
let mut conn = req.sqlx_conn::<Postgres>().await;103
let result = sqlx::query_as::<_, model::Station>(&sql)104
.fetch_all(conn.acquire().await?)105
.await?;106
Response::with(result)107
}109
pub async fn add_favorite(mut req: tide::Request<()>) -> tide::Result {110
let station_id = req.body_json::<i32>().await?;111
let account = req.ext::<model::Account>().unwrap();112
let sql = format!(113
r#"114
INSERT INTO115
favorite116
(account_id, station_id)117
VALUES118
({account_id}, {station_id})119
"#,120
account_id = account.id,121
station_id = station_id,122
);123
let mut conn = req.sqlx_conn::<Postgres>().await;124
sqlx::query(&sql).execute(conn.acquire().await?).await?;125
Response::with(())126
}128
pub async fn delete_favorite(mut req: tide::Request<()>) -> tide::Result {129
let station_id = req.body_json::<i32>().await?;130
let account = req.ext::<model::Account>().unwrap();131
let sql = format!(132
r#"133
DELETE FROM134
favorite135
WHERE account_id = {account_id}136
AND station_id = {station_id}137
"#,138
account_id = account.id,139
station_id = station_id,140
);141
let mut conn = req.sqlx_conn::<Postgres>().await;142
sqlx::query(&sql).execute(conn.acquire().await?).await?;143
Response::with(())144
}