From 2387c64fe63fab77d64a3f0b643e0620bdc05cd1 Mon Sep 17 00:00:00 2001 From: obito1903 Date: Wed, 25 Jan 2023 16:13:39 +0100 Subject: [PATCH] add option to download and keep playlist order --- src/main.rs | 93 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 67 insertions(+), 26 deletions(-) diff --git a/src/main.rs b/src/main.rs index c04a4b1..614a27d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,25 +2,28 @@ mod file_sink; extern crate rpassword; -use std::{path::PathBuf}; +use std::path::PathBuf; -use librespot::{core::authentication::Credentials, metadata::Playlist}; use librespot::core::config::SessionConfig; use librespot::core::session::Session; use librespot::core::spotify_id::SpotifyId; use librespot::playback::config::PlayerConfig; +use librespot::{core::authentication::Credentials, metadata::Playlist}; +use librespot::playback::audio_backend::Open; use librespot::playback::player::Player; -use librespot::playback::audio_backend::{Open}; -use librespot::metadata::{Track, Artist, Metadata, Album}; +use librespot::metadata::{Album, Artist, Metadata, Track}; use structopt::StructOpt; use indicatif::{ProgressBar, ProgressStyle}; #[derive(Debug, StructOpt)] -#[structopt(name = "spotify-dl", about = "A commandline utility to download music directly from Spotify")] +#[structopt( + name = "spotify-dl", + about = "A commandline utility to download music directly from Spotify" +)] struct Opt { #[structopt(help = "A list of Spotify URIs (songs, podcasts or playlists)")] tracks: Vec, @@ -28,8 +31,19 @@ struct Opt { username: String, #[structopt(short = "p", long = "password", help = "Your Spotify password")] password: Option, - #[structopt(short = "d", long = "destination", default_value = ".", help = "The directory where the songs will be downloaded")] - destination: String + #[structopt( + short = "d", + long = "destination", + default_value = ".", + help = "The directory where the songs will be downloaded" + )] + destination: String, + #[structopt( + short = "o", + long = "ordered", + help = "Download songs in the order they are in the playlist, prfixing the filename with the track number" + )] + ordered: bool, } #[derive(Clone)] @@ -41,11 +55,18 @@ pub struct TrackMetadata { async fn create_session(credentials: Credentials) -> Session { let session_config = SessionConfig::default(); - let session = Session::connect(session_config, credentials, None).await.unwrap(); + let session = Session::connect(session_config, credentials, None) + .await + .unwrap(); session } -async fn download_tracks(session: &Session, destination: PathBuf, tracks: Vec) { +async fn download_tracks( + session: &Session, + destination: PathBuf, + tracks: Vec, + ordered: bool, +) { let player_config = PlayerConfig::default(); let bar_style = ProgressStyle::default_bar() .template("[{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} (ETA: {eta}) {msg}") @@ -54,14 +75,14 @@ async fn download_tracks(session: &Session, destination: PathBuf, tracks: Vec 1 { let mut tmp: String = String::new(); @@ -72,22 +93,33 @@ async fn download_tracks(session: &Session, destination: PathBuf, tracks: Vec { match Playlist::get(&session, track).await { Ok(mut playlist) => { - println!("Adding all songs from playlist {} (by {}) to the queue", &playlist.name, &playlist.user); + println!( + "Adding all songs from playlist {} (by {}) to the queue", + &playlist.name, &playlist.user + ); tracks.append(&mut playlist.tracks); } Err(_) => { @@ -131,6 +167,11 @@ async fn main() { } } - download_tracks(&session, PathBuf::from(opt.destination), tracks).await; - + download_tracks( + &session, + PathBuf::from(opt.destination), + tracks, + opt.ordered, + ) + .await; }