create_tracks_from_nonstop.py 1.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
import mutagen

from django.core.management.base import BaseCommand, CommandError

from ...models import NonstopFile, Track, Artist

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

class Command(BaseCommand):
    def handle(self, verbosity, **kwargs):
        self.verbose = (int(verbosity) > 1)
        kwargs = {'track__isnull': True}
        #kwargs = {}
        for nonstopfile in NonstopFile.objects.filter(**kwargs):
            filepath = nonstopfile.filepath.replace(REMOTE_BASE_PATH, LOCAL_BASE_PATH)
            if self.verbose:
                print filepath[len(LOCAL_BASE_PATH):]
            if 'SPOTS/' in filepath:
                continue
fred's avatar
fred committed
21
            metadata = mutagen.File(filepath, easy=True)
22
            if not metadata or not metadata.get('artist') or not metadata.get('title'):
23 24
                if self.verbose:
                    print 'skipping', filepath
25
                continue
fred's avatar
fred committed
26 27
            artist, created = Artist.objects.get_or_create(name=metadata.get('artist')[0])
            track, created = Track.objects.get_or_create(title=metadata.get('title')[0],
28 29 30 31 32 33 34 35 36 37 38
                            artist=artist)
            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.save()