Skip to content

Commit

Permalink
Sanitize playlist name while saving (#114)
Browse files Browse the repository at this point in the history
fixes #101
  • Loading branch information
SathyaBhat authored Oct 31, 2020
1 parent 6a63fe3 commit c6ed775
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
3 changes: 2 additions & 1 deletion spotify_dl/spotify.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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):
Expand Down
8 changes: 8 additions & 0 deletions spotify_dl/utils.py
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 10 additions & 2 deletions tests/test_spotify_link.py
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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'
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"

0 comments on commit c6ed775

Please sign in to comment.