Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitize file names before saving #152

Merged
merged 3 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions spotify_dl/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
def sanitize(name):
def sanitize(name, replace_with=''):
"""
Removes some of the reserved characters from the name so it can be saved
:param name: Name to be cleaned up
:return string containing the cleaned name
"""
clean_up_list = ["\\", "/", ":", "*", "?", "\"", "<", ">", "|", "\0"]
return "".join(c for c in name if c not in clean_up_list)
for x in clean_up_list:
name = name.replace(x, replace_with)
return name
5 changes: 3 additions & 2 deletions spotify_dl/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from mutagen.mp3 import MP3

from spotify_dl.scaffold import log
from spotify_dl.utils import sanitize


def download_songs(songs, download_directory, format_string, skip_mp3):
Expand Down Expand Up @@ -49,13 +50,13 @@ def download_songs(songs, download_directory, format_string, skip_mp3):
continue

if not skip_mp3:
song_file = MP3(path.join(download_directory, f"{song.get('artist')} - {song.get('name')}.mp3"),
song_file = MP3(path.join(download_directory, sanitize(f"{song.get('artist')} - {song.get('name')}.mp3", '#')), # youtube-dl automatically replaces with #
ID3=EasyID3)
song_file['date'] = song.get('year')
song_file['tracknumber'] = str(song.get('num')) + '/' + str(song.get('num_tracks'))
song_file['genre'] = song.get('genre')
song_file.save()
song_file = MP3(path.join(download_directory, f"{song.get('artist')} - {song.get('name')}.mp3"),
song_file = MP3(path.join(download_directory, sanitize(f"{song.get('artist')} - {song.get('name')}.mp3", '#')),
ID3=ID3)
song_file.tags['APIC'] = APIC(
encoding=3,
Expand Down