Fix encoding problems hopefully

This commit is contained in:
Ritiek
2017-06-21 14:13:32 +05:30
parent 5aa8c66db3
commit acfb2f0173
3 changed files with 17 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
from mutagen.easyid3 import EasyID3
from mutagen.id3 import ID3, APIC
from mutagen.mp4 import MP4, MP4Cover
import sys
# urllib2 is urllib.request in python3
try:
@@ -25,6 +26,8 @@ def compare(file, metadata):
return already_tagged
def embed(music_file, meta_tags, output_ext):
if sys.version_info < (3, 0):
music_file = music_file.encode('utf-8')
if meta_tags is None:
print('Could not find meta-tags')
elif output_ext == '.m4a':

View File

@@ -79,7 +79,8 @@ def feed_tracks(file, tracks):
# generate filename of the song to be downloaded
def generate_filename(title):
# IMO python2 sucks dealing with unicode
title = fix_encoding(title, decode=True)
title = fix_encoding(title)
title = fix_decoding(title)
title = title.replace(' ', '_')
# slugify removes any special characters
filename = slugify(title, ok='-_()[]{}', lower=False)
@@ -99,11 +100,14 @@ def generate_search_URL(song):
return URL
# fix encoding issues in python2
def fix_encoding(query, decode=False):
def fix_encoding(query):
if sys.version_info < (3, 0):
query = query.encode('utf-8')
if decode:
query = query.decode('utf-8')
return query
def fix_decoding(query):
if sys.version_info < (3, 0):
query = query.decode('utf-8')
return query
def grace_quit():

View File

@@ -58,7 +58,7 @@ def generate_YouTube_URL(raw_song):
# generate direct search YouTube URL
searchURL = misc.generate_search_URL(song)
item = urllib2.urlopen(searchURL).read()
item = unicode(item, 'utf-8')
#item = unicode(item, 'utf-8')
items_parse = BeautifulSoup(item, "html.parser")
check = 1
if args.manual:
@@ -153,6 +153,7 @@ def download_song(content):
def convert_song(music_file):
# skip conversion if input_ext == output_ext
if not args.input_ext == args.output_ext:
music_file = music_file.encode('utf-8')
print('Converting ' + music_file + args.input_ext + ' to ' + args.output_ext[1:])
if args.avconv:
convert_with_avconv(music_file)
@@ -225,7 +226,9 @@ def check_exists(music_file, raw_song, islist):
os.remove("Music/" + file)
continue
# check if any file with similar name is already present in Music/
if file.startswith(misc.generate_filename(music_file)):
dfile = misc.fix_decoding(file)
umfile = misc.fix_decoding(misc.generate_filename(music_file))
if dfile.startswith(umfile):
# check if the already downloaded song has correct metadata
already_tagged = metadata.compare(file, generate_metadata(raw_song))
# if not, remove it and download again without prompt
@@ -299,6 +302,7 @@ def grab_single(raw_song, number=None):
print(get_YouTube_title(content, number))
# generate file name of the song to download
music_file = misc.generate_filename(content.title)
music_file = misc.fix_decoding(music_file)
if not check_exists(music_file, raw_song, islist=islist):
download_song(content)
print('')