Exemplo n.º 1
0
    def process_media(self, media, item_cache_dir, item_rel_dir):
        """
        copy media to directory & applying metadata
        """
        filename = safe_name(
            FILENAME_FORMAT %
            (media.tracknumber, media.name, media.artist.name, self.format))
        file_path = os.path.join(item_cache_dir, filename)

        # TODO: modularize and refactor to meaningful names
        #cache_file = media.get_cache_file('mp3', 'base')
        from media_asset.models import Format
        requested_format = Format.objects.get_or_create_for_media(media)
        while requested_format.status in [Format.INIT, Format.PROCESSING]:
            log.debug('format not ready yet. sleep for a while')
            time.sleep(1)
            requested_format.refresh_from_db()
        cache_file = requested_format.path

        log.info('processing media: pk %s' % media.pk)
        #log.debug('cache file: %s' % cache_file)

        if not cache_file:
            self.messages.append(
                (media, _('The file seems to be missing. Sorry.')))
            return

        try:
            shutil.copyfile(cache_file, file_path)

        except Exception as e:
            self.messages.append((media, e))
            return

        try:
            self.inject_metadata(file_path, media)

        except Exception as e:
            self.messages.append((media, e))
            return

        self.file_list.append({
            'filename': filename,
            'directory': item_rel_dir,
            'item': media
        })

        if self.dbox:
            self.dbox.upload(file_path, filename)

        return True
Exemplo n.º 2
0
    def process_playlist(self, instance, cache_dir, file_list=None):

        file_list = file_list or []

        log.debug('processing license')
        template = 'exporter/m3u/playlist.m3u'

        #filename 'playlist.m3u'
        filename = '{0}.m3u'.format(safe_name(instance.name))

        with open(os.path.join(cache_dir, filename), "w") as txt:
            str = render_to_string(template, {
                'object': instance,
                'file_list': file_list
            })
            txt.write(str.encode('utf8'))
Exemplo n.º 3
0
    def process_media(self, media, item_cache_dir, item_rel_dir):
        """
        copy media to directory & applying metadata
        """
        filename = safe_name(FILENAME_FORMAT % (media.tracknumber, media.name, media.artist.name, self.format))
        file_path = os.path.join(item_cache_dir, filename)
        cache_file = media.get_cache_file('mp3', 'base')

        log.info('processing media: pk %s' % media.pk)
        #log.debug('cache file: %s' % cache_file)

        if not cache_file:
            self.messages.append((media, _('The file seems to be missing. Sorry.')))
            return

        try:
            shutil.copyfile(cache_file, file_path)

        except Exception, e:
            print e
            self.messages.append((media, e))
            return
