csv playlists can have comments

This commit is contained in:
bkfox 2017-01-04 17:56:13 +01:00
parent 15c59024b4
commit 1ccaf7e0da
2 changed files with 18 additions and 18 deletions

View File

@ -43,19 +43,13 @@ 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:
self.data = list(csv.reader( self.data = list(csv.DictReader(
file, (row for row in file if not row.startswith('#')),
fieldnames = settings.AIRCOX_IMPORT_PLAYLIST_CSV_COLS,
delimiter = settings.AIRCOX_IMPORT_PLAYLIST_CSV_DELIMITER, delimiter = settings.AIRCOX_IMPORT_PLAYLIST_CSV_DELIMITER,
quotechar = settings.AIRCOX_IMPORT_PLAYLIST_CSV_TEXT_QUOTE, quotechar = settings.AIRCOX_IMPORT_PLAYLIST_CSV_TEXT_QUOTE,
)) ))
def __get(self, line, field, default = None):
maps = settings.AIRCOX_IMPORT_PLAYLIST_CSV_COLS
if field not in maps:
return default
index = maps.index(field)
return line[index] if index < len(line) else default
def make_playlist(self, related, save = False): def make_playlist(self, related, save = False):
""" """
Make a playlist from the read data, and return it. If save is Make a playlist from the read data, and return it. If save is
@ -67,21 +61,21 @@ class Importer:
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 = \ position = \
int(self.__get(line, 'minutes', 0)) * 60 + \ int(line.get('minute') or 0) * 60 + \
int(self.__get(line, 'seconds', 0)) \ int(line.get('seconds') or 0) \
if in_seconds else index if in_seconds else index
track, created = Track.objects.get_or_create( track, created = Track.objects.get_or_create(
related_type = ContentType.objects.get_for_model(related), related_type = ContentType.objects.get_for_model(related),
related_id = related.pk, related_id = related.pk,
title = self.__get(line, 'title'), title = line.get('title'),
artist = self.__get(line, 'artist'), artist = line.get('artist'),
position = position, position = position,
) )
track.in_seconds = in_seconds track.in_seconds = in_seconds
track.info = self.__get(line, 'info') track.info = line.get('info')
tags = self.__get(line, 'tags') tags = line.get('tags')
if tags: if tags:
track.tags.add(*tags.split(',')) track.tags.add(*tags.split(','))
@ -120,13 +114,13 @@ class Command (BaseCommand):
path__icontains = options.get('sound') path__icontains = options.get('sound')
).first() ).first()
else: else:
path, ext = os.path.splitext(options.get('path')) path_, ext = os.path.splitext(path)
related = Sound.objects.filter(path__icontains = path).first() related = Sound.objects.filter(path__icontains = path_).first()
if not related: if not related:
logger.error('no sound found in the database for the path ' \ logger.error('no sound found in the database for the path ' \
'{path}'.format(path=path)) '{path}'.format(path=path))
return -1 return
if options.get('diffusion') and related.diffusion: if options.get('diffusion') and related.diffusion:
related = related.diffusion related = related.diffusion

View File

@ -280,6 +280,7 @@ class Command(BaseCommand):
logger.info('scan all programs...') logger.info('scan all programs...')
programs = Program.objects.filter() programs = Program.objects.filter()
dirs = []
for program in programs: for program in programs:
logger.info('#%d %s', program.id, program.name) logger.info('#%d %s', program.id, program.name)
self.scan_for_program( self.scan_for_program(
@ -290,6 +291,11 @@ class Command(BaseCommand):
program, settings.AIRCOX_SOUND_EXCERPTS_SUBDIR, program, settings.AIRCOX_SOUND_EXCERPTS_SUBDIR,
type = Sound.Type.excerpt, type = Sound.Type.excerpt,
) )
dirs.append(os.path.join(program.path))
# extra scan for files that are not in programs' dir anymore
# TODO
def scan_for_program(self, program, subdir, **sound_kwargs): def scan_for_program(self, program, subdir, **sound_kwargs):
""" """