From c6ed77503e71b603effcda59340f2a7602d5f7a6 Mon Sep 17 00:00:00 2001 From: Sathyajith Bhat Date: Sat, 31 Oct 2020 12:29:35 +0200 Subject: [PATCH] Sanitize playlist name while saving (#114) fixes #101 --- spotify_dl/spotify.py | 3 ++- spotify_dl/utils.py | 8 ++++++++ tests/test_spotify_link.py | 12 ++++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 spotify_dl/utils.py diff --git a/spotify_dl/spotify.py b/spotify_dl/spotify.py index e4357982..554c09c9 100644 --- a/spotify_dl/spotify.py +++ b/spotify_dl/spotify.py @@ -1,5 +1,6 @@ import youtube_dl from spotify_dl.scaffold import * +from spotify_dl.utils import sanitize def fetch_tracks(sp, item_type, url): @@ -117,7 +118,7 @@ def get_item_name(sp, item_type, item_id): name = sp.album(album_id=item_id).get('name') elif item_type == 'track': name = sp.track(track_id=item_id).get('name') - return name + return sanitize(name) def validate_spotify_url(url): diff --git a/spotify_dl/utils.py b/spotify_dl/utils.py new file mode 100644 index 00000000..ceb5af0b --- /dev/null +++ b/spotify_dl/utils.py @@ -0,0 +1,8 @@ +def sanitize(name): + """ + 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) diff --git a/tests/test_spotify_link.py b/tests/test_spotify_link.py index d217673c..0175cd0f 100644 --- a/tests/test_spotify_link.py +++ b/tests/test_spotify_link.py @@ -1,4 +1,5 @@ -from spotify_dl.spotify import parse_spotify_url +from spotify_dl.spotify import parse_spotify_url, get_item_name +from tests.test_spotify_fetch_tracks import spotify_auth def test_parse_spotify_url(): album_url = "https://open.spotify.com/album/aabbccddee" @@ -15,4 +16,11 @@ def test_parse_spotify_url(): item_type, item_id = parse_spotify_url(track_url) assert item_type == 'track' - assert item_id == 'aabbccddee' \ No newline at end of file + assert item_id == 'aabbccddee' + +def test_get_item_name(): + url = "https://open.spotify.com/playlist/75RZ95bDNlAPsQXaGcEqDZ" + sp = spotify_auth() + item_type, item_id = parse_spotify_url(url) + name = get_item_name(sp, item_type, item_id) + assert name == "Dank Tunes V The Dranks Strike Back" \ No newline at end of file