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
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;

extern crate reqwest;

use std::collections::HashMap;

#[derive(Serialize, Deserialize, Debug)]
pub struct AceResponse<T> {
    pub response: T,
    error: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct AceResult<T> {
    pub result: T,
    error: Option<String>,
}

#[derive(Debug)]
pub struct Engine {
    pub engine_url: reqwest::Url,
    pub streams: HashMap<String, Stream>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Player {
    pub protocol: Option<String>,
    pub icon: Option<String>,
    #[serde(rename = "type")]
    pub type_name: Option<String>,
    pub id: Option<String>,
    pub name: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Stream {
    command_url: Option<String>,
    is_live: Option<usize>,
    playback_session_id: Option<String>,
    playback_url: Option<String>,
    stat_url: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Stat {
    downloaded: Option<usize>,
    pub peers: Option<usize>,
    playback_session_id: Option<String>,
    progress: Option<usize>,
    pub speed_down: Option<usize>,
    pub speed_up: Option<usize>,
    pub status: Option<String>,
    time: Option<usize>,
    total_progress: Option<usize>,

    uploaded: Option<usize>,
}

impl Default for Engine {
    fn default() -> Self {
        Self::new("http://127.0.0.1:6878")
    }
}

impl Engine {
    pub fn new(http_link: &str) -> Self {
        Engine {
            engine_url: reqwest::Url::parse(http_link).unwrap(),
            streams: HashMap::new(),
        }
    }

    pub fn is_up(&self) -> bool {
        reqwest::get(self.engine_url.clone()).is_ok()
    }

    pub fn version(&self) -> Result<serde_json::Value, reqwest::Error> {
        let mut map = HashMap::new();
        map.insert("method".to_owned(), "get_version".to_owned());
        map.insert("format".to_owned(), "json".to_owned());
        let url = self.build_url("webui/api/service", &map);
        let mut resp = reqwest::get(url).unwrap();
        resp.json()
    }

    pub fn build_url(&self, path: &str, queries: &HashMap<String, String>) -> reqwest::Url {
        let mut url = self.engine_url.join(path).unwrap();
        {
            let mut query_pairs = url.query_pairs_mut();
            for (ref k, ref v) in queries.iter() {
                query_pairs.append_pair(k.as_str(), v.as_str());
            }
        }
        url
    }

    pub fn get_stream(&self, id: &str) -> Stream {
        let mut map = HashMap::new();
        map.insert("id".to_owned(), id.to_owned());
        map.insert("format".to_owned(), "json".to_owned());
        let url = self.build_url("ace/getstream", &map);
        let mut resp = reqwest::get(url).unwrap();
        resp.json::<AceResponse<Stream>>().unwrap().response
    }

    pub fn add_stream(&mut self, id: &str) {
        let stream = self.get_stream(id);
        self.streams.insert(id.to_owned(), stream);
    }

    pub fn get_stream_link(&self, id: &str) -> String {
        let stream = &self.streams[id];
        stream.playback_url.as_ref().unwrap().to_owned()
    }

    pub fn is_stream_live(&self, id: &str) -> bool {
        let stream = &self.streams[id];
        stream.is_live.unwrap() > 0
    }

    pub fn get_stream_stat(&self, id: &str) -> Stat {
        let stream = &self.streams[id];
        let url = stream.stat_url.as_ref().unwrap();
        let mut resp = reqwest::get(url).unwrap();
        resp.json::<AceResponse<Stat>>().unwrap().response
    }

    pub fn stop_stream(&self, id: &str) -> String {
        let stream = &self.streams[id];
        let mut url = reqwest::Url::parse(stream.command_url.as_ref().unwrap()).unwrap();
        url.query_pairs_mut().append_pair("method", "stop");
        let mut resp = reqwest::get(url).unwrap();
        resp.json::<AceResponse<String>>().unwrap().response
    }

    pub fn get_players(&self) -> Vec<Player> {
        let mut map = HashMap::new();
        map.insert("method".to_owned(), "get_available_players".to_owned());
        map.insert(
            "content_id".to_owned(),
            "94c2fd8fb9bc8f2fc71a2cbe9d4b866f227a0209".to_owned(),
        );
        map.insert("format".to_owned(), "json".to_owned());
        let url = self.build_url("server/api", &map);
        let mut resp = reqwest::get(url).unwrap();
        resp.json::<AceResult<HashMap<String, Vec<Player>>>>()
            .unwrap()
            .result
            .remove("players")
            .unwrap()
    }

    pub fn play_on_player(&self, id: &str, player_id: &str) -> AceResult<String> {
        let mut map = HashMap::new();
        map.insert("method".to_owned(), "open_in_player".to_owned());
        map.insert("content_id".to_owned(), id.to_owned());
        map.insert("player_id".to_owned(), player_id.to_owned());
        map.insert("format".to_owned(), "json".to_owned());
        let url = self.build_url("server/api", &map);
        let mut resp = reqwest::get(url).unwrap();
        resp.json::<AceResult<String>>().unwrap()
    }
}