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 requests
import mutagen.mp3 import mutagen.mp3
import logging import logging
import unicodedata
log = logging.getLogger(__name__) 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): def download_track(track, args):
log.debug('Downloading "%s"' % track['title']) log.debug('Downloading "%s"', track['title'])
if 'album' in track: if 'album' in track:
fdir = join(track['artist'], track['album']).replace(' ', '_') fdir = join(safefname(track['artist']), safefname(track['album']))
else: else:
fdir = join(track['artist']) fdir = join(safefname(track['artist']))
fname = track['title'].replace(' ', '_') + '.mp3' fname = safefname(track['title'] + '.mp3')
fpath = join(args.download_base, fdir, fname) fpath = join(args.download_base, fdir, fname)
os.makedirs(join(args.download_base, fdir), exist_ok=True) os.makedirs(join(args.download_base, fdir), exist_ok=True)
if exists(fpath): if exists(fpath):