Skip to content

Commit

Permalink
respect detect-formats option when using m3u option
Browse files Browse the repository at this point in the history
  • Loading branch information
xnetcat committed Sep 1, 2023
1 parent 5e34b21 commit 8a8891e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*.opus
*.spotdl
*.m3u
*.m3u8
*.ipynb
*.lrc
.spotdl-cache
Expand Down
1 change: 1 addition & 0 deletions spotdl/download/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ def download_multiple_songs(
self.settings["format"],
self.settings["restrict"],
False,
self.settings["detect_formats"],
)

# Save results to a file
Expand Down
4 changes: 2 additions & 2 deletions spotdl/types/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class DownloaderOptions(TypedDict):
sync_without_deleting: bool
max_filename_length: Optional[int]
yt_dlp_args: Optional[str]
detect_formats: Optional[str]
detect_formats: Optional[List[str]]
save_errors: Optional[str]


Expand Down Expand Up @@ -153,7 +153,7 @@ class DownloaderOptionalOptions(TypedDict, total=False):
sync_without_deleting: bool
max_filename_length: Optional[int]
yt_dlp_args: Optional[str]
detect_formats: Optional[str]
detect_formats: Optional[List[str]]
save_errors: Optional[str]


Expand Down
6 changes: 5 additions & 1 deletion spotdl/utils/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,11 @@ def parse_output_options(parser: _ArgumentGroup):
"--detect-formats",
type=str,
nargs="*",
help="Detect already downloaded songs with file format different from the --format option",
help=(
"Detect already downloaded songs with file format different from the --format option "
"(When combined with --m3u option, "
"only first detected format will be added to m3u file)"
),
choices=FFMPEG_FORMATS.keys(),
)

Expand Down
39 changes: 33 additions & 6 deletions spotdl/utils/m3u.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def create_m3u_content(
file_extension: str,
restrict: Optional[str] = None,
short: bool = False,
detect_formats: Optional[List[str]] = None,
) -> str:
"""
Create m3u content and return it as a string.
Expand All @@ -37,10 +38,24 @@ def create_m3u_content(

text = ""
for song in song_list:
text += (
str(create_file_name(song, template, file_extension, restrict, short))
+ "\n"
)
if not detect_formats:
file_name = create_file_name(
song, template, file_extension, restrict, short
)

text += str(file_name) + "\n"
else:
for file_ext in detect_formats:
file_name = create_file_name(song, template, file_ext, restrict, short)
if file_name.exists():
text += str(file_name) + "\n"
break
else:
file_name = create_file_name(
song, template, file_extension, restrict, short
)

text += str(file_name) + "\n"

return text

Expand All @@ -52,6 +67,7 @@ def gen_m3u_files(
file_extension: str,
restrict: Optional[str] = None,
short: bool = False,
detect_formats: Optional[List[str]] = None,
):
"""
Create an m3u8 filename from the query.
Expand All @@ -64,6 +80,7 @@ def gen_m3u_files(
- file_extension: the file extension to use
- restrict: sanitization to apply to the filename
- short: whether to use the short version of the template
- detect_formats: the formats to detect
"""

# If no file name is provided, use the first list's name
Expand All @@ -80,7 +97,7 @@ def gen_m3u_files(
file_name += "/{list[0]}.m3u8"

# Check if the file name ends with .m3u or .m3u8
if not file_name.endswith(".m3u") or not file_name.endswith(".m3u8"):
if not file_name.endswith(".m3u") and not file_name.endswith(".m3u8"):
file_name += ".m3u8"

lists_object: Dict[str, List[Song]] = {}
Expand All @@ -105,6 +122,7 @@ def gen_m3u_files(
file_extension,
restrict,
short,
detect_formats,
)
elif "{list[" in file_name and "]}" in file_name:
# Create a single m3u file for specified song list name
Expand All @@ -115,6 +133,7 @@ def gen_m3u_files(
file_extension,
restrict,
short,
detect_formats,
)
else:
# Use the provided file name
Expand All @@ -125,6 +144,7 @@ def gen_m3u_files(
file_extension,
restrict,
short,
detect_formats,
)


Expand All @@ -135,6 +155,7 @@ def create_m3u_file(
file_extension: str,
restrict: Optional[str] = None,
short: bool = False,
detect_formats: Optional[List[str]] = None,
) -> str:
"""
Create the m3u file.
Expand All @@ -146,13 +167,19 @@ def create_m3u_file(
- file_extension: the file extension to use
- restrict: sanitization to apply to the filename
- short: whether to use the short version of the template
- detect_formats: the formats to detect
### Returns
- the m3u content as a string
"""

m3u_content = create_m3u_content(
song_list, template, file_extension, restrict, short
song_list,
template,
file_extension,
restrict,
short,
detect_formats,
)

with open(file_name, "w", encoding="utf-8") as m3u_file:
Expand Down

0 comments on commit 8a8891e

Please sign in to comment.