4 from django.core.management.base import BaseCommand, CommandError
5 from django.utils.text import slugify
7 from ...models import NonstopFile, Track, Artist
8 from emissions.models import Nonstop
10 LOCAL_BASE_PATH = '/media/nonstop/'
11 REMOTE_BASE_PATH = '/srv/soma/nonstop/'
13 tranche_slug_mapping = {
14 'Acouphene': 'acouphene',
15 'Biodiversite': 'biodiversite',
16 'Heure_de_pointe': 'l-heure-de-pointe',
17 'Hop_Bop_and_co': 'hop-bop-co',
18 'la_panique': 'la-panique',
19 'Mange_Disque': 'le-mange-disque',
20 'Matins_tranquilles': 'matin-tranquille',
21 'Reveries': 'reveries',
22 'Up_Beat_Tempo': 'up-beat-tempo'
26 class Command(BaseCommand):
27 def handle(self, verbosity, **kwargs):
28 self.verbose = (int(verbosity) > 1)
29 kwargs = {'track__isnull': True}
31 for tranche_key, tranche_value in tranche_slug_mapping.items():
33 tranche_slug_mapping[tranche_key] = Nonstop.objects.get(slug=tranche_value)
34 except Nonstop.DoesNotExist:
36 for nonstopfile in NonstopFile.objects.filter(**kwargs):
37 filepath = nonstopfile.filepath.replace(REMOTE_BASE_PATH, LOCAL_BASE_PATH)
38 short_filepath = filepath[len(LOCAL_BASE_PATH):]
39 if short_filepath.startswith('SPOTS'):
41 if not os.path.exists(filepath):
42 if self.verbose and short_filepath.startswith('Tranches'):
43 print 'missing file:', filepath[len(LOCAL_BASE_PATH):]
48 metadata = mutagen.File(filepath, easy=True)
49 except Exception as e:
53 if not metadata or not metadata.get('artist') or not metadata.get('title'):
55 print 'skipping', filepath
57 artist, created = Artist.objects.get_or_create(name=metadata.get('artist')[0])
58 track, created = Track.objects.get_or_create(title=metadata.get('title')[0],
60 if '/Tranches/' in filepath:
61 tranche_name = filepath[filepath.find('Tranches/'):].split('/')[1]
63 track.nonstop_zones = [tranche_slug_mapping[tranche_name]]
68 track.cfwb = (' CFWB' in filepath)
69 track.instru = (' INSTRU' in filepath)
70 for needle in (' CC', '(CC', 'DOM PUB'):
71 if needle in filepath:
74 nonstopfile.track = track
75 nonstopfile.filename = os.path.basename(nonstopfile.filepath)