1
0
Fork 0
mirror of https://github.com/librespot-org/librespot.git synced 2025-10-05 10:49:40 +02:00
librespot/examples/playlist_tracks.rs
Jay Malhotra fea16c2f07 refactor: Introduce SpotifyUri struct
Contributes to #1266

Introduces a new `SpotifyUri` struct which is layered on top of the
existing `SpotifyId`, but has the capability to support URIs that do
not confirm to the canonical base62 encoded format. This allows it to
describe URIs like `spotify:local`, `spotify:genre` and others that
`SpotifyId` cannot represent.

Changed the internal player state to use these URIs as much as possible,
such that the player could in the future accept a URI of the type
`spotify:local`, as a means of laying the groundwork for local file
support.
2025-09-07 21:29:24 +01:00

43 lines
1.2 KiB
Rust

use std::{env, process::exit};
use librespot::{
core::{
authentication::Credentials, config::SessionConfig, session::Session,
spotify_uri::SpotifyUri,
},
metadata::{Metadata, Playlist, Track},
};
#[tokio::main]
async fn main() {
env_logger::init();
let session_config = SessionConfig::default();
let args: Vec<_> = env::args().collect();
if args.len() != 3 {
eprintln!("Usage: {} ACCESS_TOKEN PLAYLIST", args[0]);
return;
}
let credentials = Credentials::with_access_token(&args[1]);
let plist_uri = SpotifyUri::from_uri(&args[2]).unwrap_or_else(|_| {
eprintln!(
"PLAYLIST should be a playlist URI such as: \
\"spotify:playlist:37i9dQZF1DXec50AjHrNTq\""
);
exit(1);
});
let session = Session::new(session_config, None);
if let Err(e) = session.connect(credentials, false).await {
println!("Error connecting: {e}");
exit(1);
}
let plist = Playlist::get(&session, &plist_uri).await.unwrap();
println!("{plist:?}");
for track_id in plist.tracks() {
let plist_track = Track::get(&session, track_id).await.unwrap();
println!("track: {} ", plist_track.name);
}
}