spotify-rs 0.4.1

A Rust wrapper for the Spotify API.
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
use std::{collections::HashMap, fmt::Debug, marker::PhantomData};

use serde::{ser::SerializeMap, Serialize};
use strum::IntoStaticStr;

use crate::{
    auth::{AuthFlow, Authorised},
    body_list,
    error::Result,
    model::{
        audio::{AudioAnalysis, AudioFeatures, AudioFeaturesList, Mode},
        recommendation::Recommendations,
        track::{SavedTrack, Track, Tracks},
        Page,
    },
    query_list, Nil,
};

use super::{Client, Endpoint};

pub fn track(id: impl Into<String>) -> TrackEndpoint {
    TrackEndpoint {
        id: id.into(),
        market: None,
    }
}

pub fn tracks<T: AsRef<str>>(ids: &[T]) -> TracksEndpoint {
    TracksEndpoint {
        ids: query_list(ids),
        market: None,
    }
}

pub fn saved_tracks() -> SavedTracksEndpoint {
    SavedTracksEndpoint::default()
}

pub async fn save_tracks<T: AsRef<str>>(
    ids: &[T],
    spotify: &Client<impl AuthFlow + Authorised>,
) -> Result<Nil> {
    spotify
        .put("/me/tracks".to_owned(), body_list("ids", ids))
        .await
}

pub async fn remove_saved_tracks<T: AsRef<str>>(
    ids: &[T],
    spotify: &Client<impl AuthFlow + Authorised>,
) -> Result<Nil> {
    spotify
        .delete("/me/tracks".to_owned(), body_list("ids", ids))
        .await
}

pub async fn check_saved_tracks<T: AsRef<str>>(
    ids: &[T],
    spotify: &Client<impl AuthFlow + Authorised>,
) -> Result<Vec<bool>> {
    spotify
        .get("/me/tracks/contains".to_owned(), [("ids", query_list(ids))])
        .await
}

/// **Note:** This endpoint has been deprecated by Spotify. It continues to work for
/// applications already using the extended mode in the API.
///
/// You can read more about this [here](https://developer.spotify.com/blog/2024-11-27-changes-to-the-web-api).
pub async fn get_track_audio_features(
    id: impl Into<String>,
    spotify: &Client<impl AuthFlow>,
) -> Result<AudioFeatures> {
    spotify
        .get::<(), _>(format!("/audio-features/{}", id.into()), None)
        .await
}

/// **Note:** This endpoint has been deprecated by Spotify. It continues to work for
/// applications already using the extended mode in the API.
///
/// You can read more about this [here](https://developer.spotify.com/blog/2024-11-27-changes-to-the-web-api).
pub async fn get_tracks_audio_features<T: AsRef<str>>(
    ids: &[T],
    spotify: &Client<impl AuthFlow>,
) -> Result<Vec<Option<AudioFeatures>>> {
    spotify
        .get("/audio-features".to_owned(), [("ids", query_list(ids))])
        .await
        .map(|a: AudioFeaturesList| a.audio_features)
}

pub async fn get_track_audio_analysis(
    id: impl Into<String>,
    spotify: &Client<impl AuthFlow>,
) -> Result<AudioAnalysis> {
    spotify
        .get::<(), _>(format!("/audio-analysis/{}", id.into()), None)
        .await
}

/// Get recommendations based on given seeds. You must specify at least one
/// seed (whether that be a seed artist, track or genre). More seed types can
/// be used optionally via the builder.
///
/// **Note:** This endpoint has been deprecated by Spotify. It continues to work for
/// applications already using the extended mode in the API.
///
/// You can read more about this [here](https://developer.spotify.com/blog/2024-11-27-changes-to-the-web-api).
#[doc = include_str!("../docs/seed_limit.md")]
pub fn recommendations<S: SeedType, T: AsRef<str>>(seed: Seed<T, S>) -> RecommendationsEndpoint<S> {
    let (seed_artists, seed_genres, seed_tracks) = match seed {
        Seed::Artists(ids, _) => (Some(query_list(ids)), None, None),
        Seed::Genres(genres, _) => (None, Some(query_list(genres)), None),
        Seed::Tracks(ids, _) => (None, None, Some(query_list(ids))),
    };

    RecommendationsEndpoint {
        seed_artists,
        seed_genres,
        seed_tracks,
        limit: None,
        market: None,
        features: None,
        marker: std::marker::PhantomData,
    }
}