Exemplo n.º 4
0
    def process_task(obj):

        from atracker.util import create_event

        process = Process()

        dbox = None
        # dbox = Synchronizer(obj.user)

        log = logging.getLogger('exporter.models.process_task')

        archive_dir = create_archive_dir(obj)
        archive_cache_dir = os.path.join(archive_dir, 'cache/')
        archive_path = os.path.join(archive_dir, 'archive') # .zip appended by 'make_archive'

        # clean cache and recreate
        shutil.rmtree(archive_cache_dir, True)
        os.makedirs(archive_cache_dir)

        log.debug('archive_dir: %s' % (archive_dir))
        log.debug('archive_cache_dir: %s' % (archive_cache_dir))
        log.debug('archive_path: %s' % (archive_path))

        """
        first collect respective items and store everything in a temporary directory
        (the archive_cache_dir)
        """

        try:


            for item in obj.export_items.all():

                log.debug('export ctype: %s | id: %s' % (item.content_type, item.object_id))

                # switching processing for different types

                """
                Releases
                """
                if item.content_type.name.lower() == 'release':

                    t_item = item.content_object

                    """
                    create item specific path
                    < Artist Name >/< Release Name >/...
                    """

                    # relative path, for any target
                    item_rel_dir = os.path.join(safe_name(t_item.get_artist_display()), safe_name(t_item.name))
                    item_cache_dir = os.path.join(archive_cache_dir, item_rel_dir)

                    os.makedirs(item_cache_dir)

                    # holder for playlist entries
                    playlist_items = []

                    # string format for filename
                    filename_format = '%s - %s - %s.%s'

                    for media in t_item.media_release.all():

                        log.debug('export item: %s | id: %s' % ( media.name, media.pk))

                        if obj.fileformat == 'mp3':

                            filename = filename_format % (media.tracknumber, media.name, media.artist.name, 'mp3')
                            filename = safe_name(filename)
                            #filepath = os.path.join(archive_cache_dir, filename)
                            filepath = os.path.join(item_cache_dir, filename)

                            shutil.copyfile(media.get_cache_file('mp3', 'base'), filepath)
                            try:
                                process.inject_metadata(filepath, media)
                            except Exception, e:
                                pass

                        # just dummy - not possible...
                        if obj.fileformat == 'flac':

                            filename = filename_format % (media.tracknumber, media.name, media.artist.name, 'mp3')
                            filename = safe_name(filename)
                            filepath = os.path.join(item_cache_dir, filename)

                            shutil.copyfile(media.get_cache_file('mp3', 'base'), filepath)
                            try:
                                process.inject_metadata(filepath, media)
                            except Exception, e:
                                pass

                        playlist_items.append({'filename': filename, 'item': media})

                        if dbox:
                            dbox.upload(filepath, os.path.join('Releases', item_rel_dir, filename))

                        create_event(obj.user, media, None, 'download')

                    if t_item.main_image:
                        try:
                            shutil.copyfile(t_item.main_image.path, os.path.join(item_cache_dir, 'cover.jpg'))
                        except Exception, e:
                            print e
                            pass


                            #archive_file.write(t_item.main_image.path, 'cover.jpg')

                    """
                    Add additional assets
                    REATME.TXT LICENSE.TXT etc
                    """
                    with open(os.path.join(item_cache_dir, 'README.TXT'), "w") as txt:
                        str = render_to_string('exporter/txt/README.TXT', {'object': t_item})
                        txt.write(str)

                    """
Exemplo n.º 5
0
                        str = render_to_string('exporter/txt/LICENSE.TXT', { 'objects': playlist_items })
                        txt.write(str)
                    """

                """
                Media
                """
                if item.content_type.name.lower() == 'track':

                    t_item = item.content_object

                    """
                    create item specific path
                    < Playlist Name >/...
                    """
                    item_cache_dir = os.path.join(archive_cache_dir, safe_name(t_item.name))
                    os.makedirs(item_cache_dir)

                    # holder for playlist entries
                    playlist_items = []

                    # string format for filename
                    filename_format = '%s - %s.%s'

                    for playlist_item in [t_item]:

                        media = playlist_item

                        log.debug('export item: %s | id: %s' % ( media.name, media.pk))

                        if obj.fileformat == 'mp3':
Exemplo n.º 6
0
    def process_item(self, item):

        log.info('export ctype: %s | id: %s' % (item.content_type, item.object_id))

        media_set = None
        content_object = item.content_object
        image = None

        ct = item.content_type.name.lower()


        if ct == 'release':
            media_set = content_object.media_release.all()
            image = content_object.main_image
            if content_object.get_artist_display:
                item_rel_dir = os.path.join(
                    safe_name(content_object.get_artist_display()),
                    safe_name(content_object.name)
                )
            else:
                item_rel_dir = safe_name(content_object.get_artist_display())


        if ct == 'track':
            media_set = [content_object]
            if content_object.artist and content_object.release:
                item_rel_dir = os.path.join(
                    safe_name(content_object.artist.name),
                    safe_name(content_object.release.name)
                )
            elif content_object.artist:
                item_rel_dir = safe_name(content_object.artist.name)
            else:
                item_rel_dir = safe_name(content_object.name)


        if ct == 'playlist':
            media_set = []
            image = content_object.main_image
            for m in content_object.get_items():
                media_set.append(m.content_object)

            if content_object.user and content_object.user.get_full_name():
                item_rel_dir = '%s (%s)' % (
                    safe_name(content_object.name),
                    safe_name(content_object.user.get_full_name())
                )
            else:
                item_rel_dir = safe_name(content_object.name)


        item_cache_dir = os.path.join(
            self.archive_cache_dir,
            safe_name(item_rel_dir)
        )
        if not os.path.exists(item_cache_dir):
            os.makedirs(item_cache_dir)

        log.debug('%s tracks to export' % len(media_set))

        # process tracks
        for media in media_set:
            if self.process_media(media, item_cache_dir, item_rel_dir) and CREATE_EVENTS:
                create_event(self.instance.user, media, None, 'download')

        # process additional resources
        if image and os.path.isfile(image.path):
            try:
                shutil.copyfile(image.path, os.path.join(item_cache_dir, IMAGE_FILENAME))
            except Exception, e:
                print e
