Skip to content

Commit b994d80

Browse files
authored
1 parent db509ea commit b994d80

File tree

10 files changed

+75
-83
lines changed

10 files changed

+75
-83
lines changed

spotdl/console/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,13 @@ def console_entry_point():
2121
args_dict = vars(arguments)
2222

2323
if arguments.ffmpeg:
24-
args_dict['ffmpeg'] = str(Path(arguments.ffmpeg).absolute())
24+
args_dict["ffmpeg"] = str(Path(arguments.ffmpeg).absolute())
2525
else:
26-
args_dict['ffmpeg'] = "ffmpeg"
26+
args_dict["ffmpeg"] = "ffmpeg"
2727

2828
# Check if ffmpeg has correct version, if not exit
2929
if (
30-
ffmpeg.has_correct_version(
31-
arguments.ignore_ffmpeg_version, args_dict['ffmpeg']
32-
)
30+
ffmpeg.has_correct_version(arguments.ignore_ffmpeg_version, args_dict["ffmpeg"])
3331
is False
3432
):
3533
sys.exit(1)
@@ -84,6 +82,7 @@ def graceful_exit(signal, frame):
8482
arguments.generate_m3u,
8583
arguments.lyrics_provider,
8684
arguments.search_threads,
85+
arguments.path_template,
8786
)
8887

8988
# Start downloading

spotdl/download/downloader.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,19 @@ async def download_song(self, song_object: SongObject) -> None:
220220

221221
downloaded_file_path = Path(downloaded_file_path_string)
222222

223-
if downloaded_file_path.suffix == ".m4a" and self.arguments["output_format"] != "m4a":
223+
if (
224+
downloaded_file_path.suffix == ".m4a"
225+
and self.arguments["output_format"] == "m4a"
226+
):
227+
downloaded_file_path.rename(converted_file_path)
228+
ffmpeg_success = True
229+
else:
224230
ffmpeg_success = await ffmpeg.convert(
225231
downloaded_file_path=downloaded_file_path,
226232
converted_file_path=converted_file_path,
227233
output_format=self.arguments["output_format"],
228234
ffmpeg_path=self.arguments["ffmpeg"],
229235
)
230-
else:
231-
downloaded_file_path.rename(converted_file_path)
232-
ffmpeg_success = True
233236

234237
if display_progress_tracker:
235238
display_progress_tracker.notify_conversion_completion()

spotdl/download/embed_metadata.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import base64
2+
from pathlib import Path
23

34
from urllib.request import urlopen
45
from mutagen.oggopus import OggOpus

spotdl/download/ffmpeg.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,9 @@ async def convert(
6868
"mp3": ["-codec:a", "libmp3lame"],
6969
"flac": ["-codec:a", "flac"],
7070
"ogg": ["-codec:a", "libvorbis"],
71-
"opus": [
72-
"-vn", "-c:a", "copy"
73-
] if downloaded_file_path.endswith(".opus") else [
74-
"-c:a", "libopus"
75-
],
71+
"opus": ["-vn", "-c:a", "copy"]
72+
if downloaded_file_path.endswith(".opus")
73+
else ["-c:a", "libopus"],
7674
"m4a": ["-codec:a", "aac", "-vn"],
7775
"wav": [],
7876
}

spotdl/parsers/argument_parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pkg_resources
44

5-
help_notice = r""" # noqa: E501
5+
help_notice = r"""
66
To download a song run,
77
spotdl [trackUrl]
88
ex. spotdl https://open.spotify.com/track/0VjIjW4GlUZAMYd2vXMi3b
@@ -78,7 +78,7 @@
7878
7979
spotDL downloads up to 4 songs in parallel, so for a faster experience,
8080
download albums and playlist, rather than tracks.
81-
"""
81+
""" # noqa: E501
8282

8383

8484
def parse_arguments():

spotdl/parsers/query_parser.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66

77

