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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
use serde::de::{Deserialize, Deserializer};
use serde_json;
use std::fmt;
use std::ops::Range;

use query::Query;
use search::SearchPage;
use {Client, Error, HlsPlaylist, Media, Result, Streamable};

/// A work of music contained on a Subsonic server.
#[derive(Debug, Clone)]
pub struct Song {
    /// Unique identifier for the song.
    pub id: u64,
    /// Title of the song. Prefers the song's ID3 tags, but will fall back to
    /// the file name.
    pub title: String,
    /// Album the song belongs to. Reads from the song's ID3 tags.
    pub album: Option<String>,
    /// The ID of the released album.
    album_id: Option<u64>,
    /// Credited artist for the song. Reads from the song's ID3 tags.
    pub artist: Option<String>,
    /// The ID of the releasing artist.
    artist_id: Option<u64>,
    /// Position of the song in the album.
    pub track: Option<u64>,
    /// Year the song was released.
    pub year: Option<u64>,
    /// Genre of the song.
    pub genre: Option<String>,
    /// ID of the song's cover art. Defaults to the parent album's cover.
    cover_id: Option<String>,
    /// File size of the song, in bytes.
    pub size: u64,
    /// An audio MIME type.
    content_type: String,
    /// The file extension of the song.
    suffix: String,
    /// The MIME type that the song will be transcoded to.
    transcoded_content_type: Option<String>,
    /// The file extension that the song will be transcoded to.
    transcoded_suffix: Option<String>,
    /// Duration of the song, in seconds.
    pub duration: Option<u64>,
    /// The absolute path of the song in the server database.
    path: String,
    /// Will always be "song".
    media_type: String,
    /// Bit rate the song will be downsampled to.
    stream_br: Option<usize>,
    /// Format the song will be transcoded to.
    stream_tc: Option<String>,
}

impl Song {
    /// Returns a single song from the Subsonic server.
    ///
    /// # Errors
    ///
    /// Aside from other errors the `Client` may cause, the server will return
    /// an error if there is no song matching the provided ID.
    pub fn get(client: &Client, id: u64) -> Result<Song> {
        let res = client.get("getSong", Query::with("id", id))?;
        Ok(serde_json::from_value(res)?)
    }

    /// Returns a number of random songs similar to this one.
    ///
    /// last.fm suggests a number of similar songs to the one the method is
    /// called on. Optionally takes a `count` to specify the maximum number of
    /// results to return.
    pub fn similar<U>(&self, client: &Client, count: U) -> Result<Vec<Song>>
    where
        U: Into<Option<usize>>,
    {
        let args = Query::with("id", self.id)
            .arg("count", count.into())
            .build();

        let song = client.get("getSimilarSongs2", args)?;
        Ok(get_list_as!(song, Song))
    }

    /// Returns a number of random songs. Optionally accepts a maximum number
    /// of results to return.
    ///
    /// Some parts of the query can be modified. Use [`random_with`] to be able
    /// to set these optional fields.
    ///
    /// [`random_with`]: #method.random_with
    pub fn random<U>(client: &Client, size: U) -> Result<Vec<Song>>
    where
        U: Into<Option<usize>>,
    {
        let arg = Query::with("size", size.into().unwrap_or(10));
        let song = client.get("getRandomSongs", arg)?;
        Ok(get_list_as!(song, Song))
    }

