A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: c74cf003134b0e89f68cb956560ec6a975b6a649 (8661 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::model::Country;9
use crate::model::Id;10
use crate::model::Language;11
use crate::model::Tag;12
use crate::unwrap_option_or_throw;13
use crate::unwrap_result_or_throw;14
use crate::Response;15
use crate::ToSql;17
pub async fn do_prefetch(_: tide::Request<()>) -> tide::Result {18
Ok(tide::Response::new(200))19
}21
pub async fn get_account(req: tide::Request<()>) -> tide::Result {22
let account =23
unwrap_option_or_throw!(req.ext::<model::Account>(), "no account in request found!");24
Response::with(account)25
}27
pub async fn do_search(req: tide::Request<()>) -> tide::Result {28
let query = req.query::<model::Query>()?;29
let account =30
unwrap_option_or_throw!(req.ext::<model::Account>(), "no account in request found!");31
let sql = format!(32
r#"33
SELECT34
station.*,35
station_status.is_restricted,36
station_status.is_broken,37
station_status.is_no_track_info,38
station_status.is_hidden,39
CASE40
WHEN favorite.id IS NULL OR favorite.account_id != {account_id}41
THEN false42
ELSE true43
END AS is_favorite44
FROM station45
LEFT JOIN favorite46
ON station.id = favorite.station_id47
LEFT JOIN station_status48
ON station.id = station_status.station_id49
{conditions}50
AND station_status.is_hidden IS NOT true51
OFFSET {offset}52
LIMIT 1053
"#,54
account_id = account.id,55
offset = query.page.unwrap_or_default() * 10,56
conditions = unwrap_result_or_throw!(query.to_sql(), "cannot parse sql statement!"),57
);58
let mut conn = req.sqlx_conn::<Postgres>().await;59
let result = sqlx::query_as::<_, model::Station>(&sql)60
.fetch_all(conn.acquire().await?)61
.await?;62
Response::with(result)63
}65
pub async fn get_song(req: tide::Request<()>) -> tide::Result {66
let station = req.query::<model::Station>()?;67
let mut response = surf::get(&station.url).header("icy-metadata", "1").await?;68
let meta_int = match response.header("icy-metaint") {69
Some(header) => header.as_str(),70
None => return Response::throw("no icy metadata header found!"),71
};72
let interval = match meta_int.parse::<usize>() {73
Ok(interval) => interval * 2,74
Err(_) => return Response::throw("icy metadata interval is not a number!"),75
};76
let mut total = 0;77
let mut bytes = Vec::new();78
let mut buf = [0; 10000];79
let mut title = None;80
loop {81
let len =82
unwrap_result_or_throw!(response.read(&mut buf).await, "cannot read stream data!");83
bytes.extend_from_slice(&buf);84
if total + len > interval {85
let metadata = String::from_utf8_lossy(&bytes);86
if let Some(cap) =87
unwrap_result_or_throw!(Regex::new("StreamTitle='([^;]*)';"), "cannot parse regex!")88
.captures_iter(&metadata)89
.next()90
{91
title = Some(cap[1].to_string());92
}93
break;94
}95
if total > interval * 2 {96
warn!("too many iterations while acquiring song title!");97
break;98
}99
total += len;100
}101
Response::with(title)102
}104
pub async fn get_favorites(req: tide::Request<()>) -> tide::Result {105
let account =106
unwrap_option_or_throw!(req.ext::<model::Account>(), "no account in request found!");107
let sql = format!(108
r#"109
SELECT110
station.*,111
station_status.is_restricted,112
station_status.is_broken,113
station_status.is_no_track_info,114
station_status.is_hidden,115
true as is_favorite116
FROM station117
LEFT JOIN favorite118
ON station.id = favorite.station_id119
LEFT JOIN station_status120
ON station.id = station_status.station_id121
WHERE favorite.account_id = {account_id}122
AND station_status.is_hidden IS NOT true123
"#,124
account_id = account.id,125
);126
let mut conn = req.sqlx_conn::<Postgres>().await;127
let result = sqlx::query_as::<_, model::Station>(&sql)128
.fetch_all(conn.acquire().await?)129
.await?;130
Response::with(result)131
}133
pub async fn add_favorite(mut req: tide::Request<()>) -> tide::Result {134
let station_id = req.body_json::<i32>().await?;135
let account =136
unwrap_option_or_throw!(req.ext::<model::Account>(), "no account in request found!");137
let sql = format!(138
r#"139
INSERT INTO140
favorite141
(account_id, station_id)142
VALUES143
({account_id}, {station_id})144
"#,145
account_id = account.id,146
station_id = station_id,147
);148
let mut conn = req.sqlx_conn::<Postgres>().await;149
sqlx::query(&sql).execute(conn.acquire().await?).await?;150
Response::with(())151
}153
pub async fn delete_favorite(mut req: tide::Request<()>) -> tide::Result {154
let station_id = req.body_json::<i32>().await?;155
let account =156
unwrap_option_or_throw!(req.ext::<model::Account>(), "no account in request found!");157
let sql = format!(158
r#"159
DELETE FROM160
favorite161
WHERE account_id = {account_id}162
AND station_id = {station_id}163
"#,164
account_id = account.id,165
station_id = station_id,166
);167
let mut conn = req.sqlx_conn::<Postgres>().await;168
sqlx::query(&sql).execute(conn.acquire().await?).await?;169
Response::with(())170
}172
pub async fn get_station(req: tide::Request<()>) -> tide::Result {173
let station_id = req.query::<Id>()?;174
let account =175
unwrap_option_or_throw!(req.ext::<model::Account>(), "no account in request found!");176
let sql = format!(177
r#"178
SELECT179
station.*,180
station_status.is_restricted,181
station_status.is_broken,182
station_status.is_no_track_info,183
station_status.is_hidden,184
CASE185
WHEN favorite.id IS NULL OR favorite.account_id != {account_id}186
THEN false187
ELSE true188
END AS is_favorite189
FROM station190
LEFT JOIN favorite191
ON station.id = favorite.station_id192
LEFT JOIN station_status193
ON station.id = station_status.station_id194
WHERE station.id = {station_id}195
AND station_status.is_hidden IS NOT true196
"#,197
account_id = account.id,198
station_id = station_id.0,199
);200
let mut conn = req.sqlx_conn::<Postgres>().await;201
let result = sqlx::query_as::<_, model::Station>(&sql)202
.fetch_optional(conn.acquire().await?)203
.await?;204
Response::with(result)205
}207
pub async fn get_countries(req: tide::Request<()>) -> tide::Result {208
let sql = format!(209
r#"210
SELECT211
country212
FROM station213
WHERE country != ''214
GROUP BY country215
ORDER BY COUNT(*) DESC;216
"#,217
);218
let mut conn = req.sqlx_conn::<Postgres>().await;219
let result = sqlx::query_as::<_, Country>(&sql)220
.fetch_all(conn.acquire().await?)221
.await?;222
Response::with(result)223
}225
pub async fn get_languages(req: tide::Request<()>) -> tide::Result {226
let sql = format!(227
r#"228
SELECT229
UNNEST(languages) AS language230
FROM station231
GROUP BY UNNEST(languages)232
ORDER BY COUNT(*) DESC;233
"#,234
);235
let mut conn = req.sqlx_conn::<Postgres>().await;236
let result = sqlx::query_as::<_, Language>(&sql)237
.fetch_all(conn.acquire().await?)238
.await?;239
Response::with(result)240
}242
pub async fn get_tags(req: tide::Request<()>) -> tide::Result {243
let sql = format!(244
r#"245
SELECT246
UNNEST(tags) AS tag247
FROM station248
GROUP BY UNNEST(tags)249
ORDER BY COUNT(*) DESC;250
"#,251
);252
let mut conn = req.sqlx_conn::<Postgres>().await;253
let result = sqlx::query_as::<_, Tag>(&sql)254
.fetch_all(conn.acquire().await?)255
.await?;256
Response::with(result)257
}