From 0c328079a8b90d2e203f42f14e9ea140bb313b8e Mon Sep 17 00:00:00 2001 From: Ritiek Date: Wed, 21 Jun 2017 00:34:24 +0530 Subject: [PATCH] Fix encoding problems in python2 --- .gitignore | 2 +- core/misc.py | 19 +++++++++++-------- spotdl.py | 3 +-- test/.cache/v/cache/lastfailed | 1 + test/test_sample.py | 6 ++++++ 5 files changed, 20 insertions(+), 11 deletions(-) create mode 100644 test/.cache/v/cache/lastfailed create mode 100644 test/test_sample.py diff --git a/.gitignore b/.gitignore index 1159f8e..ffeaad5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ *.pyc -/core/__pycache__/ +__pycache__/ /Music/ /*.txt diff --git a/core/misc.py b/core/misc.py index 5987585..e268b3b 100644 --- a/core/misc.py +++ b/core/misc.py @@ -78,12 +78,14 @@ def feed_tracks(file, tracks): # generate filename of the song to be downloaded def generate_filename(title): - raw_title = title.replace(' ', '_') + # IMO python2 sucks dealing with unicode + title = fix_encoding(title, decode=True) + title = title.replace(' ', '_') # slugify removes any special characters - filename = slugify(raw_title, ok='-_()[]{}', lower=False) + filename = slugify(title, ok='-_()[]{}', lower=False) return filename -# please respect this user token :) +# please respect these credentials :) def generate_token(): creds = oauth2.SpotifyClientCredentials( client_id='4fe3fecfe5334023a1472516cc99d805', @@ -97,11 +99,12 @@ def generate_search_URL(song): return URL # fix encoding issues in python2 -def fix_encoding(query): - if sys.version_info > (3, 0): - return query - else: - return query.encode('utf-8') +def fix_encoding(query, decode=False): + if sys.version_info < (3, 0): + query = query.encode('utf-8') + if decode: + query = query.decode('utf-8') + return query def grace_quit(): print('') diff --git a/spotdl.py b/spotdl.py index a478231..c5fd806 100644 --- a/spotdl.py +++ b/spotdl.py @@ -297,8 +297,7 @@ def grab_single(raw_song, number=None): # otherwise print "[artist] - [song]" print(get_YouTube_title(content, number)) # generate file name of the song to download - title = misc.fix_encoding(content.title) - music_file = misc.generate_filename(title) + music_file = misc.generate_filename(content.title) if not check_exists(music_file, raw_song, islist=islist): download_song(content) print('') diff --git a/test/.cache/v/cache/lastfailed b/test/.cache/v/cache/lastfailed new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/test/.cache/v/cache/lastfailed @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/test_sample.py b/test/test_sample.py new file mode 100644 index 0000000..ff0cc08 --- /dev/null +++ b/test/test_sample.py @@ -0,0 +1,6 @@ +# content of test_sample.py +def func(x): + return x + 1 + +def test_answer(): + assert func(3) == 4