    /// Creates a new builder to request a set of random songs.
    ///
    /// See the [struct level documentation] for more information on how to use
    /// the builder.
    ///
    /// [struct level documentation]: ./struct.RandomSongs.html
    pub fn random_with<'a>(client: &Client) -> RandomSongs {
        RandomSongs::new(client, 10)
    }

    /// Lists all the songs in a provided genre. Supports paging through the
    /// result.
    ///
    /// See the [struct level documentation] about paging for more.
    ///
    /// [struct level documentation]: ../search/struct.SearchPage.html
    pub fn list_in_genre<U>(
        client: &Client,
        genre: &str,
        page: SearchPage,
        folder_id: U,
    ) -> Result<Vec<Song>>
    where
        U: Into<Option<u64>>,
    {
        let args = Query::with("genre", genre)
            .arg("count", page.count)
            .arg("offset", page.offset)
            .arg("musicFolderId", folder_id.into())
            .build();

        let song = client.get("getSongsByGenre", args)?;
        Ok(get_list_as!(song, Song))
    }

    /// Creates an HLS (HTTP Live Streaming) playlist used for streaming video
    /// or audio. HLS is a streaming protocol implemented by Apple and works by
    /// breaking the overall stream into a sequence of small HTTP-based file
    /// downloads. It's supported by iOS and newer versions of Android.
    ///
    ///  Returns an M3U8 playlist on success (content type
    ///  "application/vnd.apple.mpegurl").
    ///
    /// The method also supports adaptive streaming; when supplied with multiple
    /// bit rates, the server will create a variable playlist, suitable for
    /// adaptive bitrate streaming. The playlist will support streaming at all
    /// the specified bitrates. The `bit_rate` parameter can be omitted (with an
    /// empty array) to disable adaptive streaming, or given a single value to
    /// force streaming at that bit rate.
    pub fn hls(&self, client: &Client, bit_rates: &[u64]) -> Result<HlsPlaylist> {
        let args = Query::with("id", self.id)
            .arg_list("bitrate", bit_rates)
            .build();

        let raw = client.get_raw("hls", args)?;
        Ok(raw.parse::<HlsPlaylist>()?)
    }
}

impl Streamable for Song {
    fn stream(&self, client: &Client) -> Result<Vec<u8>> {
        let mut q = Query::with("id", self.id);
        q.arg("maxBitRate", self.stream_br);
        client.get_bytes("stream", q)
    }

    fn stream_url(&self, client: &Client) -> Result<String> {
        let mut q = Query::with("id", self.id);
        q.arg("maxBitRate", self.stream_br);
        client.build_url("stream", q)
    }

    fn download(&self, client: &Client) -> Result<Vec<u8>> {
        client.get_bytes("download", Query::with("id", self.id))
    }

    fn download_url(&self, client: &Client) -> Result<String> {
        client.build_url("download", Query::with("id", self.id))
    }

    fn encoding(&self) -> &str {
        self.transcoded_content_type
            .as_ref()
            .unwrap_or(&self.content_type)
    }

    fn set_max_bit_rate(&mut self, bit_rate: usize) {
        self.stream_br = Some(bit_rate);
    }

    fn set_transcoding(&mut self, format: &str) {
        self.stream_tc = Some(format.to_string());
    }
}

impl Media for Song {
    fn has_cover_art(&self) -> bool {
        self.cover_id.is_some()
    }

    fn cover_id(&self) -> Option<&str> {
        self.cover_id.as_ref().map(|s| s.as_str())
    }

    fn cover_art<U: Into<Option<usize>>>(&self, client: &Client, size: U) -> Result<Vec<u8>> {
        let cover = self
            .cover_id()
            .ok_or_else(|| Error::Other("no cover art found"))?;
        let query = Query::with("id", cover).arg("size", size.into()).build();

        client.get_bytes("getCoverArt", query)
    }

    fn cover_art_url<U: Into<Option<usize>>>(&self, client: &Client, size: U) -> Result<String> {
        let cover = self
            .cover_id()
            .ok_or_else(|| Error::Other("no cover art found"))?;
        let query = Query::with("id", cover).arg("size", size.into()).build();

        client.build_url("getCoverArt", query)
    }
}

impl fmt::Display for Song {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if let Some(ref artist) = self.artist {
            write!(f, "{} - ", artist)?;
        } else {
            write!(f, "Unknown Artist - ")?;
        }

        if let Some(ref album) = self.album {
            write!(f, "{}", album)?;
        } else {
            write!(f, "Unknown Album")?;
        }

