spotify-client 0.1.2

A util crate for Spotify Auth Client
Documentation
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
pub use rspotify::model as rspotify_model;
use rspotify::model::CurrentPlaybackContext;
pub use rspotify::model::{AlbumId, ArtistId, Id, PlaylistId, TrackId, UserId};

use crate::utils::map_join;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;

#[derive(Serialize, Clone, Debug)]
#[serde(untagged)]
/// A Spotify context (playlist, album, artist)
pub enum Context {
    Playlist {
        playlist: Playlist,
        tracks: Vec<Track>,
    },
    Album {
        album: Album,
        tracks: Vec<Track>,
    },
    Artist {
        artist: Artist,
        top_tracks: Vec<Track>,
        albums: Vec<Album>,
        related_artists: Vec<Artist>,
    },
    Tracks {
        tracks: Vec<Track>,
        desc: String,
    },
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TracksId {
    pub uri: String,
    pub kind: String,
}

#[derive(Clone, Debug, PartialEq, Eq)]
/// A context Id
pub enum ContextId {
    Playlist(PlaylistId<'static>),
    Album(AlbumId<'static>),
    Artist(ArtistId<'static>),
    Tracks(TracksId),
}

#[derive(Clone, Debug)]
/// Data used to start a new playback.
/// There are two ways to start a new playback:
/// - Specify the playing context ID with an offset
/// - Specify the list of track IDs with an offset
///
/// An offset can be either a track's URI or its absolute offset in the context
pub enum Playback {
    Context(ContextId, Option<rspotify_model::Offset>),
    URIs(Vec<TrackId<'static>>, Option<rspotify_model::Offset>),
}

#[derive(Default, Clone, Debug, Deserialize, Serialize)]
/// Data returned when searching a query using Spotify APIs.
pub struct SearchResults {
    pub tracks: Vec<Track>,
    pub artists: Vec<Artist>,
    pub albums: Vec<Album>,
    pub playlists: Vec<Playlist>,
}

#[derive(Debug)]
/// A track order
pub enum TrackOrder {
    AddedAt,
    TrackName,
    Album,
    Artists,
    Duration,
}

#[derive(Debug, Clone)]
/// A Spotify item (track, album, artist, playlist)
pub enum Item {
    Track(Track),
    Album(Album),
    Artist(Artist),
    Playlist(Playlist),
}

#[derive(Debug, Clone)]
pub enum ItemId {
    Track(TrackId<'static>),
    Album(AlbumId<'static>),
    Artist(ArtistId<'static>),
    Playlist(PlaylistId<'static>),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PlaybackMetadata {
    pub device_name: String,
    pub device_id: Option<String>,
    pub volume: Option<u32>,
    pub is_playing: bool,
    pub repeat_state: rspotify_model::RepeatState,
    pub shuffle_state: bool,
    pub mute_state: Option<u32>,
    /// Indicate if fake track repeat mode is enabled.
    /// This mode is a workaround for a librespot's [limitation] that doesn't support `track` repeat.
    ///
    /// [limitation]: https://github.com/librespot-org/librespot/issues/19
    pub fake_track_repeat_state: bool,
}

#[derive(Debug, Clone)]
/// A Spotify device
pub struct Device {
    pub id: String,
    pub name: String,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
/// A Spotify track
pub struct Track {
    pub id: TrackId<'static>,
    pub name: String,
    pub artists: Vec<Artist>,
    pub album: Option<Album>,
    pub duration: std::time::Duration,
    pub explicit: bool,
    #[serde(skip)]
    pub added_at: u64,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
/// A Spotify album
pub struct Album {
    pub id: AlbumId<'static>,
    pub release_date: String,
    pub name: String,
    pub artists: Vec<Artist>,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
/// A Spotify artist
pub struct Artist {
    pub id: ArtistId<'static>,
    pub name: String,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
/// A Spotify playlist
pub struct Playlist {
    pub id: PlaylistId<'static>,
    pub collaborative: bool,
    pub name: String,
    pub owner: (String, UserId<'static>),
    pub desc: String,
}

#[derive(Clone, Debug)]
/// A Spotify category
pub struct Category {
    pub id: String,
    pub name: String,
}

impl Context {
    /// gets the context's description
    pub fn description(&self) -> String {
        match self {
            Context::Album {
                ref album,
                ref tracks,
            } => {
                format!(
                    "{} | {} | {} songs",
                    album.name,
                    album.release_date,
                    tracks.len()
                )
            }
            Context::Playlist {
                ref playlist,
                tracks,
            } => {
                format!(
                    "{} | {} | {} songs",
                    playlist.name,
                    playlist.owner.0,
                    tracks.len()
                )
            }
            Context::Artist { ref artist, .. } => artist.name.to_string(),
            Context::Tracks { desc, tracks } => format!("{} | {} songs", desc, tracks.len()),
        }
    }
}

impl ContextId {
    pub fn uri(&self) -> String {
        match self {
            Self::Album(id) => id.uri(),
            Self::Artist(id) => id.uri(),
            Self::Playlist(id) => id.uri(),
            Self::Tracks(id) => id.uri.to_owned(),
        }
    }
}

impl TrackOrder {
    pub fn compare(&self, x: &Track, y: &Track) -> std::cmp::Ordering {
        match *self {
            Self::AddedAt => x.added_at.cmp(&y.added_at),
            Self::TrackName => x.name.cmp(&y.name),
            Self::Album => x.album_info().cmp(&y.album_info()),
            Self::Duration => x.duration.cmp(&y.duration),
            Self::Artists => x.artists_info().cmp(&y.artists_info()),
        }
    }
}

impl Device {
    /// tries to convert from a `rspotify_model::Device` into `Device`
    pub fn try_from_device(device: rspotify_model::Device) -> Option<Self> {
        Some(Self {
            id: device.id?,
            name: device.name,
        })
    }
}

impl Track {
    /// gets the track's artists information
    pub fn artists_info(&self) -> String {
        map_join(&self.artists, |a| &a.name, ", ")
    }

    /// gets the track's album information
    pub fn album_info(&self) -> String {
        self.album
            .as_ref()
            .map(|a| a.name.clone())
            .unwrap_or_default()
    }

    /// gets the track's name, including an explicit label
    pub fn display_name(&self) -> Cow<'_, str> {
        if self.explicit {
            Cow::Owned(format!("{} (E)", self.name))
        } else {
            Cow::Borrowed(self.name.as_str())
        }
    }

    /// tries to convert from a `rspotify_model::SimplifiedTrack` into `Track`
    pub fn try_from_simplified_track(track: rspotify_model::SimplifiedTrack) -> Option<Self> {
        if track.is_playable.unwrap_or(true) {
            let id = match track.linked_from {
                Some(d) => d.id,
                None => track.id?,
            };
            Some(Self {
                id,
                name: track.name,
                artists: from_simplified_artists_to_artists(track.artists),
                album: None,
                duration: track.duration.to_std().expect("valid chrono duration"),
                explicit: track.explicit,
                added_at: 0,
            })
        } else {
            None
        }
    }

    /// tries to convert from a `rspotify_model::FullTrack` into `Track`
    pub fn try_from_full_track(track: rspotify_model::FullTrack) -> Option<Self> {
        if track.is_playable.unwrap_or(true) {
            let id = match track.linked_from {
                Some(d) => d.id,
                None => track.id?,
            };
            Some(Self {
                id,
                name: track.name,
                artists: from_simplified_artists_to_artists(track.artists),
                album: Album::try_from_simplified_album(track.album),
                duration: track.duration.to_std().expect("valid chrono duration"),
                explicit: track.explicit,
                added_at: 0,
            })
        } else {
            None
        }
    }
}

impl std::fmt::Display for Track {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}{}{}",
            self.display_name(),
            self.artists_info(),
            self.album_info(),
        )
    }
}

impl Album {
    /// tries to convert from a `rspotify_model::SimplifiedAlbum` into `Album`
    pub fn try_from_simplified_album(album: rspotify_model::SimplifiedAlbum) -> Option<Self> {
        Some(Self {
            id: album.id?,
            name: album.name,
            release_date: album.release_date.unwrap_or_default(),
            artists: from_simplified_artists_to_artists(album.artists),
        })
    }

    /// gets the album's release year
    pub fn year(&self) -> String {
        self.release_date
            .split('-')
            .next()
            .unwrap_or("")
            .to_string()
    }
}

impl From<rspotify_model::FullAlbum> for Album {
    fn from(album: rspotify_model::FullAlbum) -> Self {
        Self {
            name: album.name,
            id: album.id,
            release_date: album.release_date,
            artists: from_simplified_artists_to_artists(album.artists),
        }
    }
}

impl std::fmt::Display for Album {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}{} ({})",
            self.name,
            map_join(&self.artists, |a| &a.name, ", "),
            self.year()
        )
    }
}

impl Artist {
    /// tries to convert from a `rspotify_model::SimplifiedArtist` into `Artist`
    pub fn try_from_simplified_artist(artist: rspotify_model::SimplifiedArtist) -> Option<Self> {
        Some(Self {
            id: artist.id?,
            name: artist.name,
        })
    }
}

impl From<rspotify_model::FullArtist> for Artist {
    fn from(artist: rspotify_model::FullArtist) -> Self {
        Self {
            name: artist.name,
            id: artist.id,
        }
    }
}

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

/// a helper function to convert a vector of `rspotify_model::SimplifiedArtist`
/// into a vector of `Artist`.
fn from_simplified_artists_to_artists(
    artists: Vec<rspotify_model::SimplifiedArtist>,
) -> Vec<Artist> {
    artists
        .into_iter()
        .filter_map(Artist::try_from_simplified_artist)
        .collect()
}

impl From<rspotify_model::SimplifiedPlaylist> for Playlist {
    fn from(playlist: rspotify_model::SimplifiedPlaylist) -> Self {
        Self {
            id: playlist.id,
            name: playlist.name,
            collaborative: playlist.collaborative,
            owner: (
                playlist.owner.display_name.unwrap_or_default(),
                playlist.owner.id,
            ),
            desc: String::new(),
        }
    }
}

impl From<rspotify_model::FullPlaylist> for Playlist {
    fn from(playlist: rspotify_model::FullPlaylist) -> Self {
        // remove HTML tags from the description
        // TODO: may also need to do HTML escaping here
        let re = regex::Regex::new("(<.*?>|</.*?>)").expect("valid regex");
        let desc = playlist.description.unwrap_or_default();
        let desc = re.replace_all(&desc, "").to_string();

        Self {
            id: playlist.id,
            name: playlist.name,
            collaborative: playlist.collaborative,
            owner: (
                playlist.owner.display_name.unwrap_or_default(),
                playlist.owner.id,
            ),
            desc,
        }
    }
}

impl std::fmt::Display for Playlist {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}{}", self.name, self.owner.0)
    }
}

impl From<rspotify_model::category::Category> for Category {
    fn from(c: rspotify_model::category::Category) -> Self {
        Self {
            name: c.name,
            id: c.id,
        }
    }
}

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

impl TracksId {
    pub fn new<U, K>(uri: U, kind: K) -> Self
        where
            U: Into<String>,
            K: Into<String>,
    {
        Self {
            uri: uri.into(),
            kind: kind.into(),
        }
    }
}

impl Playback {
    /// creates new playback with a specified offset based on the current playback
    pub fn uri_offset(&self, uri: String, limit: usize) -> Self {
        match self {
            Playback::Context(id, _) => {
                Playback::Context(id.clone(), Some(rspotify_model::Offset::Uri(uri)))
            }
            Playback::URIs(ids, _) => {
                let ids = if ids.len() < limit {
                    ids.clone()
                } else {
                    let pos = ids
                        .iter()
                        .position(|id| id.uri() == uri)
                        .unwrap_or_default();
                    let l = pos.saturating_sub(limit / 2);
                    let r = std::cmp::min(l + limit, ids.len());
                    // For a list with too many tracks, to avoid payload limit when making the `start_playback`
                    // API request, we restrict the range of tracks to be played, which is based on the
                    // playing track's position (if any) and the application's limit (`app_config.tracks_playback_limit`).
                    // Related issue: https://github.com/aome510/spotify-player/issues/78
                    ids[l..r].to_vec()
                };

                Playback::URIs(ids, Some(rspotify_model::Offset::Uri(uri)))
            }
        }
    }
}

impl PlaybackMetadata {
    pub fn from_playback(p: &CurrentPlaybackContext) -> Self {
        Self {
            device_name: p.device.name.clone(),
            device_id: p.device.id.clone(),
            is_playing: p.is_playing,
            volume: p.device.volume_percent,
            repeat_state: p.repeat_state,
            shuffle_state: p.shuffle_state,
            mute_state: None,
            fake_track_repeat_state: false,
        }
    }
}