import mutagen import os from django.core.management.base import BaseCommand, CommandError from django.utils.text import slugify from ...models import NonstopFile, Track, Artist from emissions.models import Nonstop LOCAL_BASE_PATH = '/media/nonstop/' REMOTE_BASE_PATH = '/srv/soma/nonstop/' tranche_slug_mapping = { 'Acouphene': 'acouphene', 'Biodiversite': 'biodiversite', 'Heure_de_pointe': 'l-heure-de-pointe', 'Hop_Bop_and_co': 'hop-bop-co', 'la_panique': 'la-panique', 'Mange_Disque': 'le-mange-disque', 'Matins_tranquilles': 'matin-tranquille', 'Reveries': 'reveries', 'Up_Beat_Tempo': 'up-beat-tempo' } class Command(BaseCommand): def handle(self, verbosity, **kwargs): self.verbose = (int(verbosity) > 1) kwargs = {'track__isnull': True} #kwargs = {} for tranche_key, tranche_value in tranche_slug_mapping.items(): try: tranche_slug_mapping[tranche_key] = Nonstop.objects.get(slug=tranche_value) except Nonstop.DoesNotExist: continue for nonstopfile in NonstopFile.objects.filter(**kwargs): filepath = nonstopfile.filepath.replace(REMOTE_BASE_PATH, LOCAL_BASE_PATH) short_filepath = filepath[len(LOCAL_BASE_PATH):] if short_filepath.startswith('SPOTS'): continue if not os.path.exists(filepath): if self.verbose and short_filepath.startswith('Tranches'): print 'missing file:', filepath[len(LOCAL_BASE_PATH):] continue if self.verbose: print short_filepath try: metadata = mutagen.File(filepath, easy=True) except Exception as e: if self.verbose: print 'E:', e continue if not metadata or not metadata.get('artist') or not metadata.get('title'): if self.verbose: print 'skipping', filepath continue artist, created = Artist.objects.get_or_create(name=metadata.get('artist')[0]) track, created = Track.objects.get_or_create(title=metadata.get('title')[0], artist=artist) if '/Tranches/' in filepath: tranche_name = filepath[filepath.find('Tranches/'):].split('/')[1] try: track.nonstop_zones = [tranche_slug_mapping[tranche_name]] except KeyError: pass if ' FR' in filepath: track.language = 'fr' track.cfwb = (' CFWB' in filepath) track.instru = (' INSTRU' in filepath) for needle in (' CC', '(CC', 'DOM PUB'): if needle in filepath: track.sabam = False break nonstopfile.track = track nonstopfile.filename = os.path.basename(nonstopfile.filepath) nonstopfile.save() track.save()