Exemplo n.º 7
0
    def process_item(self, item):

        log.info('export ctype: %s | id: %s' %
                 (item.content_type, item.object_id))

        media_set = None
        content_object = item.content_object
        image = None

        ct = item.content_type.name.lower()

        if ct == 'release':
            media_set = content_object.media_release.all()
            image = content_object.main_image
            if content_object.get_artist_display:
                item_rel_dir = os.path.join(
                    safe_name(content_object.get_artist_display()),
                    safe_name(content_object.name))
            else:
                item_rel_dir = safe_name(content_object.get_artist_display())

        if ct == 'track':
            media_set = [content_object]
            if content_object.artist and content_object.release:
                item_rel_dir = os.path.join(
                    safe_name(content_object.artist.name),
                    safe_name(content_object.release.name))
            elif content_object.artist:
                item_rel_dir = safe_name(content_object.artist.name)
            else:
                item_rel_dir = safe_name(content_object.name)

        if ct == 'playlist':
            media_set = []
            image = content_object.main_image
            for m in content_object.get_items():
                media_set.append(m.content_object)

            if content_object.user and content_object.user.get_full_name():
                item_rel_dir = '%s (%s)' % (
                    safe_name(content_object.name),
                    safe_name(content_object.user.get_full_name()))
            else:
                item_rel_dir = safe_name(content_object.name)

        item_cache_dir = os.path.join(self.archive_cache_dir,
                                      safe_name(item_rel_dir))
        if not os.path.exists(item_cache_dir):
            os.makedirs(item_cache_dir)

        log.debug('%s tracks to export' % len(media_set))

        # process tracks
        for media in media_set:
            if self.process_media(media, item_cache_dir,
                                  item_rel_dir) and CREATE_EVENTS:
                create_event(self.instance.user, media, None, 'download')

        # process additional resources
        if image and os.path.isfile(image.path):
            try:
                shutil.copyfile(image.path,
                                os.path.join(item_cache_dir, IMAGE_FILENAME))
            except Exception as e:
                log.warning('unable to copy image: {}'.format(image.path))

        if INCLUDE_README:
            self.process_readme(instance=content_object,
                                cache_dir=item_cache_dir)

        if INCLUDE_HTML_README:
            try:
                self.process_html_readme(instance=content_object,
                                         cache_dir=item_cache_dir)
            except:
                pass

        if INCLUDE_LICENSE:
            self.process_license(instance=content_object,
                                 cache_dir=item_cache_dir,
                                 file_list=self.file_list)

        if ct == 'playlist' and INCLUDE_PLAYLIST:
            self.process_playlist(instance=content_object,
                                  cache_dir=item_cache_dir,
                                  file_list=self.file_list)

        # include mixdown file in download
        # TODO: this is an experimental implementation!
        if ct == 'playlist' and self.user and self.user.profile.enable_alpha_features:
            if content_object.mixdown_file and os.path.exists(
                    content_object.mixdown_file.path):
                try:
                    shutil.copyfile(
                        content_object.mixdown_file.path,
                        os.path.join(
                            item_cache_dir,
                            content_object.mixdown_file.name.split('/')[-1]))
                    log.debug('include mixdown file for playlist')
                except Exception as e:
                    log.warning('unable to copy mixdown file: {}'.format(
                        content_object.mixdown_file.path))
            else:
                log.debug('mixdown file not available')

            self.process_playlist(instance=content_object,
                                  cache_dir=item_cache_dir,
                                  file_list=self.file_list)

        return None, None