        if let Some(year) = self.year {
            write!(f, " [{}]", year)?;
        }

        write!(f, " - {}", self.title)?;

        Ok(())
    }
}

impl<'de> Deserialize<'de> for Song {
    fn deserialize<D>(de: D) -> ::std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Debug, Deserialize)]
        #[serde(rename_all = "camelCase")]
        struct _Song {
            id: String,
            parent: String,
            is_dir: bool,
            title: String,
            album: Option<String>,
            artist: Option<String>,
            track: Option<u64>,
            year: Option<u64>,
            genre: Option<String>,
            cover_art: Option<String>,
            size: u64,
            content_type: String,
            suffix: String,
            transcoded_content_type: Option<String>,
            transcoded_suffix: Option<String>,
            duration: Option<u64>,
            bit_rate: Option<u64>,
            path: String,
            is_video: Option<bool>,
            play_count: u64,
            disc_number: Option<u64>,
            created: String,
            album_id: Option<String>,
            artist_id: Option<String>,
            #[serde(rename = "type")]
            media_type: String,
        }

        let raw = _Song::deserialize(de)?;

        Ok(Song {
            id: raw.id.parse().unwrap(),
            title: raw.title,
            album: raw.album,
            album_id: raw.album_id.map(|i| i.parse().unwrap()),
            artist: raw.artist,
            artist_id: raw.artist_id.map(|i| i.parse().unwrap()),
            cover_id: raw.cover_art,
            track: raw.track,
            year: raw.year,
            genre: raw.genre,
            size: raw.size,
            content_type: raw.content_type,
            suffix: raw.suffix,
            transcoded_content_type: raw.transcoded_content_type,
            transcoded_suffix: raw.transcoded_suffix,
            duration: raw.duration,
            path: raw.path,
            media_type: raw.media_type,
            stream_br: None,
            stream_tc: None,
        })
    }
}

/// A struct matching a lyric search result.
#[derive(Debug, Deserialize)]
pub struct Lyrics {
    /// Title of the song.
    pub title: String,
    /// Artist that performed the song.
    pub artist: String,
    /// Lyrics to the song.
    #[serde(rename = "value")]
    pub lyrics: String,
}

/// A builder struct for a query of random songs.
///
/// A `RandomSongs` can only be created with [`Song::random_with`]. This allows
/// customisation of the results to return.
///
/// The builder holds an internal reference of the client that it will query
/// using, so there's no need to provide it with one when sending the query.
///
/// If you don't need to customise a query and just need a set of random songs,
/// use [`Song::random`] instead, as it skips constructing the builder and
/// directly queries the Subsonic server.
///
/// [`Song::random_with`]: ./struct.Song.html#method.random_with
/// [`Song::random`]: ./struct.Song.html#method.random
///
/// # Examples
///
/// ```no_run
/// extern crate sunk;
/// use sunk::song::Song;
/// use sunk::Client;
///
/// # fn run() -> sunk::Result<()> {
/// # let site = "http://demo.subsonic.org";
/// # let user = "guest3";
/// # let password = "guest";
/// let client = Client::new(site, user, password)?;
///
/// // Get 25 songs from the last 10 years
/// let random = Song::random_with(&client)
///     .size(25)
///     .in_years(2008 .. 2018)
///     .request()?;
/// # Ok(())
/// # }
/// # fn main() { }
/// ```
#[derive(Debug)]
pub struct RandomSongs<'a> {
    client: &'a Client,
    size: usize,
    genre: Option<&'a str>,
    from_year: Option<usize>,
    to_year: Option<usize>,
    folder_id: Option<usize>,
}

impl<'a> RandomSongs<'a> {
    fn new(client: &'a Client, n: usize) -> RandomSongs<'a> {
        RandomSongs {
            client,
            size: n,
            genre: None,
            from_year: None,
            to_year: None,
            folder_id: None,
        }
    }

    /// Sets the number of songs to return.
    pub fn size(&mut self, n: usize) -> &mut RandomSongs<'a> {
        self.size = n;
        self
    }

