fix error in playlist import + more verbose command

This commit is contained in:
bkfox 2017-01-04 18:12:58 +01:00
parent 1ccaf7e0da
commit 8bbeb5fe6d
2 changed files with 32 additions and 17 deletions

View File

@ -26,6 +26,7 @@ logger = logging.getLogger('aircox.tools')
class Importer: class Importer:
path = None
data = None data = None
tracks = None tracks = None
@ -43,6 +44,8 @@ class Importer:
if not os.path.exists(path): if not os.path.exists(path):
return True return True
with open(path, 'r') as file: with open(path, 'r') as file:
logger.info('start reading csv ' + path)
self.path = path
self.data = list(csv.DictReader( self.data = list(csv.DictReader(
(row for row in file if not row.startswith('#')), (row for row in file if not row.startswith('#')),
fieldnames = settings.AIRCOX_IMPORT_PLAYLIST_CSV_COLS, fieldnames = settings.AIRCOX_IMPORT_PLAYLIST_CSV_COLS,
@ -58,26 +61,35 @@ class Importer:
maps = settings.AIRCOX_IMPORT_PLAYLIST_CSV_COLS maps = settings.AIRCOX_IMPORT_PLAYLIST_CSV_COLS
tracks = [] tracks = []
logger.info('parse csv file ' + self.path)
in_seconds = ('minutes' or 'seconds') in maps in_seconds = ('minutes' or 'seconds') in maps
for index, line in enumerate(self.data): for index, line in enumerate(self.data):
position = \ if ('title' or 'artist') not in line:
int(line.get('minute') or 0) * 60 + \ return
int(line.get('seconds') or 0) \
if in_seconds else index
track, created = Track.objects.get_or_create( try:
related_type = ContentType.objects.get_for_model(related), position = \
related_id = related.pk, int(line.get('minute') or 0) * 60 + \
title = line.get('title'), int(line.get('seconds') or 0) \
artist = line.get('artist'), if in_seconds else index
position = position,
)
track.in_seconds = in_seconds track, created = Track.objects.get_or_create(
track.info = line.get('info') related_type = ContentType.objects.get_for_model(related),
tags = line.get('tags') related_id = related.pk,
if tags: title = line.get('title'),
track.tags.add(*tags.split(',')) artist = line.get('artist'),
position = position,
)
track.in_seconds = in_seconds
track.info = line.get('info')
tags = line.get('tags')
if tags:
track.tags.add(*tags.split(','))
except:
logger.warning('an error occured for track {index}, it may not '
'have been saved'.format(index = index))
continue
if save: if save:
track.save() track.save()

View File

@ -1004,12 +1004,15 @@ class Sound(Nameable):
except: except:
meta = {} meta = {}
if meta is None:
meta = {}
def get_meta(key, cast=str): def get_meta(key, cast=str):
value = meta.get(key) value = meta.get(key)
return cast(value[0]) if value else None return cast(value[0]) if value else None
info = '{} ({})'.format(get_meta('album'), get_meta('year')) \ info = '{} ({})'.format(get_meta('album'), get_meta('year')) \
if 'album' and 'year' in meta else \ if meta and ('album' and 'year' in meta) else \
get_meta('album') \ get_meta('album') \
if 'album' else \ if 'album' else \
('year' in meta) and get_meta('year') or '' ('year' in meta) and get_meta('year') or ''