summaryrefslogtreecommitdiff
path: root/src/channels.rs
blob: b89169ff079d8f918d4ca6dae78e5bbfd9f1f5c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use serde::Deserialize;
use std::fmt;

#[derive(Deserialize, Debug)]
pub struct Channel {
    pub id: String,
    pub title: String,
    pub description: String,
    pub image: String,
}

impl fmt::Display for Channel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}: {}", self.title, self.description)
    }
}

#[derive(Deserialize, Debug)]
pub struct Channels {
    pub channels: Vec<Channel>,
}

pub fn get_channels() -> Vec<Channel> {
    let url = "https://api.somafm.com/channels.json";
    let res = reqwest::blocking::get(url).unwrap().text().unwrap();
    let channels: Channels = serde_json::from_str(&res).unwrap();
    channels.channels
}

mod tests {
    #[test]
    fn test_channel_parse() {
        let chan_str = r#"{"id":"7soul","title":"Seven Inch Soul","description":"Vintage soul tracks from the original 45 RPM vinyl.","dj":"Dion Watts Garcia","djmail":"dion@somafm.com","genre":"oldies","image":"https://api.somafm.com/img/7soul120.png","largeimage":"https://api.somafm.com/logos/256/7soul256.png","xlimage":"https://api.somafm.com/logos/512/7soul512.png","twitter":"","updated":"1396144686","playlists":[{"url":"https://api.somafm.com/7soul.pls","format":"mp3","quality":"highest"},{"url":"https://api.somafm.com/7soul130.pls","format":"aac","quality":"highest"},{"url":"https://api.somafm.com/7soul64.pls","format":"aacp","quality":"high"},{"url":"https://api.somafm.com/7soul32.pls","format":"aacp","quality":"low"}],"preroll":[],"listeners":"80","lastPlaying":"Durand Jones & The Indications - Is It Any Wonder"}"#;
        let chan: super::Channel = serde_json::from_str(chan_str).unwrap();
        assert_eq!(chan.id, "7soul");
    }
}