so://phie

verbal

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]

1use async_std::io::ReadExt;

2use regex::Regex;

3use sqlx::Acquire;

4use sqlx::Postgres;

5use tide_sqlx::SQLxRequestExt;

6

7use crate::model;

8use crate::Response;

9use crate::ToSql;

10

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

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

13}

14

15pub async fn get_account(req: tide::Request<()>) -> tide::Result {

16 let account = req.ext::<model::Account>().unwrap();

17 Response::with(account)

18}

19

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

26 station.*,

27 CASE

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

29 THEN false

30 ELSE true

31 END AS is_favorite

32 FROM station

33 LEFT JOIN favorite

34 ON station.id = favorite.station_id

35 {conditions}

36 OFFSET {offset}

37 LIMIT 10

38 "#,

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}

49

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

87

88pub async fn get_favorites(req: tide::Request<()>) -> tide::Result {

89 let account = req.ext::<model::Account>().unwrap();

90 let sql = format!(

91 r#"

92 SELECT

93 station.*,

94 true as is_favorite

95 FROM station

96 LEFT JOIN favorite

97 ON station.id = favorite.station_id

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

108

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

115 favorite

116 (account_id, station_id)

117 VALUES

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

127

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

134 favorite

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