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 Basic Track Details From The Spotify API In Rust

December 2023
Code
//! ```cargo
//! [dependencies]
//! rspotify = { version = "0.12.0", default-features = false, features = ["client-reqwest", "reqwest-native-tls", "env-file"] }
//! tokio = { version = "1.11.0", features = ["rt-multi-thread", "macros"] }
//! ```

use rspotify::{model::TrackId, prelude::*, ClientCredsSpotify, Credentials};

#[tokio::main]
async fn main() {
    let creds = Credentials::from_env().unwrap();
    let spotify = ClientCredsSpotify::new(creds);
    spotify.request_token().await.unwrap();
    let track_id = TrackId::from_uri("spotify:track:4rc6aeLJCWqZ3GBW5rU1P1").unwrap();
    let track = spotify.track(track_id, None).await;
    dbg!(track.ok());
}
Results
[_active_nvim_run:16] track.ok() = Some(
    FullTrack {
        album: SimplifiedAlbum {
            album_group: None,
            album_type: Some(
                "album",
            ),
            artists: [
                SimplifiedArtist {
                    external_urls: {
                        "spotify": "https://open.spotify.com/artist/59pWgeY26Q6yJy37QvJflh",
                    },
                    href: Some(
                        "https://api.spotify.com/v1/artists/59pWgeY26Q6yJy37QvJflh",
                    ),
                    id: Some(
                        ArtistId(
                            "59pWgeY26Q6yJy37QvJflh",
                        ),
                    ),
                    name: "Sleigh Bells",
                },
            ],
            available_markets: [],
            external_urls: {
                "spotify": "https://open.spotify.com/album/4TQDU2C2TcvDQGeFs4UgGT",
            },
            href: Some(
                "https://api.spotify.com/v1/albums/4TQDU2C2TcvDQGeFs4UgGT",
            ),
            id: Some(
                AlbumId(
                    "4TQDU2C2TcvDQGeFs4UgGT",
                ),
            ),
            images: [
                Image {
                    height: Some(
                        640,
                    ),
                    url: "https://i.scdn.co/image/ab67616d0000b273da89a46b20a6adeb03bbdf95",
                    width: Some(
                        640,
                    ),
                },
                Image {
                    height: Some(
                        300,
                    ),
                    url: "https://i.scdn.co/image/ab67616d00001e02da89a46b20a6adeb03bbdf95",
                    width: Some(
                        300,
                    ),
                },
                Image {
                    height: Some(
                        64,
                    ),
                    url: "https://i.scdn.co/image/ab67616d00004851da89a46b20a6adeb03bbdf95",
                    width: Some(
                        64,
                    ),
                },
            ],
            name: "Treats",
            release_date: Some(
                "2010-05-10",
            ),
            release_date_precision: Some(
                "day",
            ),
            restrictions: None,
        },
        artists: [
            SimplifiedArtist {
                external_urls: {
                    "spotify": "https://open.spotify.com/artist/59pWgeY26Q6yJy37QvJflh",
                },
                href: Some(
                    "https://api.spotify.com/v1/artists/59pWgeY26Q6yJy37QvJflh",
                ),
                id: Some(
                    ArtistId(
                        "59pWgeY26Q6yJy37QvJflh",
                    ),
                ),
                name: "Sleigh Bells",
            },
        ],
        available_markets: [],
        disc_number: 1,
        duration: Duration {
            secs: 229,
            nanos: 893000000,
        },
        explicit: false,
        external_ids: {
            "isrc": "USQE91000035",
        },
        external_urls: {
            "spotify": "https://open.spotify.com/track/4rc6aeLJCWqZ3GBW5rU1P1",
        },
        href: Some(
            "https://api.spotify.com/v1/tracks/4rc6aeLJCWqZ3GBW5rU1P1",
        ),
        id: Some(
            TrackId(
                "4rc6aeLJCWqZ3GBW5rU1P1",
            ),
        ),
        is_local: false,
        is_playable: None,
        linked_from: None,
        restrictions: None,
        name: "Rill Rill",
        popularity: 0,
        preview_url: None,
        track_number: 7,
    },
)
═══ § ═══