safe filename

This commit is contained in:
boyska 2017-03-09 23:43:35 +01:00
parent b8a9bee704
commit cd973ae368

View file

@ -3,16 +3,21 @@ from os.path import join, exists
import requests
import mutagen.mp3
import logging
import unicodedata
log = logging.getLogger(__name__)
def safefname(s):
asciified = unicodedata.normalize('NFKD', s).encode('ascii', 'ignore').decode('ascii')
return asciified.replace('/', '_').replace(' ', '_')
def download_track(track, args):
log.debug('Downloading "%s"' % track['title'])
log.debug('Downloading "%s"', track['title'])
if 'album' in track:
fdir = join(track['artist'], track['album']).replace(' ', '_')
fdir = join(safefname(track['artist']), safefname(track['album']))
else:
fdir = join(track['artist'])
fname = track['title'].replace(' ', '_') + '.mp3'
fdir = join(safefname(track['artist']))
fname = safefname(track['title'] + '.mp3')
fpath = join(args.download_base, fdir, fname)
os.makedirs(join(args.download_base, fdir), exist_ok=True)
if exists(fpath):