88
def parse_query(
9-
query: List[str], format, use_youtube, generate_m3u, lyrics_provider, threads
9+
query: List[str],
10+
format,
11+
use_youtube,
12+
generate_m3u,
13+
lyrics_provider,
14+
threads,
15+
path_template,
1016
) -> List[SongObject]:
1117
"""
1218
Parse query and return list containing song object
@@ -21,7 +27,13 @@ def parse_query(
2127

2228
songs_list.extend(
2329
parse_request(
24-
request, format, use_youtube, generate_m3u, lyrics_provider, threads
30+
request,
31+
format,
32+
use_youtube,
33+
generate_m3u,
34+
lyrics_provider,
35+
threads,
36+
path_template,
2537
)
2638
)
2739

@@ -46,6 +58,7 @@ def parse_request(
4658
generate_m3u: bool = False,
4759
lyrics_provider: str = None,
4860
threads: int = 1,
61+
path_template: str = None,
4962
) -> List[SongObject]:
5063
song_list: List[SongObject] = []
5164
if (
@@ -81,12 +94,24 @@ def parse_request(
8194
elif "open.spotify.com" in request and "album" in request:
8295
print("Fetching Album...")
8396
song_list = song_gatherer.from_album(
84-
request, output_format, use_youtube, lyrics_provider, generate_m3u, threads
97+
request,
98+
output_format,
99+
use_youtube,
100+
lyrics_provider,
101+
generate_m3u,
102+
threads,
103+
path_template,
85104
)
86105
elif "open.spotify.com" in request and "playlist" in request:
87106
print("Fetching Playlist...")
88107
song_list = song_gatherer.from_playlist(
89-
request, output_format, use_youtube, lyrics_provider, generate_m3u, threads
108+
request,
109+
output_format,
110+
use_youtube,
111+
lyrics_provider,
112+
generate_m3u,
113+
threads,
114+
path_template,
90115
)
91116
elif "open.spotify.com" in request and "artist" in request:
92117
print("Fetching artist...")

spotdl/providers/provider_utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ def _match_percentage(str1: str, str2: str, score_cutoff: float = 0) -> float:
3838
if each_letter.isalnum() or each_letter.isspace()
3939
)
4040

41-
return fuzz.partial_ratio(new_str1, new_str2, processor=None, score_cutoff=score_cutoff)
41+
return fuzz.partial_ratio(
42+
new_str1, new_str2, processor=None, score_cutoff=score_cutoff
43+
)
4244

4345

4446
def _parse_duration(duration: str) -> float:

spotdl/providers/yt_provider.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def _order_yt_results(
116116
# ! (remix)' would return 100, so we're absolutely corrent in matching
117117
# ! artists to song name.
118118
if _match_percentage(
119-
unidecode(artist.lower()), unidecode(result.title).lower(), 85
119+
str(unidecode(artist.lower())), str(unidecode(result.title).lower()), 85
120120
):
121121
artist_match_number += 1
122122

@@ -129,7 +129,7 @@ def _order_yt_results(
129129
song_title = _create_song_title(song_name, song_artists).lower()
130130
name_match = round(
131131
_match_percentage(
132-
unidecode(result.title.lower()), unidecode(song_title), 60
132+
str(unidecode(result.title.lower())), str(unidecode(song_title)), 60
133133
),
134134
ndigits=3,
135135
)

spotdl/providers/ytm_provider.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def _order_ytm_results(
160160
if result["type"] == "song":
161161
for artist in song_artists:
162162
artist_match_number += _match_percentage(
163-
unidecode(artist.lower()), unidecode(result["artist"]).lower()
163+
str(unidecode(artist.lower())), unidecode(result["artist"].lower())
164164
)
165165
else:
166166
# ! i.e if video
@@ -169,7 +169,7 @@ def _order_ytm_results(
169169
# ! (remix)' would return 100, so we're absolutely corrent in matching
170170
# ! artists to song name.
171171
artist_match_number += _match_percentage(
172-
unidecode(artist.lower()), unidecode(result["name"]).lower()
172+
str(unidecode(artist.lower())), unidecode(result["name"].lower())
173173
)
174174

175175
# we didn't find artist in the video title, so we fallback to
@@ -178,7 +178,7 @@ def _order_ytm_results(
178178
if artist_match_number == 0:
179179
for artist in song_artists:
180180
artist_match_number += _match_percentage(
181-
unidecode(artist.lower()),
181+
str(unidecode(artist.lower())),
182182
unidecode(result["artist"].lower()),
183183
)
184184

@@ -192,12 +192,16 @@ def _order_ytm_results(
192192
# this needs more testing
193193
if result["type"] == "song":
194194
name_match = round(
195-
_match_percentage(unidecode(result["name"]), unidecode(song_name), 60),
195+
_match_percentage(
196+
unidecode(result["name"]), str(unidecode(song_name)), 60
197+
),
196198
ndigits=3,
197199
)
198200
else:
199201
name_match = round(
200-
_match_percentage(unidecode(result["name"]), unidecode(song_title), 60),
202+
_match_percentage(
203+
unidecode(result["name"]), str(unidecode(song_title)), 60
204+
),
201205
ndigits=3,
202206
)
203207

spotdl/search/song_gatherer.py

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
lyrics_providers,
1212
)
1313
from spotdl.search import SongObject, SpotifyClient
14-
from spotdl.providers.provider_utils import _get_converted_file_path
1514
from spotdl.utils.song_name_utils import format_name
15+
from spotdl.providers.provider_utils import (
16+
_get_converted_file_path,
17+
_parse_path_template,
18+
)
1619

1720

1821
def from_spotify_url(
@@ -141,6 +144,7 @@ def from_album(
141144
lyrics_provider: str = None,
142145
generate_m3u: bool = False,
143146
threads: int = 1,
147+
path_template: str = None,
144148
) -> List[SongObject]:
145149
"""
146150
Create and return list containing SongObject for every song in the album
@@ -188,7 +192,7 @@ def get_tracks(track):
188192
)
189193

