WIP - Sound.file instead of Sound.path; fix issues with player; program.path is now relative

This commit is contained in:
bkfox
2022-03-18 14:12:59 +01:00
parent d17d6831dd
commit e3b744be70
17 changed files with 109 additions and 118 deletions

View File

@ -128,7 +128,7 @@ class Command(BaseCommand):
def handle(self, path, *args, **options):
# FIXME: absolute/relative path of sounds vs given path
if options.get('sound'):
sound = Sound.objects.filter(path__icontains=options.get('sound'))\
sound = Sound.objects.filter(file__icontains=options.get('sound'))\
.first()
else:
path_, ext = os.path.splitext(path)

View File

@ -36,6 +36,7 @@ import mutagen
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler, FileModifiedEvent
from django.conf import settings as conf
from django.core.management.base import BaseCommand, CommandError
from django.utils import timezone as tz
from django.utils.translation import gettext as _
@ -64,12 +65,16 @@ class SoundFile:
def __init__(self, path):
self.path = path
@property
def sound_path(self):
return self.path.replace(conf.MEDIA_ROOT + '/', '')
def sync(self, sound=None, program=None, deleted=False, **kwargs):
"""
Update related sound model and save it.
"""
if deleted:
sound = Sound.objects.filter(path=self.path).first()
sound = Sound.objects.filter(file=self.path).first()
if sound:
sound.type = sound.TYPE_REMOVED
sound.check_on_file()
@ -78,13 +83,13 @@ class SoundFile:
# FIXME: sound.program as not null
program = kwargs['program'] = Program.get_from_path(self.path)
sound, created = Sound.objects.get_or_create(path=self.path, defaults=kwargs) \
sound, created = Sound.objects.get_or_create(file=self.sound_path, defaults=kwargs) \
if not sound else (sound, False)
self.sound = sound
sound.program = program
if created or sound.check_on_file():
logger.info('sound is new or have been modified -> %s', self.path)
logger.info('sound is new or have been modified -> %s', self.sound_path)
self.read_path()
sound.name = self.path_info.get('name')
@ -153,7 +158,7 @@ class SoundFile:
if not diffusion:
return None
logger.info('%s <--> %s', self.sound.path, str(diffusion.episode))
logger.info('%s <--> %s', self.sound.file.name, str(diffusion.episode))
self.sound.episode = diffusion.episode
return diffusion
@ -172,7 +177,7 @@ class SoundFile:
return
# import playlist
path = os.path.splitext(self.sound.path)[0] + '.csv'
path = os.path.splitext(self.sound.file.path)[0] + '.csv'
if os.path.exists(path):
PlaylistImport(path, sound=sound).run()
# use metadata
@ -227,7 +232,7 @@ class MonitorHandler(PatternMatchingEventHandler):
def on_moved(self, event):
logger.info('sound moved: %s -> %s', event.src_path, event.dest_path)
def moved(event, sound_kwargs):
sound = Sound.objects.filter(path=event.src_path)
sound = Sound.objects.filter(file=event.src_path)
sound_file = SoundFile(event.dest_path) if not sound else sound
sound_file.sync(**sound_kwargs)
self.pool.submit(moved, event, self.sound_kwargs)
@ -268,7 +273,8 @@ class Command(BaseCommand):
program, settings.AIRCOX_SOUND_EXCERPTS_SUBDIR,
type=Sound.TYPE_EXCERPT,
)
dirs.append(os.path.join(program.path))
dirs.append(os.path.join(program.abspath))
return dirs
def scan_for_program(self, program, subdir, **sound_kwargs):
"""
@ -279,7 +285,7 @@ class Command(BaseCommand):
if not program.ensure_dir(subdir):
return
subdir = os.path.join(program.path, subdir)
subdir = os.path.join(program.abspath, subdir)
sounds = []
# sounds in directory
@ -293,7 +299,7 @@ class Command(BaseCommand):
sounds.append(sound_file.sound.pk)
# sounds in db & unchecked
sounds = Sound.objects.filter(path__startswith=subdir). \
sounds = Sound.objects.filter(file__startswith=subdir). \
exclude(pk__in=sounds)
self.check_sounds(sounds, program=program)
@ -302,7 +308,7 @@ class Command(BaseCommand):
# check files
for sound in qs:
if sound.check_on_file():
SoundFile(sound.path).sync(sound=sound, **sync_kwargs)
SoundFile(sound.file.path).sync(sound=sound, **sync_kwargs)
def monitor(self):
""" Run in monitor mode """

View File

@ -160,7 +160,7 @@ class Command (BaseCommand):
self.bad = []
self.good = []
for sound in self.sounds:
logger.info('analyse ' + sound.path)
logger.info('analyse ' + sound.file.name)
sound.analyse()
sound.check(attr, minmax[0], minmax[1])
if sound.bad:
@ -171,6 +171,6 @@ class Command (BaseCommand):
# resume
if options.get('resume'):
for sound in self.good:
logger.info('\033[92m+ %s\033[0m', sound.path)
logger.info('\033[92m+ %s\033[0m', sound.file.name)
for sound in self.bad:
logger.info('\033[91m+ %s\033[0m', sound.path)
logger.info('\033[91m+ %s\033[0m', sound.file.name)