create_tracks_from_nonstop.py 2.98 KB
Newer Older
1
import mutagen
fred's avatar
fred committed
2
import os
3 4

from django.core.management.base import BaseCommand, CommandError
fred's avatar
fred committed
5
from django.utils.text import slugify
6 7

from ...models import NonstopFile, Track, Artist
fred's avatar
fred committed
8
from emissions.models import Nonstop
9 10 11 12

LOCAL_BASE_PATH = '/media/nonstop/'
REMOTE_BASE_PATH = '/srv/soma/nonstop/'

fred's avatar
fred committed
13 14 15 16 17 18 19 20 21 22 23 24 25
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'
}


26 27 28 29 30
class Command(BaseCommand):
    def handle(self, verbosity, **kwargs):
        self.verbose = (int(verbosity) > 1)
        kwargs = {'track__isnull': True}
        #kwargs = {}
fred's avatar
fred committed
31 32 33 34 35
        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
36 37
        for nonstopfile in NonstopFile.objects.filter(**kwargs):
            filepath = nonstopfile.filepath.replace(REMOTE_BASE_PATH, LOCAL_BASE_PATH)
fred's avatar
fred committed
38 39
            short_filepath = filepath[len(LOCAL_BASE_PATH):]
            if short_filepath.startswith('SPOTS'):
40
                continue
fred's avatar
fred committed
41
            if not os.path.exists(filepath):
fred's avatar
fred committed
42
                if self.verbose and short_filepath.startswith('Tranches'):
fred's avatar
fred committed
43 44
                    print 'missing file:', filepath[len(LOCAL_BASE_PATH):]
                continue
fred's avatar
fred committed
45 46
            if self.verbose:
                print short_filepath
fred's avatar
fred committed
47 48 49 50 51 52
            try:
                metadata = mutagen.File(filepath, easy=True)
            except Exception as e:
                if self.verbose:
                    print 'E:', e
                continue
53
            if not metadata or not metadata.get('artist') or not metadata.get('title'):
54 55
                if self.verbose:
                    print 'skipping', filepath
56
                continue
fred's avatar
fred committed
57 58
            artist, created = Artist.objects.get_or_create(name=metadata.get('artist')[0])
            track, created = Track.objects.get_or_create(title=metadata.get('title')[0],
59
                            artist=artist)
fred's avatar
fred committed
60 61 62 63 64 65
            if '/Tranches/' in filepath:
                tranche_name = filepath[filepath.find('Tranches/'):].split('/')[1]
                try:
                    track.nonstop_zones = [tranche_slug_mapping[tranche_name]]
                except KeyError:
                    pass
66 67 68 69 70 71 72 73 74
            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
fred's avatar
fred committed
75
            nonstopfile.filename = os.path.basename(nonstopfile.filepath)
76
            nonstopfile.save()
fred's avatar
fred committed
77
            track.save()