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):
return True
with open(path, 'r') as file:
self.data = list(csv.reader(
file,
self.data = list(csv.DictReader(
(row for row in file if not row.startswith('#')),
fieldnames = settings.AIRCOX_IMPORT_PLAYLIST_CSV_COLS,
delimiter = settings.AIRCOX_IMPORT_PLAYLIST_CSV_DELIMITER,
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):
"""
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
for index, line in enumerate(self.data):
position = \
int(self.__get(line, 'minutes', 0)) * 60 + \
int(self.__get(line, 'seconds', 0)) \
int(line.get('minute') or 0) * 60 + \
int(line.get('seconds') or 0) \
if in_seconds else index
track, created = Track.objects.get_or_create(
related_type = ContentType.objects.get_for_model(related),
related_id = related.pk,
title = self.__get(line, 'title'),
artist = self.__get(line, 'artist'),
title = line.get('title'),
artist = line.get('artist'),
position = position,
)
track.in_seconds = in_seconds
track.info = self.__get(line, 'info')
tags = self.__get(line, 'tags')
track.info = line.get('info')
tags = line.get('tags')
if tags:
track.tags.add(*tags.split(','))
@ -120,13 +114,13 @@ class Command (BaseCommand):
path__icontains = options.get('sound')
).first()
else:
path, ext = os.path.splitext(options.get('path'))
related = Sound.objects.filter(path__icontains = path).first()
path_, ext = os.path.splitext(path)
related = Sound.objects.filter(path__icontains = path_).first()
if not related:
logger.error('no sound found in the database for the path ' \
'{path}'.format(path=path))
return -1
return
if options.get('diffusion') and related.diffusion:
related = related.diffusion

View File

@ -280,6 +280,7 @@ class Command(BaseCommand):
logger.info('scan all programs...')
programs = Program.objects.filter()
dirs = []
for program in programs:
logger.info('#%d %s', program.id, program.name)
self.scan_for_program(
@ -290,6 +291,11 @@ class Command(BaseCommand):
program, settings.AIRCOX_SOUND_EXCERPTS_SUBDIR,
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):
"""