190194
if generate_m3u:
191-
file_path = _get_converted_file_path(song, output_format)
195+
file_path = _parse_path_template(path_template, song, output_format)
192196

193197
return song, f"{file_path}\n"
194198

@@ -197,31 +201,8 @@ def get_tracks(track):
197201
return None, None
198202
except OSError:
199203
if generate_m3u:
200-
file_path = (
201-
str(
202-
provider_utils._create_song_title(
203-
track["name"],
204-
[artist["name"] for artist in track["artists"]],
205-
)
206-
)
207-
+ "."
208-
+ output_format
209-
if output_format is not None
210-
else "mp3"
211-
)
212-
213-
if len(file_path) > 256:
214-
file_path = (
215-
str(
216-
provider_utils._create_song_title(
217-
track["name"], [track["artists"][0]["name"]]
218-
)
219-
)
220-
+ "."
221-
+ output_format
222-
if output_format is not None
223-
else "mp3"
224-
)
204+
song_obj = SongObject({*track}, album_response, {}, None, "", None)
205+
file_path = _parse_path_template(path_template, song_obj, output_format)
225206

226207
return None, f"{file_path}\n"
227208

@@ -233,7 +214,7 @@ def get_tracks(track):
233214
album_text = ""
234215
for result in results:
235216
if result[1] is not None:
236-
album_text += format_name(result[1])
217+
album_text += result[1]
237218

238219
if result[0] is not None and result[0].youtube_link is not None:
239220
tracks.append(result[0])
@@ -263,6 +244,7 @@ def from_playlist(
263244
lyrics_provider: str = None,
264245
generate_m3u: bool = False,
265246
threads: int = 1,
247+
path_template: str = None,
266248
) -> List[SongObject]:
267249
"""
268250
Create and return list containing SongObject for every song in the playlist
@@ -320,7 +302,7 @@ def get_song(track):
320302
)
321303

322304
if generate_m3u:
323-
file_path = _get_converted_file_path(song, output_format)
305+
file_path = _parse_path_template(path_template, song, output_format)
324306

325307
return song, f"{file_path}\n"
326308

@@ -329,32 +311,10 @@ def get_song(track):
329311
return None, None
330312
except OSError:
331313
if generate_m3u:
332-
file_path = (
333-
str(
334-
provider_utils._create_song_title(
335-
track["track"]["name"],
336-
[artist["name"] for artist in track["track"]["artists"]],
337-
)
338-
)
339-
+ "."
340-
+ output_format
341-
if output_format is not None
342-
else "mp3"
314+
song_obj = SongObject(
315+
{*track["track"]}, {}, {}, None, "", playlist_response
343316
)
344-
345-
if len(file_path) > 256:
346-
file_path = (
347-
str(
348-
provider_utils._create_song_title(
349-
track["track"]["name"],
350-
[track["track"]["artists"][0]["name"]],
351-
)
352-
)
353-
+ "."
354-
+ output_format
355-
if output_format is not None
356-
else "mp3"
357-
)
317+
file_path = _parse_path_template(path_template, song_obj, output_format)
358318

359319
return None, f"{file_path}\n"
360320

@@ -366,7 +326,7 @@ def get_song(track):
366326
playlist_text = ""
367327
for result in results:
368328
if result[1] is not None:
369-
playlist_text += format_name(result[1])
329+
playlist_text += result[1]
370330

371331
if result[0] is not None and result[0].youtube_link is not None:
372332
tracks.append(result[0])

0 commit comments

Comments
 (0)