From 8e06c9bdf3320f6e1e2dc3e2745553514a339902 Mon Sep 17 00:00:00 2001 From: 45Ninjas Date: Thu, 1 Sep 2022 17:19:41 +0800 Subject: [PATCH] feat; skip existing songs before downloading (#40) --- src/downloader.rs | 9 +++++++++ src/error.rs | 2 ++ 2 files changed, 11 insertions(+) diff --git a/src/downloader.rs b/src/downloader.rs index b5ba846..387776a 100644 --- a/src/downloader.rs +++ b/src/downloader.rs @@ -531,6 +531,13 @@ impl DownloaderInternal { } ); let path = Path::new(&path).to_owned(); + + // Don't download if we are skipping and the path exists. + if config.skip_existing && path.is_file() { + return Err(SpotifyError::AlreadyDownloaded); + } + + let path_clone = path.clone(); let key = session.audio_key().request(track.id, *file_id).await?; @@ -861,6 +868,7 @@ pub struct DownloaderConfig { pub id3v24: bool, pub convert_to_mp3: bool, pub separator: String, + pub skip_existing: bool, } impl DownloaderConfig { @@ -874,6 +882,7 @@ impl DownloaderConfig { id3v24: true, convert_to_mp3: false, separator: ", ".to_string(), + skip_existing: true } } } diff --git a/src/error.rs b/src/error.rs index 11c8a43..d8ef18e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -19,6 +19,7 @@ pub enum SpotifyError { ID3Error(String, String), Reqwest(String), InvalidFormat, + AlreadyDownloaded, } impl std::error::Error for SpotifyError {} @@ -42,6 +43,7 @@ impl fmt::Display for SpotifyError { SpotifyError::ID3Error(k, e) => write!(f, "ID3 Error: {} {}", k, e), SpotifyError::Reqwest(e) => write!(f, "Reqwest Error: {}", e), SpotifyError::InvalidFormat => write!(f, "Invalid Format!"), + SpotifyError::AlreadyDownloaded => write!(f, "Already Downloaded") } } }