Skip to content

Commit

Permalink
Fixes: #2212 ; Feature: Skip Duplicate Song from Sub-Folders ; Fixed (#…
Browse files Browse the repository at this point in the history
…2214)

Co-authored-by: Mill GithHub Actions <mill-ci@localhost>
  • Loading branch information
2 people authored and xnetcat committed Oct 21, 2024
1 parent a81eb3f commit 602fe87
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,6 @@ temp/
# VS Code
.vscode
*.txt

# Output Folder
output/
39 changes: 31 additions & 8 deletions spotdl/download/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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:
Expand All @@ -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}")
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 602fe87

Please sign in to comment.