]> git.0d.be Git - django-panik-nonstop.git/blob - nonstop/management/commands/create_tracks_from_nonstop.py
ignore mutagen failures
[django-panik-nonstop.git] / nonstop / management / commands / create_tracks_from_nonstop.py
1 import mutagen
2 import os
3
4 from django.core.management.base import BaseCommand, CommandError
5 from django.utils.text import slugify
6
7 from ...models import NonstopFile, Track, Artist
8 from emissions.models import Nonstop
9
10 LOCAL_BASE_PATH = '/media/nonstop/'
11 REMOTE_BASE_PATH = '/srv/soma/nonstop/'
12
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'
23 }
24
25
26 class Command(BaseCommand):
27     def handle(self, verbosity, **kwargs):
28         self.verbose = (int(verbosity) > 1)
29         kwargs = {'track__isnull': True}
30         #kwargs = {}
31         for tranche_key, tranche_value in tranche_slug_mapping.items():
32             try:
33                 tranche_slug_mapping[tranche_key] = Nonstop.objects.get(slug=tranche_value)
34             except Nonstop.DoesNotExist:
35                 continue
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'):
40                 continue
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):]
44                 continue
45             if self.verbose:
46                 print short_filepath
47             try:
48                 metadata = mutagen.File(filepath, easy=True)
49             except Exception as e:
50                 if self.verbose:
51                     print 'E:', e
52                 continue
53             if not metadata or not metadata.get('artist') or not metadata.get('title'):
54                 if self.verbose:
55                     print 'skipping', filepath
56                 continue
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],
59                             artist=artist)
60             if '/Tranches/' in filepath:
61                 tranche_name = filepath[filepath.find('Tranches/'):].split('/')[1]
62                 try:
63                     track.nonstop_zones = [tranche_slug_mapping[tranche_name]]
64                 except KeyError:
65                     pass
66             if ' FR' in filepath:
67                 track.language = 'fr'
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:
72                     track.sabam = False
73                     break
74             nonstopfile.track = track
75             nonstopfile.filename = os.path.basename(nonstopfile.filepath)
76             nonstopfile.save()
77             track.save()