diff --git a/.gitignore b/.gitignore index 1cffda606..257924c6e 100644 --- a/.gitignore +++ b/.gitignore @@ -145,3 +145,6 @@ temp/ # VS Code .vscode *.txt + +# Output Folder +output/ \ No newline at end of file diff --git a/spotdl/download/downloader.py b/spotdl/download/downloader.py index fdf86fb70..60db8c500 100644 --- a/spotdl/download/downloader.py +++ b/spotdl/download/downloader.py @@ -459,6 +459,18 @@ def search_and_download( # pylint: disable=R0911 restrict=self.settings["restrict"], file_name_length=self.settings["max_filename_length"], ) + + # Update output path using song.album_name if valid; otherwise, use song.artist. + output_file = ( + Path("output") + / ( + Path(song.album_name) + if Path(song.album_name).is_dir() + else Path(song.artist) + ) + / output_file + ) + except Exception: song = reinit_song(song) @@ -470,6 +482,17 @@ def search_and_download( # pylint: disable=R0911 file_name_length=self.settings["max_filename_length"], ) + # Update output path using song.album_name if valid; otherwise, use song.artist. + output_file = ( + Path("output") + / ( + Path(song.album_name) + if Path(song.album_name).is_dir() + else Path(song.artist) + ) + / output_file + ) + reinitialized = True if song.explicit is True and self.settings["skip_explicit"] is True: @@ -488,14 +511,14 @@ def search_and_download( # pylint: disable=R0911 dup_song_paths: List[Path] = self.known_songs.get(song.url, []) # Remove files from the list that have the same path as the output file - dup_song_paths = [ - dup_song_path - for dup_song_path in dup_song_paths - if (dup_song_path.absolute() != output_file.absolute()) - and dup_song_path.exists() - ] + dup_song_paths = list(Path(output_file.parts[0]).rglob(output_file.name)) + + # Checking if file already exists in all subfolders of output directory + file_exists = ( + next(Path(output_file.parts[0]).rglob(output_file.name), None) + or dup_song_paths + ) - file_exists = output_file.exists() or dup_song_paths if not self.settings["scan_for_songs"]: for file_extension in self.scan_formats: ext_path = output_file.with_suffix(f".{file_extension}") @@ -571,7 +594,7 @@ def search_and_download( # pylint: disable=R0911 logger.info("Removing duplicate file: %s", dup_song_path) dup_song_path.unlink() - except (PermissionError, OSError) as exc: + except (PermissionError, OSError, Exception) as exc: logger.debug( "Could not remove duplicate file: %s, error: %s", dup_song_path,