Esempio n. 1
0
    def process_task(self,filepath):
        # what sort of file is it? Guess the model to use from extension.
        root, ext = splitext(filepath)

        if islink(filepath):
            raise TaskSkipped('Linked file is probably mapped to CAS')

        #if not access(filepath,R_OK):
        #    raise TaskError('Could not access %s' % filepath)

        if ext == '.mp3':
            AudioFile.from_mp3(filepath).save()

        elif ext == '.aac':
            AudioFile.from_aac(filepath).save()
        else:
            raise TaskSkipped('Not a supported file %s' % filepath)
Esempio n. 2
0
    def process_task(self, filepath):
        # what sort of file is it? Guess the model to use from extension.
        root, ext = splitext(filepath)

        if islink(filepath):
            raise TaskSkipped('Linked file is probably mapped to CAS')

        #if not access(filepath,R_OK):
        #    raise TaskError('Could not access %s' % filepath)

        if ext == '.mp3':
            AudioFile.from_mp3(filepath).save()

        elif ext == '.aac':
            AudioFile.from_aac(filepath).save()
        else:
            raise TaskSkipped('Not a supported file %s' % filepath)
Esempio n. 3
0
    def process_task(self,track):
        # does it exist already? Using enough fields to disambiguate without
        # having an extra non-standard ID. Should also match tracks produced by the old system, otherwise all of the dictionary would do.
        if AudioFile.objects.filter(
            artist=track['artist'],
            title=track['title'],
            genre=track['genre'],
        ).exists():
            raise TaskSkipped()

        # download MP3 file (tags could be non-existent to great)
        fd,filepath = mkstemp(dir=settings.CAS_DIRECTORY,prefix="soundcloud_mp3_")
        # os-level, not python File. Cannot be GC'd. Plug leak.
        try:
            with fdopen(fd,'wb') as f:
                mp3_res = get(track['mp3_url'],params={'client_id':self.key},stream=True)

                if mp3_res.status_code != 200:
                    raise Exception('Could not download {0}, got HTTP {1}'.format(track['mp3_url'],mp3_res.status_code))

                for chunk in mp3_res.iter_content(chunk_size=8192):
                    f.write(chunk)

            # inspect headers (some can be better than 'meta' can provide)
            audio = MP3(filepath)
            # add ID3 headers if not there (probably isn't) will except if they are
            try: audio.add_tags()
            except: pass

            # download cover art
            cover_res = get(track['cover_art_url'])
            if cover_res.status_code != 200:
                # undo hack
                cover_res = get(track['cover_art_url'].replace('large','original'))

            if cover_res.status_code == 200:
                # else, hopefully there is one embedded
                cover_art = cover_res.content

                audio.tags.add(
                    APIC(
                        encoding=3, # 3 is for utf-8
                        mime='image/jpeg',
                        type=3, # 3 is for the cover image
                        desc=u'Cover',
                        data=cover_res.content
                    )
                )

            audio.tags.add(TIT2(encoding=3, text=track['title']))
            audio.tags.add(TALB(encoding=3, text=track['album']))
            audio.tags.add(TPE1(encoding=3, text=track['artist']))
            audio.tags.add(TPE2(encoding=3, text=track['album_artist']))
            audio.tags.add(TCON(encoding=3, text=track['genre']))
            audio.tags.add(TDRC(encoding=3, text=track['year']))

            # save header to tmp mp3 file
            audio.save()

            # insert into index
            audioFile = AudioFile.from_mp3(filepath)
            AudioFile.origin = track['origin']
            AudioFile.save()

        finally:
            # GC
            unlink(filepath)
Esempio n. 4
0
    def process_task(self, track):
        # does it exist already? Using enough fields to disambiguate without
        # having an extra non-standard ID. Should also match tracks produced by the old system, otherwise all of the dictionary would do.
        if AudioFile.objects.filter(
                artist=track['artist'],
                title=track['title'],
                genre=track['genre'],
        ).exists():
            raise TaskSkipped()

        # download MP3 file (tags could be non-existent to great)
        fd, filepath = mkstemp(dir=settings.CAS_DIRECTORY,
                               prefix="soundcloud_mp3_")
        # os-level, not python File. Cannot be GC'd. Plug leak.
        try:
            with fdopen(fd, 'wb') as f:
                mp3_res = get(track['mp3_url'],
                              params={'client_id': self.key},
                              stream=True)

                if mp3_res.status_code != 200:
                    raise Exception(
                        'Could not download {0}, got HTTP {1}'.format(
                            track['mp3_url'], mp3_res.status_code))

                for chunk in mp3_res.iter_content(chunk_size=8192):
                    f.write(chunk)

            # inspect headers (some can be better than 'meta' can provide)
            audio = MP3(filepath)
            # add ID3 headers if not there (probably isn't) will except if they are
            try:
                audio.add_tags()
            except:
                pass

            # download cover art
            cover_res = get(track['cover_art_url'])
            if cover_res.status_code != 200:
                # undo hack
                cover_res = get(track['cover_art_url'].replace(
                    'large', 'original'))

            if cover_res.status_code == 200:
                # else, hopefully there is one embedded
                cover_art = cover_res.content

                audio.tags.add(
                    APIC(
                        encoding=3,  # 3 is for utf-8
                        mime='image/jpeg',
                        type=3,  # 3 is for the cover image
                        desc=u'Cover',
                        data=cover_res.content))

            audio.tags.add(TIT2(encoding=3, text=track['title']))
            audio.tags.add(TALB(encoding=3, text=track['album']))
            audio.tags.add(TPE1(encoding=3, text=track['artist']))
            audio.tags.add(TPE2(encoding=3, text=track['album_artist']))
            audio.tags.add(TCON(encoding=3, text=track['genre']))
            audio.tags.add(TDRC(encoding=3, text=track['year']))

            # save header to tmp mp3 file
            audio.save()

            # insert into index
            audioFile = AudioFile.from_mp3(filepath)
            AudioFile.origin = track['origin']
            AudioFile.save()

        finally:
            # GC
            unlink(filepath)