#132 | #121: backoffice / dev-1.0-121 (#131)

cfr #121

Co-authored-by: Christophe Siraut <d@tobald.eu.org>
Co-authored-by: bkfox <thomas bkfox net>
Co-authored-by: Thomas Kairos <thomas@bkfox.net>
Reviewed-on: rc/aircox#131
Co-authored-by: Chris Tactic <ctactic@noreply.git.radiocampus.be>
Co-committed-by: Chris Tactic <ctactic@noreply.git.radiocampus.be>
This commit is contained in:
2024-04-28 22:02:09 +02:00
committed by Thomas Kairos
parent 1e17a1334a
commit 55123c386d
348 changed files with 124397 additions and 17879 deletions

View File

@ -21,23 +21,18 @@ parameters given by the setting SOUND_QUALITY. This script requires
Sox (and soxi).
"""
import logging
import os
import re
from datetime import date
import mutagen
from django.conf import settings as conf
from django.utils import timezone as tz
from django.utils.translation import gettext as _
from aircox import utils
from aircox.models import Program, Sound, Track
from aircox.models import Program, Sound, EpisodeSound
from .playlist_import import PlaylistImport
logger = logging.getLogger("aircox.commands")
__all__ = ("SoundFile",)
class SoundFile:
"""Handle synchronisation between sounds on files and database."""
@ -61,153 +56,40 @@ class SoundFile:
def sync(self, sound=None, program=None, deleted=False, keep_deleted=False, **kwargs):
"""Update related sound model and save it."""
if deleted:
return self._on_delete(self.path, keep_deleted)
self.sound = self._on_delete(self.path, keep_deleted)
return self.sound
# FIXME: sound.program as not null
if not program:
program = Program.get_from_path(self.path)
logger.debug('program from path "%s" -> %s', self.path, program)
kwargs["program_id"] = program.pk
program = sound and sound.program or Program.get_from_path(self.path)
if program:
kwargs["program_id"] = program.pk
if sound:
created = False
else:
created = False
if not sound:
sound, created = Sound.objects.get_or_create(file=self.sound_path, defaults=kwargs)
self.sound = sound
self.path_info = self.read_path(self.path)
sound.program = program
if created or sound.check_on_file():
sound.name = self.path_info.get("name")
self.info = self.read_file_info()
if self.info is not None:
sound.duration = utils.seconds_to_time(self.info.info.length)
# check for episode
if sound.episode is None and "year" in self.path_info:
sound.episode = self.find_episode(sound, self.path_info)
sound.sync_fs(on_update=True, find_playlist=True)
sound.save()
# check for playlist
self.find_playlist(sound)
if not sound.episodesound_set.all().exists():
self.create_episode_sound(sound)
return sound
def create_episode_sound(self, sound):
episode = sound.find_episode()
if episode:
# FIXME: position from name
item = EpisodeSound(
episode=episode, sound=sound, position=episode.episodesound_set.all().count(), broadcast=sound.broadcast
)
item.save()
def _on_delete(self, path, keep_deleted):
# TODO: remove from db on delete
sound = None
if keep_deleted:
sound = Sound.objects.path(self.path).first()
if sound:
if keep_deleted:
sound.type = sound.TYPE_REMOVED
sound.check_on_file()
sound.save()
return sound
else:
Sound.objects.path(self.path).delete()
def read_path(self, path):
"""Parse path name returning dictionary of extracted info. It can
contain:
- `year`, `month`, `day`: diffusion date
- `hour`, `minute`: diffusion time
- `n`: sound arbitrary number (used for sound ordering)
- `name`: cleaned name extracted or file name (without extension)
"""
basename = os.path.basename(path)
basename = os.path.splitext(basename)[0]
reg_match = self._path_re.search(basename)
if reg_match:
info = reg_match.groupdict()
for k in ("year", "month", "day", "hour", "minute", "n"):
if info.get(k) is not None:
info[k] = int(info[k])
name = info.get("name")
info["name"] = name and self._into_name(name) or basename
else:
info = {"name": basename}
return info
_path_re = re.compile(
"^(?P<year>[0-9]{4})(?P<month>[0-9]{2})(?P<day>[0-9]{2})"
"(_(?P<hour>[0-9]{2})h(?P<minute>[0-9]{2}))?"
"(_(?P<n>[0-9]+))?"
"_?[ -]*(?P<name>.*)$"
)
def _into_name(self, name):
name = name.replace("_", " ")
return " ".join(r.capitalize() for r in name.split(" "))
def read_file_info(self):
"""Read file information and metadata."""
try:
if os.path.exists(self.path):
return mutagen.File(self.path)
except Exception:
pass
return None
def find_episode(self, sound, path_info):
"""For a given program, check if there is an initial diffusion to
associate to, using the date info we have. Update self.sound and save
it consequently.
We only allow initial diffusion since there should be no rerun.
"""
program, pi = sound.program, path_info
if "year" not in pi or not sound or sound.episode:
return None
year, month, day = pi.get("year"), pi.get("month"), pi.get("day")
if pi.get("hour") is not None:
at = tz.datetime(year, month, day, pi.get("hour", 0), pi.get("minute", 0))
at = tz.make_aware(at)
else:
at = date(year, month, day)
diffusion = program.diffusion_set.at(at).first()
if not diffusion:
return None
logger.debug("%s <--> %s", sound.file.name, str(diffusion.episode))
return diffusion.episode
def find_playlist(self, sound=None, use_meta=True):
"""Find a playlist file corresponding to the sound path, such as:
my_sound.ogg => my_sound.csv.
Use sound's file metadata if no corresponding playlist has been
found and `use_meta` is True.
"""
if sound is None:
sound = self.sound
if sound.track_set.count() > 1:
return
# import playlist
path_noext, ext = os.path.splitext(self.sound.file.path)
path = path_noext + ".csv"
if os.path.exists(path):
PlaylistImport(path, sound=sound).run()
# use metadata
elif use_meta:
if self.info is None:
self.read_file_info()
if self.info and self.info.tags:
tags = self.info.tags
title, artist, album, year = tuple(
t and ", ".join(t) for t in (tags.get(k) for k in ("title", "artist", "album", "year"))
)
title = title or (self.path_info and self.path_info.get("name")) or os.path.basename(path_noext)
info = "{} ({})".format(album, year) if album and year else album or year or ""
track = Track(
sound=sound,
position=int(tags.get("tracknumber", 0)),
title=title,
artist=artist or _("unknown"),
info=info,
)
track.save()
if sound := Sound.objects.path(self.path).first():
sound.is_removed = True
sound.save(sync=False)
elif sound := Sound.objects.path(self.path):
sound.delete()
return sound

View File

@ -105,8 +105,7 @@ class MoveTask(Task):
def __call__(self, event, **kw):
sound = Sound.objects.filter(file=event.src_path).first()
if sound:
kw["sound"] = sound
kw["path"] = event.src_path
kw = {**kw, "sound": sound, "path": event.src_path}
else:
kw["path"] = event.dest_path
return super().__call__(event, **kw)
@ -214,15 +213,15 @@ class SoundMonitor:
logger.info(f"#{program.id} {program.title}")
self.scan_for_program(
program,
settings.SOUND_ARCHIVES_SUBDIR,
settings.SOUND_BROADCASTS_SUBDIR,
logger=logger,
type=Sound.TYPE_ARCHIVE,
broadcast=True,
)
self.scan_for_program(
program,
settings.SOUND_EXCERPTS_SUBDIR,
logger=logger,
type=Sound.TYPE_EXCERPT,
broadcast=False,
)
dirs.append(program.abspath)
return dirs
@ -234,12 +233,12 @@ class SoundMonitor:
if not program.ensure_dir(subdir):
return
subdir = os.path.join(program.abspath, subdir)
abs_subdir = os.path.join(program.abspath, subdir)
sounds = []
# sounds in directory
for path in os.listdir(subdir):
path = os.path.join(subdir, path)
for path in os.listdir(abs_subdir):
path = os.path.join(abs_subdir, path)
if not path.endswith(settings.SOUND_FILE_EXT):
continue
@ -248,14 +247,14 @@ class SoundMonitor:
sounds.append(sound_file.sound.pk)
# sounds in db & unchecked
sounds = Sound.objects.filter(file__startswith=subdir).exclude(pk__in=sounds)
sounds = Sound.objects.filter(file__startswith=program.path).exclude(pk__in=sounds)
self.check_sounds(sounds, program=program)
def check_sounds(self, qs, **sync_kwargs):
"""Only check for the sound existence or update."""
# check files
for sound in qs:
if sound.check_on_file():
if sound.sync_fs(on_update=True):
SoundFile(sound.file.path).sync(sound=sound, **sync_kwargs)
_running = False
@ -267,15 +266,15 @@ class SoundMonitor:
"""Run in monitor mode."""
with futures.ThreadPoolExecutor() as pool:
archives_handler = MonitorHandler(
settings.SOUND_ARCHIVES_SUBDIR,
settings.SOUND_BROADCASTS_SUBDIR,
pool,
type=Sound.TYPE_ARCHIVE,
broadcast=True,
logger=logger,
)
excerpts_handler = MonitorHandler(
settings.SOUND_EXCERPTS_SUBDIR,
pool,
type=Sound.TYPE_EXCERPT,
broadcast=False,
logger=logger,
)