Skip to content

Commit

Permalink
Merge pull request #1311 from spotDL/dev
Browse files Browse the repository at this point in the history
Finally publish v3.6.2
  • Loading branch information
Silverarmor authored Jun 22, 2021
2 parents 58a0b4c + b92a514 commit c8eb2a9
Show file tree
Hide file tree
Showing 12 changed files with 52,288 additions and 47,082 deletions.
87 changes: 81 additions & 6 deletions spotdl/download/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,85 @@
# === Base functionality ===
# ==========================

# ========================
# === Helper function ===
# ========================

def _sanitize_filename(input_str: str) -> str:
output = input_str

# ! this is windows specific (disallowed chars)
output = "".join(char for char in output if char not in "/?\\*|<>")

# ! double quotes (") and semi-colons (:) are also disallowed characters but we would
# ! like to retain their equivalents, so they aren't removed in the prior loop
output = output.replace('"', "'").replace(':', '-')

return output


def _get_smaller_file_path(input_song: SongObj, output_format: str) -> Path:
# Only use the first artist if the song path turns out to be too long
smaller_name = f"{input_song.get_contributing_artists()[0]} - {input_song.get_song_name()}"

# ! this is windows specific (disallowed chars)
smaller_name = "".join(char for char in smaller_name if char not in "/?\\*|<>")

# ! double quotes (") and semi-colons (:) are also disallowed characters
# ! but we would like to retain their equivalents, so they aren't removed
# ! in the prior loop
smaller_name = smaller_name.replace('"', "'")
smaller_name = smaller_name.replace(':', '-')

smaller_name = _sanitize_filename(smaller_name)

try:
return Path(f"{smaller_name}.{output_format}").resolve()
except (OSError, WindowsError):
# Expected to happen in the rare case when the saved path is too long,
# even with the short filename
raise OSError("Cannot save song due to path issues.")


def _get_converted_file_path(song_obj: SongObj, output_format: str = None) -> Path:

# ! we eliminate contributing artist names that are also in the song name, else we
# ! would end up with things like 'Jetta, Mastubs - I'd love to change the world
# ! (Mastubs REMIX).mp3' which is kinda an odd file name.

# also make sure that main artist is included in artistStr even if they
# are in the song name, for example
# Lil Baby - Never Recover (Lil Baby & Gunna, Drake).mp3

artists_filtered = []

if output_format is None:
output_format = "mp3"

for artist in song_obj.get_contributing_artists():
if artist.lower() not in song_obj.get_song_name():
artists_filtered.append(artist)
elif artist.lower() is song_obj.get_contributing_artists()[0].lower():
artists_filtered.append(artist)

artist_str = ", ".join(artists_filtered)

converted_file_name = _sanitize_filename(
f"{artist_str} - {song_obj.get_song_name()}.{output_format}"
)

converted_file_path = Path(converted_file_name)

# ! Checks if a file name is too long (256 max on both linux and windows)
try:
if len(str(converted_file_path.resolve().name)) > 256:
print("Path was too long. Using Small Path.")
return _get_smaller_file_path(song_obj, output_format)
except (OSError, WindowsError):
return _get_smaller_file_path(song_obj, output_format)

return converted_file_path


# ===========================================================
# === The Download Manager (the tyrannical boss lady/guy) ===
Expand Down Expand Up @@ -159,11 +238,7 @@ async def download_song(self, songObj: SongObj) -> None:
if not tempFolder.exists():
tempFolder.mkdir()

convertedFileName = songObj.get_file_name()

convertedFilePath = Path(
".", f"{convertedFileName}.{self.arguments['format']}"
)
convertedFilePath = _get_converted_file_path(songObj, self.arguments["format"])

# if a song is already downloaded skip it
if convertedFilePath.is_file():
Expand Down Expand Up @@ -200,7 +275,7 @@ async def download_song(self, songObj: SongObj) -> None:
return None

downloadedFilePathString = await self._perform_audio_download_async(
convertedFileName, tempFolder, trackAudioStream
convertedFilePath.name, tempFolder, trackAudioStream
)

if downloadedFilePathString is None:
Expand Down
4 changes: 2 additions & 2 deletions spotdl/download/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ async def convert(
proc_out = await process.communicate()

if proc_out[0] is not None and proc_out[1]:
out = str(b''.join(proc_out))
out = str(b"".join(proc_out))
else:
out = ''
out = ""

if process.returncode != 0:
message = (
Expand Down
8 changes: 6 additions & 2 deletions spotdl/search/audioProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ def _query_and_simplify(searchTerm: str, filter: str) -> List[dict]:


def search_and_get_best_match(
songName: str, songArtists: List[str], songAlbumName: str, songDuration: int, isrc: str
songName: str,
songArtists: List[str],
songAlbumName: str,
songDuration: int,
isrc: str,
) -> typing.Optional[str]:
"""
`str` `songName` : name of song
Expand All @@ -163,7 +167,7 @@ def search_and_get_best_match(
isrcResult = isrcResults[0]

if isrcResult is not None:
return isrcResult['link']
return isrcResult["link"]

songTitle = create_song_title(songName, songArtists)

Expand Down
Loading

0 comments on commit c8eb2a9

Please sign in to comment.