impl Endpoint for TrackEndpoint {}
impl Endpoint for TracksEndpoint {}
impl Endpoint for SavedTracksEndpoint {}
impl<S: SeedType> Endpoint for RecommendationsEndpoint<S> {}

pub trait SeedType: Debug {}
impl SeedType for SeedArtists {}
impl SeedType for SeedGenres {}
impl SeedType for SeedTracks {}

#[derive(Clone, Copy, Debug)]
pub enum SeedArtists {}
#[derive(Clone, Copy, Debug)]
pub enum SeedGenres {}
#[derive(Clone, Copy, Debug)]
pub enum SeedTracks {}

#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum Seed<'a, T: AsRef<str>, S: SeedType> {
    Artists(&'a [T], PhantomData<S>),
    Genres(&'a [T], PhantomData<S>),
    Tracks(&'a [T], PhantomData<S>),
}

impl<'a, T: AsRef<str> + Clone> Seed<'a, T, SeedArtists> {
    #[doc = include_str!("../docs/seed_limit.md")]
    pub fn artists(ids: &'a [T]) -> Self {
        Self::Artists(ids, PhantomData)
    }
}

impl<'a, T: AsRef<str> + Clone> Seed<'a, T, SeedGenres> {
    #[doc = include_str!("../docs/seed_limit.md")]
    pub fn genres(genres: &'a [T]) -> Self {
        Self::Genres(genres, PhantomData)
    }
}

impl<'a, T: AsRef<str> + Clone> Seed<'a, T, SeedTracks> {
    #[doc = include_str!("../docs/seed_limit.md")]
    pub fn tracks(ids: &'a [T]) -> Self {
        Self::Tracks(ids, PhantomData)
    }
}

// #[derive(Clone, Copy, Debug, Serialize, IntoStaticStr)]
// #[serde(untagged)]
// #[serde(rename_all = "snake_case")]
// #[strum(serialize_all = "snake_case")]
// pub enum Feature {
//     MinAcousticness(f32),
//     MaxAcousticness(f32),
//     TargetAcousticness(f32),
//     MinDanceability(f32),
//     MaxDanceability(f32),
//     TargetDanceability(f32),
//     MinDurationMs(u32),
//     MaxDurationMs(u32),
//     TargetDurationMs(u32),
//     MinEnergy(f32),
//     MaxEnergy(f32),
//     TargetEnergy(f32),
//     MinInstrumentalness(f32),
//     MaxInstrumentalness(f32),
//     TargetInstrumentalness(f32),
//     MinKey(u32),
//     MaxKey(u32),
//     TargetKey(u32),
//     MinLiveness(f32),
//     MaxLiveness(f32),
//     TargetLiveness(f32),
//     MinLoudness(f32),
//     MaxLoudness(f32),
//     TargetLoudness(f32),
//     MinMode(u32),
//     MaxMode(u32),
//     TargetMode(u32),
//     MinPopularity(u32),
//     MaxPopularity(u32),
//     TargetPopularity(u32),
//     MinSpeechiness(f32),
//     MaxSpeechiness(f32),
//     TargetSpeechiness(f32),
//     MinTempo(f32),
//     MaxTempo(f32),
//     TargetTempo(f32),
//     MinTimeSignature(u32),
//     MaxTimeSignature(u32),
//     TargetTimeSignature(u32),
//     MinValence(f32),
//     MaxValence(f32),
//     TargetValence(f32),
// }

/// Represents what feature exactly is set.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, IntoStaticStr)]
#[strum(serialize_all = "snake_case")]
pub enum FeatureKind {
    Acousticness,
    Danceability,
    DurationMs,
    Energy,
    Instrumentalness,
    Key,
    Liveness,
    Loudness,
    Mode,
    Popularity,
    Speechiness,
    Tempo,
    TimeSignature,
    Valence,
}

/// Represents the value of a feature. They can be either floats or values.
///
/// The value can be directly specified (e.g. 0, 100, 0.5, Mode::Major etc.), as this type
/// implements `From` for various type.
///
/// **Note:** You *must* use the right values yourself for each option. They can either take
/// integers from 0 - 100, floats from 0 - 1, or special types (like [`Mode`](crate::model::audio::Mode))
///
/// In order to know what values each feature takes, you can use
/// [this](https://developer.spotify.com/documentation/web-api/reference/get-recommendations)
/// page.
///
/// # Example
///
/// ```rs
/// let recommendations = recommendations(Seed::artists(&["59XQUEHhy5830QsAsmhe2M"]))
/// .features(&[
///     Feature::min(FeatureKind::Energy, 0.5),
///     Feature::max(FeatureKind::Popularity, 64),
///     Feature::target(FeatureKind::Mode, Mode::Major),
/// ])
/// .get(spotify)
/// .await?;
/// ```
#[derive(Clone, Copy, Debug, Serialize)]
#[serde(untagged)]
pub enum FeatureValue {
    Float(f32),
    Int(u32),
}

impl From<Vec<Feature>> for Features {
    fn from(value: Vec<Feature>) -> Self {
        Self(value)
    }
}

impl<const N: usize> From<[Feature; N]> for Features {
    fn from(value: [Feature; N]) -> Self {
        Self(value.to_vec())
    }
}

impl From<&[Feature]> for Features {
    fn from(value: &[Feature]) -> Self {
        Self(value.to_vec())
    }
}

/// Represesnts a list of features. You can use an array or vector instead of it,
/// as this type implements the `From` trait.
#[derive(Clone, Debug, Default)]
pub struct Features(Vec<Feature>);

impl Serialize for Features {
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut map = HashMap::new();

        for element in &self.0 {
            let kind: &'static str = element.kind.into();

            if let Some(target) = element.target {
                map.insert(format!("target_{kind}"), target);
            }

            if let Some(min) = element.min {
                map.insert(format!("min_{kind}"), min);
            }

            if let Some(max) = element.max {
                map.insert(format!("max_{kind}"), max);
            }
        }

        let mut serialized_map = serializer.serialize_map(Some(map.len()))?;

        for (k, v) in map {
            serialized_map.serialize_entry(&k, &v)?
        }

        serialized_map.end()
    }
}

#[derive(Clone, Copy, Debug)]
pub struct Feature {
    kind: FeatureKind,
    target: Option<FeatureValue>,
    min: Option<FeatureValue>,
    max: Option<FeatureValue>,
}

impl From<u32> for FeatureValue {
    fn from(value: u32) -> Self {
        Self::Int(value)
    }
}

impl From<f32> for FeatureValue {
    fn from(value: f32) -> Self {
        Self::Float(value)
    }
}

impl From<Mode> for FeatureValue {
    fn from(value: Mode) -> Self {
        Self::Int(value as u32)
    }
}

impl Feature {
    pub fn new<T: Into<FeatureValue>>(
        kind: FeatureKind,
        target: Option<T>,
        min: Option<T>,
        max: Option<T>,
    ) -> Self {
        Self {
            kind,
            target: target.map(Into::into),
            min: min.map(Into::into),
            max: max.map(Into::into),
        }
    }

    pub fn target<T: Into<FeatureValue>>(kind: FeatureKind, target: T) -> Self {
        Self {
            kind,
            target: Some(target.into()),
            min: None,
            max: None,
        }
    }

    pub fn min<T: Into<FeatureValue>>(kind: FeatureKind, min: T) -> Self {
        Self {
            kind,
            target: None,
            min: Some(min.into()),
            max: None,
        }
    }

    pub fn max<T: Into<FeatureValue>>(kind: FeatureKind, max: T) -> Self {
        Self {
            kind,
            target: None,
            min: None,
            max: Some(max.into()),
        }
    }

    pub fn exact<T: Into<FeatureValue>>(kind: FeatureKind, value: T) -> Self {
        let value = value.into();

        Self {
            kind,
            target: Some(value),
            min: Some(value),
            max: Some(value),
        }
    }
}

#[derive(Clone, Debug, Default, Serialize)]
pub struct TrackEndpoint {
    #[serde(skip)]
    pub(crate) id: String,
    pub(crate) market: Option<String>,
}

impl TrackEndpoint {
    #[doc = include_str!("../docs/market.md")]
    pub fn market(mut self, market: impl Into<String>) -> Self {
        self.market = Some(market.into());
        self
    }

    #[doc = include_str!("../docs/send.md")]
    pub async fn get(self, spotify: &Client<impl AuthFlow>) -> Result<Track> {
        spotify.get(format!("/tracks/{}", self.id), self).await
    }
}
#[derive(Clone, Debug, Default, Serialize)]
pub struct TracksEndpoint {
    pub(crate) ids: String,
    pub(crate) market: Option<String>,
}

impl TracksEndpoint {
    #[doc = include_str!("../docs/market.md")]
    pub fn market(mut self, market: impl Into<String>) -> Self {
        self.market = Some(market.into());
        self
    }

    #[doc = include_str!("../docs/send.md")]
    pub async fn get(self, spotify: &Client<impl AuthFlow>) -> Result<Vec<Track>> {
        spotify
            .get("/tracks".to_owned(), self)
            .await
            .map(|t: Tracks| t.tracks)
    }
}

#[derive(Clone, Debug, Default, Serialize)]
pub struct SavedTracksEndpoint {
    pub(crate) market: Option<String>,
    pub(crate) limit: Option<u32>,
    pub(crate) offset: Option<u32>,
}

impl SavedTracksEndpoint {
    #[doc = include_str!("../docs/market.md")]
    pub fn market(mut self, market: impl Into<String>) -> Self {
        self.market = Some(market.into());
        self
    }

    #[doc = include_str!("../docs/limit.md")]
    pub fn limit(mut self, limit: u32) -> Self {
        self.limit = Some(limit);
        self
    }

    #[doc = include_str!("../docs/offset.md")]
    pub fn offset(mut self, offset: u32) -> Self {
        self.offset = Some(offset);
        self
    }

    #[doc = include_str!("../docs/send.md")]
    pub async fn get(
        self,
        spotify: &Client<impl AuthFlow + Authorised>,
    ) -> Result<Page<SavedTrack>> {
        spotify.get("/me/tracks".to_owned(), self).await
    }
}

#[derive(Clone, Debug, Default, Serialize)]
pub struct RecommendationsEndpoint<S: SeedType> {
    pub(crate) seed_artists: Option<String>,
    pub(crate) seed_genres: Option<String>,
    pub(crate) seed_tracks: Option<String>,
    pub(crate) limit: Option<u32>,
    pub(crate) market: Option<String>,
    #[serde(flatten)]
    pub(crate) features: Option<Features>,
    #[serde(skip)]
    pub(crate) marker: PhantomData<S>,
}

impl RecommendationsEndpoint<SeedArtists> {
    #[doc = include_str!("../docs/seed_limit.md")]
    pub fn seed_genres<T: AsRef<str>>(mut self, genres: &[T]) -> Self {
        self.seed_genres = Some(query_list(genres));
        self
    }

    #[doc = include_str!("../docs/seed_limit.md")]
    pub fn seed_tracks<T: AsRef<str>>(mut self, track_ids: &[T]) -> Self {
        self.seed_tracks = Some(query_list(track_ids));
        self
    }
}

impl RecommendationsEndpoint<SeedGenres> {
    #[doc = include_str!("../docs/seed_limit.md")]
    pub fn seed_artists<T: AsRef<str>>(mut self, artist_ids: &[T]) -> Self {
        self.seed_genres = Some(query_list(artist_ids));
        self
    }

    #[doc = include_str!("../docs/seed_limit.md")]
    pub fn seed_tracks<T: AsRef<str>>(mut self, track_ids: &[T]) -> Self {
        self.seed_tracks = Some(query_list(track_ids));
        self
    }
}

impl RecommendationsEndpoint<SeedTracks> {
    #[doc = include_str!("../docs/seed_limit.md")]
    pub fn seed_genres<T: AsRef<str>>(mut self, genres: &[T]) -> Self {
        self.seed_genres = Some(query_list(genres));
        self
    }

    #[doc = include_str!("../docs/seed_limit.md")]
    pub fn seed_artists<T: AsRef<str>>(mut self, artist_ids: &[T]) -> Self {
        self.seed_genres = Some(query_list(artist_ids));
        self
    }
}

impl<S: SeedType> RecommendationsEndpoint<S> {
    #[doc = include_str!("../docs/limit.md")]
    pub fn limit(mut self, limit: u32) -> Self {
        self.limit = Some(limit);
        self
    }

    #[doc = include_str!("../docs/market.md")]
    pub fn market(mut self, market: impl Into<String>) -> Self {
        self.market = Some(market.into());
        self
    }

    /// A list of [`Features`](Feature). Read more about the available features
    /// [here](https://developer.spotify.com/documentation/web-api/reference/get-recommendations).
    pub fn features(mut self, features: &[Feature]) -> Self {
        self.features = Some(features.into());
        self
    }

    #[doc = include_str!("../docs/send.md")]
    pub async fn get(self, spotify: &Client<impl AuthFlow>) -> Result<Recommendations> {
        spotify.get("/recommendations".to_owned(), self).await
    }
}