Merge pull request #8 from nyvs/master

add album download
This commit is contained in:
Guillem Castro 2024-05-08 08:14:33 +02:00 committed by GitHub
commit 11536a7aa9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 33 additions and 16 deletions

View file

@ -1,6 +1,6 @@
# spotify-dl # spotify-dl
A command line utility to download songs and playlists directly from Spotify's servers. A command line utility to download songs, podcasts, playlists and albums directly from Spotify's servers.
You need a Spotify Premium account. You need a Spotify Premium account.
@ -50,7 +50,7 @@ OPTIONS:
ARGS: ARGS:
<tracks>... A list of Spotify URIs (songs, podcasts or playlists)``` <tracks>... A list of Spotify URIs (songs, podcasts or playlists)```
Songs and playlists must be passed as Spotify URIs or URLs (e.g. `spotify:track:123456789abcdefghABCDEF` for songs and `spotify:playlist:123456789abcdefghABCDEF` for playlists or `https://open.spotify.com/playlist/123456789abcdefghABCDEF?si=1234567890`). Songs, playlists and albums must be passed as Spotify URIs or URLs (e.g. `spotify:track:123456789abcdefghABCDEF` for songs and `spotify:playlist:123456789abcdefghABCDEF` for playlists or `https://open.spotify.com/playlist/123456789abcdefghABCDEF?si=1234567890`).
## Disclaimer ## Disclaimer

View file

@ -27,7 +27,7 @@ use indicatif::{ProgressBar, ProgressStyle};
about = "A commandline utility to download music directly from Spotify" about = "A commandline utility to download music directly from Spotify"
)] )]
struct Opt { struct Opt {
#[structopt(help = "A list of Spotify URIs (songs, podcasts or playlists)")] #[structopt(help = "A list of Spotify URIs (songs, podcasts, playlists or albums)")]
tracks: Vec<String>, tracks: Vec<String>,
#[structopt(short = "u", long = "username", help = "Your Spotify username")] #[structopt(short = "u", long = "username", help = "Your Spotify username")]
username: String, username: String,
@ -175,6 +175,33 @@ async fn download_tracks(
bar.finish(); bar.finish();
} }
async fn get_tracks_from_playlist_or_album(session: &Session, track: SpotifyId, track_url: &String) -> Vec<SpotifyId> {
match Playlist::get(&session, track).await {
Ok(playlist) => {
println!(
"Adding all songs from playlist {} (by {}) to the queue",
&playlist.name, &playlist.user
);
return playlist.tracks;
},
_ => ()
}
match Album::get(&session, track).await {
Ok(album) => {
println!(
"Adding all songs from album {} (by {:?}) to the queue",
&album.name, &album.artists
);
return album.tracks;
}
Err(_) => {
println!("Unsupported URI {}", &track_url);
vec![]
}
}
}
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let opt = Opt::from_args(); let opt = Opt::from_args();
@ -210,18 +237,8 @@ async fn main() {
tracks.push(track); tracks.push(track);
} }
librespot::core::spotify_id::SpotifyAudioType::NonPlayable => { librespot::core::spotify_id::SpotifyAudioType::NonPlayable => {
match Playlist::get(&session, track).await { let mut multiple_tracks = get_tracks_from_playlist_or_album(&session, track, &track_url).await;
Ok(mut playlist) => { tracks.append(&mut multiple_tracks)
println!(
"Adding all songs from playlist {} (by {}) to the queue",
&playlist.name, &playlist.user
);
tracks.append(&mut playlist.tracks);
}
Err(_) => {
println!("Unsupported track {}", &track_url);
}
}
} }
} }
} }