aircox-radiocampus/aircox/management/commands/import_playlist.py
Thomas Kairos f9ad81ddac !111: tests: aircox.management (#114)
!111

Co-authored-by: bkfox <thomas bkfox net>
Reviewed-on: rc/aircox#114
2023-06-30 16:39:55 +02:00

76 lines
2.2 KiB
Python
Executable File

"""Import one or more playlist for the given sound. Attach it to the provided
sound.
Playlists are in CSV format, where columns are separated with a
'{settings.IMPORT_PLAYLIST_CSV_DELIMITER}'. Text quote is
{settings.IMPORT_PLAYLIST_CSV_TEXT_QUOTE}.
The order of the elements is: {settings.IMPORT_PLAYLIST_CSV_COLS}
If 'minutes' or 'seconds' are given, position will be expressed as timed
position, instead of position in playlist.
"""
import logging
import os
from argparse import RawTextHelpFormatter
from django.core.management.base import BaseCommand
from aircox.conf import settings
from aircox.models import Sound
from aircox.controllers.playlist_import import PlaylistImport
__doc__ = __doc__.format(settings=settings)
__all__ = ("Command",)
logger = logging.getLogger("aircox.commands")
class Command(BaseCommand):
help = __doc__
def add_arguments(self, parser):
parser.formatter_class = RawTextHelpFormatter
parser.add_argument(
"path",
metavar="PATH",
type=str,
help="path of the input playlist to read",
)
parser.add_argument(
"--sound",
"-s",
type=str,
help="generate a playlist for the sound of the given path. "
"If not given, try to match a sound with the same path.",
)
def handle(self, path, *args, **options):
# FIXME: absolute/relative path of sounds vs given path
if options.get("sound"):
sound = Sound.objects.filter(
file__icontains=options.get("sound")
).first()
else:
path_, ext = os.path.splitext(path)
sound = Sound.objects.filter(path__icontains=path_).first()
if not sound:
logger.error(
"no sound found in the database for the path "
"{path}".format(path=path)
)
return
# FIXME: auto get sound.episode if any
importer = PlaylistImport(path, sound=sound).run()
for track in importer.tracks:
logger.info(
"track #{pos} imported: {title}, by {artist}".format(
pos=track.position, title=track.title, artist=track.artist
)
)