Fix encoding problems in python2

This commit is contained in:
Ritiek
2017-06-21 00:34:24 +05:30
parent f36b3cc3f0
commit 0c328079a8
5 changed files with 20 additions and 11 deletions

2
.gitignore vendored
View File

@@ -1,4 +1,4 @@
*.pyc
/core/__pycache__/
__pycache__/
/Music/
/*.txt

View File

@@ -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('')

View File

@@ -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('')

1
test/.cache/v/cache/lastfailed vendored Normal file
View File

@@ -0,0 +1 @@
{}

6
test/test_sample.py Normal file
View File

@@ -0,0 +1,6 @@
# content of test_sample.py
def func(x):
return x + 1
def test_answer():
assert func(3) == 4