Skip to content

Commit 38c62cc

Browse files
FarmViviSilverarmorxnetcatyezz123phcreery
authored
Feature/song names (spotDL#1429)
Co-authored-by: Silverarmor <[email protected]> Co-authored-by: Jakub Kot <[email protected]> Co-authored-by: Yasser Tahiri <[email protected]> Co-authored-by: Peyton Creery <[email protected]> Co-authored-by: Arbaaz Shafiq <[email protected]> Co-authored-by: Jakub Kot <[email protected]>
1 parent 3451660 commit 38c62cc

File tree

8 files changed

+27
-32
lines changed

8 files changed

+27
-32
lines changed

spotdl/download/tracking_file_handler.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Optional, List
33

44
from spotdl.search import SongObject, song_gatherer
5+
from spotdl.utils.song_name_utils import format_name
56

67

78
class DownloadTracker:
@@ -79,9 +80,7 @@ def backup_to_disk(self):
7980
if not self.save_file:
8081
song_name = self.song_list[0].song_name
8182

82-
song_name = "".join(char for char in song_name if char not in "/?\\*|<>")
83-
84-
song_name = song_name.replace('"', "'").replace(":", " - ")
83+
song_name = format_name(song_name)
8584

8685
self.save_file = Path(song_name + ".spotdlTrackingFile")
8786

spotdl/providers/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
_match_percentage,
77
_parse_duration,
88
)
9+
from spotdl.utils.song_name_utils import format_name

spotdl/providers/provider_utils.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
from rapidfuzz import fuzz
77

8+
from spotdl.utils.song_name_utils import format_name
9+
810

911
def _match_percentage(str1: str, str2: str, score_cutoff: float = 0) -> float:
1012
"""
@@ -58,20 +60,11 @@ def _parse_duration(duration: str) -> float:
5860

5961
def _create_song_title(song_name: str, song_artists: List[str]) -> str:
6062
joined_artists = ", ".join(song_artists)
61-
return f"{joined_artists} - {song_name}"
63+
return _sanitize_filename(f"{joined_artists} - {song_name}")
6264

6365

6466
def _sanitize_filename(input_str: str) -> str:
65-
output = input_str
66-
67-
# ! this is windows specific (disallowed chars)
68-
output = "".join(char for char in output if char not in "/?\\*|<>")
69-
70-
# ! double quotes (") and semi-colons (:) are also disallowed characters but we would
71-
# ! like to retain their equivalents, so they aren't removed in the prior loop
72-
output = output.replace('"', "'").replace(":", "-")
73-
74-
return output
67+
return format_name(input_str)
7568

7669

7770
def _get_smaller_file_path(input_song, output_format: str) -> Path:

spotdl/search/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
from_saved_tracks,
1010
from_dump,
1111
) # noqa: F401
12+
from spotdl.utils.song_name_utils import format_name # noqa: F401

spotdl/search/song_gatherer.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
)
1313
from spotdl.search import SongObject, SpotifyClient
1414
from spotdl.providers.provider_utils import _get_converted_file_path
15+
from spotdl.utils.song_name_utils import format_name
1516

1617

1718
def from_spotify_url(
@@ -232,7 +233,7 @@ def get_tracks(track):
232233
album_text = ""
233234
for result in results:
234235
if result[1] is not None:
235-
album_text += "".join(char for char in result[1] if char not in "/?\\*|<>")
236+
album_text += format_name(result[1])
236237

237238
if result[0] is not None and result[0].youtube_link is not None:
238239
tracks.append(result[0])
@@ -245,7 +246,7 @@ def get_tracks(track):
245246
else:
246247
album_name = album_tracks[0]["name"]
247248

248-
album_name = "".join(char for char in album_name if char not in "/?\\*|<>")
249+
album_name = format_name(album_name)
249250

250251
album_file = Path(f"{album_name}.m3u")
251252

@@ -365,9 +366,7 @@ def get_song(track):
365366
playlist_text = ""
366367
for result in results:
367368
if result[1] is not None:
368-
playlist_text += "".join(
369-
char for char in result[1] if char not in "/?\\*|<>"
370-
)
369+
playlist_text += format_name(result[1])
371370

372371
if result[0] is not None and result[0].youtube_link is not None:
373372
tracks.append(result[0])
@@ -380,9 +379,7 @@ def get_song(track):
380379
else:
381380
playlist_name = playlist_tracks[0]["track"]["name"]
382381

383-
playlist_name = "".join(
384-
char for char in playlist_name if char not in "/?\\*|<>"
385-
)
382+
playlist_name = format_name(playlist_name)
386383

387384
playlist_file = Path(f"{playlist_name}.m3u")
388385

spotdl/search/song_object.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from typing import List, Optional
22

3+
from spotdl.utils.song_name_utils import format_name
4+
35

46
class SongObject:
57

@@ -204,13 +206,4 @@ def create_file_name(song_name: str, song_artists: List[str]) -> str:
204206

205207
converted_file_name = artist_string + " - " + song_name
206208

207-
# ! this is windows specific (disallowed chars)
208-
converted_file_name = "".join(
209-
char for char in converted_file_name if char not in "/?\\*|<>"
210-
)
211-
212-
# ! double quotes (") and semi-colons (:) are also disallowed characters but we would
213-
# ! like to retain their equivalents, so they aren't removed in the prior loop
214-
converted_file_name = converted_file_name.replace('"', "'").replace(":", "-")
215-
216-
return converted_file_name
209+
return format_name(converted_file_name)

spotdl/utils/__init__.py

Whitespace-only changes.

spotdl/utils/song_name_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def format_name(input_str: str) -> str:
2+
output = input_str
3+
4+
# ! this is windows specific (disallowed chars)
5+
output = "".join(char for char in output if char not in "/?\\*|<>#")
6+
7+
# ! double quotes (") and semi-colons (:) are also disallowed characters but we would
8+
# ! like to retain their equivalents, so they aren't removed in the prior loop
9+
output = output.replace('"', "'").replace(":", " - ")
10+
11+
return output

0 commit comments

Comments
 (0)