    /// Sets the genre that songs will be in.
    ///
    /// Genres will vary between Subsonic instances, but can be found using the
    /// [`Client::genres`] method.
    ///
    /// [`Client::genres`]: ../struct.Client.html#method.genres
    pub fn genre(&mut self, genre: &'a str) -> &mut RandomSongs<'a> {
        self.genre = Some(genre);
        self
    }

    /// Sets a lower bound on the year that songs were released in.
    pub fn from_year(&mut self, year: usize) -> &mut RandomSongs<'a> {
        self.from_year = Some(year);
        self
    }

    /// Sets an upper bound on the year that songs were released in.
    pub fn to_year(&mut self, year: usize) -> &mut RandomSongs<'a> {
        self.to_year = Some(year);
        self
    }

    /// Sets both the lower and upper year bounds using a range.
    ///
    /// The range is set *inclusive* at both ends, unlike a standard Rust
    /// range. For example, a range `2013..2016` will return songs that
    /// were released in 2013, 2014, 2015, and 2016.
    pub fn in_years(&mut self, years: Range<usize>) -> &mut RandomSongs<'a> {
        self.from_year = Some(years.start);
        self.to_year = Some(years.end);
        self
    }

    /// Sets the folder index that songs must be in.
    ///
    /// Music folders are zero-indexed, and there will always be index `0`
    /// (provided the server is configured at all) . A list of music
    /// folders can be found using the [`Client::music_folders`] method.
    ///
    /// [`Client::music_folders`]: ../struct.Client.html#method.music_folders
    pub fn in_folder(&mut self, id: usize) -> &mut RandomSongs<'a> {
        self.folder_id = Some(id);
        self
    }

    /// Issues the query to the Subsonic server. Returns a list of random
    /// songs, modified by the builder.
    pub fn request(&mut self) -> Result<Vec<Song>> {
        let args = Query::with("size", self.size)
            .arg("genre", self.genre)
            .arg("fromYear", self.from_year)
            .arg("toYear", self.to_year)
            .arg("musicFolderId", self.folder_id)
            .build();

        let song = self.client.get("getRandomSongs", args)?;
        Ok(get_list_as!(song, Song))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use test_util;

    #[test]
    fn parse_song() {
        let parsed = serde_json::from_value::<Song>(raw()).unwrap();

        assert_eq!(parsed.id, 27);
        assert_eq!(parsed.title, String::from("Bellevue Avenue"));
        assert_eq!(parsed.track, Some(1));
    }

    #[test]
    fn get_hls() {
        let mut srv = test_util::demo_site().unwrap();
        let song = serde_json::from_value::<Song>(raw()).unwrap();

        let hls = song.hls(&mut srv, &[]).unwrap();
        assert_eq!(hls.len(), 20)
    }

    fn raw() -> serde_json::Value {
        serde_json::from_str(
            r#"{
            "id" : "27",
            "parent" : "25",
            "isDir" : false,
            "title" : "Bellevue Avenue",
            "album" : "Bellevue",
            "artist" : "Misteur Valaire",
            "track" : 1,
            "genre" : "(255)",
            "coverArt" : "25",
            "size" : 5400185,
            "contentType" : "audio/mpeg",
            "suffix" : "mp3",
            "duration" : 198,
            "bitRate" : 216,
            "path" : "Misteur Valaire/Bellevue/01 - Misteur Valaire - Bellevue Avenue.mp3",
            "averageRating" : 3.0,
            "playCount" : 706,
            "created" : "2017-03-12T11:07:27.000Z",
            "starred" : "2017-06-01T19:48:25.635Z",
            "albumId" : "1",
            "artistId" : "1",
            "type" : "music"
        }"#,
        )
        .unwrap()
    }
}