home
NOTE: Under Construction - I'm in the middle of upgrading my site and lots of stuff is kinda broken. Please forgive the mess.

Get Track Features From The Spotify API In Rust

December 2023
Code
//! ```cargo
//! [dependencies]
//! rspotify = { version = "0.12.0", default-features = false, features = ["client-ureq", "ureq-rustls-tls", "env-file"] }
//! ```

#![allow(non_snake_case)]
use rspotify::{model::TrackId, prelude::*, ClientCredsSpotify, Credentials};

fn main() {
    let chunk_size = 2; // up to 50
    let ids = vec![
      "spotify:track:0UlwTmT01jdFp3BaofARtU",
      "spotify:track:7MXK7DwU1DpGFTA31AZW6S",
      "spotify:track:0Lz0VvjdDvO8LaUOPW94iT",
      "spotify:track:3obwANwnr6iXVABsOnXLNC",
      "spotify:track:5xYZXIgVAND5sWjN8G0hID",
    ];
    let creds = Credentials::from_env().unwrap();
    let spotify = ClientCredsSpotify::new(creds);
    spotify.request_token().unwrap();
    let track_ids = ids.iter().map(|id| TrackId::from_uri(id).unwrap()).collect::<Vec<TrackId>>();
     track_ids.chunks(chunk_size).for_each(|track_set| {
        let tracks = spotify.tracks_features(track_set.to_owned());
        dbg!(tracks.ok());
    });
}
Results
[_active_nvim_run:24] tracks.ok() = Some(
    Some(
        [
            AudioFeatures {
                acousticness: 0.0245,
                analysis_url: "https://api.spotify.com/v1/audio-analysis/0UlwTmT01jdFp3BaofARtU",
                danceability: 0.593,
                duration: Duration {
                    secs: 213,
                    nanos: 960000000,
                },
                energy: 0.693,
                id: TrackId(
                    "0UlwTmT01jdFp3BaofARtU",
                ),
                instrumentalness: 9.54e-6,
                key: 1,
                liveness: 0.0433,
                loudness: -10.264,
                mode: Minor,
                speechiness: 0.0271,
                tempo: 135.747,
                time_signature: 4,
                track_href: "https://api.spotify.com/v1/tracks/0UlwTmT01jdFp3BaofARtU",
                valence: 0.917,
            },
            AudioFeatures {
                acousticness: 0.0873,
                analysis_url: "https://api.spotify.com/v1/audio-analysis/7MXK7DwU1DpGFTA31AZW6S",
                danceability: 0.8,
                duration: Duration {
                    secs: 233,
                    nanos: 960000000,
                },
                energy: 0.532,
                id: TrackId(
                    "7MXK7DwU1DpGFTA31AZW6S",
                ),
                instrumentalness: 0.0,
                key: 11,
                liveness: 0.17,
                loudness: -9.937,
                mode: Minor,
                speechiness: 0.136,
                tempo: 140.156,
                time_signature: 4,
                track_href: "https://api.spotify.com/v1/tracks/7MXK7DwU1DpGFTA31AZW6S",
                valence: 0.863,
            },
        ],
    ),
)
[_active_nvim_run:24] tracks.ok() = Some(
    Some(
        [
            AudioFeatures {
                acousticness: 0.0241,
                analysis_url: "https://api.spotify.com/v1/audio-analysis/0Lz0VvjdDvO8LaUOPW94iT",
                danceability: 0.714,
                duration: Duration {
                    secs: 229,
                    nanos: 239000000,
                },
                energy: 0.815,
                id: TrackId(
                    "0Lz0VvjdDvO8LaUOPW94iT",
                ),
                instrumentalness: 0.0542,
                key: 9,
                liveness: 0.0917,
                loudness: -4.472,
                mode: Major,
                speechiness: 0.0536,
                tempo: 114.994,
                time_signature: 4,
                track_href: "https://api.spotify.com/v1/tracks/0Lz0VvjdDvO8LaUOPW94iT",
                valence: 0.943,
            },
            AudioFeatures {
                acousticness: 0.0455,
                analysis_url: "https://api.spotify.com/v1/audio-analysis/3obwANwnr6iXVABsOnXLNC",
                danceability: 0.257,
                duration: Duration {
                    secs: 339,
                    nanos: 787000000,
                },
                energy: 0.796,
                id: TrackId(
                    "3obwANwnr6iXVABsOnXLNC",
                ),
                instrumentalness: 0.0331,
                key: 2,
                liveness: 0.15,
                loudness: -8.39,
                mode: Major,
                speechiness: 0.106,
                tempo: 165.868,
                time_signature: 4,
                track_href: "https://api.spotify.com/v1/tracks/3obwANwnr6iXVABsOnXLNC",
                valence: 0.711,
            },
        ],
    ),
)
[_active_nvim_run:24] tracks.ok() = Some(
    Some(
        [
            AudioFeatures {
                acousticness: 0.195,
                analysis_url: "https://api.spotify.com/v1/audio-analysis/5xYZXIgVAND5sWjN8G0hID",
                danceability: 0.568,
                duration: Duration {
                    secs: 252,
                    nanos: 721000000,
                },
                energy: 0.795,
                id: TrackId(
                    "5xYZXIgVAND5sWjN8G0hID",
                ),
                instrumentalness: 0.588,
                key: 6,
                liveness: 0.0924,
                loudness: -10.0,
                mode: Major,
                speechiness: 0.037,
                tempo: 87.482,
                time_signature: 4,
                track_href: "https://api.spotify.com/v1/tracks/5xYZXIgVAND5sWjN8G0hID",
                valence: 0.662,
            },
        ],
    ),
)
  • You can get up to 50 tracks at a time

  • This one is done syncronously since I'm working with an SQLite database for inserts and don't want to hit it async

  • The credentails come from a .env file with: RSPOTIFY_CLIENT_ID, RSPOTIFY_CLIENT_SECRET for Spotify Client Credentails auth

═══ § ═══

Footnotes And References