so://phie

verbal

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]

1use async_std::io::ReadExt;

2use regex::Regex;

3use sqlx::Acquire;

4use sqlx::Postgres;

5use tide_sqlx::SQLxRequestExt;

6

7use crate::model;

8use crate::model::Country;

9use crate::model::Id;

10use crate::model::Language;

11use crate::model::Tag;

12use crate::unwrap_option_or_throw;

13use crate::unwrap_result_or_throw;

14use crate::Response;

15use crate::ToSql;

16

17pub async fn do_prefetch(_: tide::Request<()>) -> tide::Result {

18 Ok(tide::Response::new(200))

19}

20

21pub 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}

26

27pub 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 SELECT

34 station.*,

35 station_status.is_restricted,

36 station_status.is_broken,

37 station_status.is_no_track_info,

38 station_status.is_hidden,

39 CASE

40 WHEN favorite.id IS NULL OR favorite.account_id != {account_id}

41 THEN false

42 ELSE true

43 END AS is_favorite

44 FROM station

45 LEFT JOIN favorite

46 ON station.id = favorite.station_id

47 LEFT JOIN station_status

48 ON station.id = station_status.station_id

49 {conditions}

50 AND station_status.is_hidden IS NOT true

51 OFFSET {offset}

52 LIMIT 10

53 "#,

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}

64

65pub 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}

103

104pub 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 SELECT

110 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_favorite

116 FROM station

117 LEFT JOIN favorite

118 ON station.id = favorite.station_id

119 LEFT JOIN station_status

120 ON station.id = station_status.station_id

121 WHERE favorite.account_id = {account_id}

122 AND station_status.is_hidden IS NOT true

123 "#,

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}

132

133pub 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 INTO

140 favorite

141 (account_id, station_id)

142 VALUES

143 ({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}

152

153pub 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 FROM

160 favorite

161 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}

171

172pub 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 SELECT

179 station.*,

180 station_status.is_restricted,

181 station_status.is_broken,

182 station_status.is_no_track_info,

183 station_status.is_hidden,

184 CASE

185 WHEN favorite.id IS NULL OR favorite.account_id != {account_id}

186 THEN false

187 ELSE true

188 END AS is_favorite

189 FROM station

190 LEFT JOIN favorite

191 ON station.id = favorite.station_id

192 LEFT JOIN station_status

193 ON station.id = station_status.station_id

194 WHERE station.id = {station_id}

195 AND station_status.is_hidden IS NOT true

196 "#,

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}

206

207pub async fn get_countries(req: tide::Request<()>) -> tide::Result {

208 let sql = format!(

209 r#"

210 SELECT

211 country

212 FROM station

213 WHERE country != ''

214 GROUP BY country

215 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}

224

225pub async fn get_languages(req: tide::Request<()>) -> tide::Result {

226 let sql = format!(

227 r#"

228 SELECT

229 UNNEST(languages) AS language

230 FROM station

231 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}

241

242pub async fn get_tags(req: tide::Request<()>) -> tide::Result {

243 let sql = format!(

244 r#"

245 SELECT

246 UNNEST(tags) AS tag

247 FROM station

248 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}