Esempio n. 1
0
    def _to_python(self, value, state=None):
        """Convert the given plain text or HTML into valid XHTML.

        Invalid elements are stripped or converted.
        Essentially a wapper for :func:`~mediacore.helpers.clean_xhtml`.
        """
        return clean_xhtml(value)
Esempio n. 2
0
    def _to_python(self, value, state=None):
        """Convert the given plain text or HTML into valid XHTML.

        Invalid elements are stripped or converted.
        Essentially a wapper for :func:`~mediacore.helpers.clean_xhtml`.
        """
        return clean_xhtml(value)
Esempio n. 3
0
 def _import_video(self, entry):
     player_url = self._player_url_from_entry(entry)
     if not player_url:
         log.debug('Video Feed Error: No player URL? %s' % entry)
         return None
     if self._has_media_file_for(player_url):
         return None
     
     media = fetch_row(Media, u'new')
     media.author = Author(self.user.display_name, self.user.email_address)
     media.reviewed = True
     media.title = unicode(entry.media.title.text, "utf-8")
     if entry.media.description.text:
         encoded_description = unicode(entry.media.description.text, "utf-8")
         media.description = clean_xhtml(encoded_description)
     media.slug = get_available_slug(Media, media.title, media)
     
     if self.tags:
         media.set_tags(unicode(self.tags))
     if self.categories:
         media.set_categories(self.categories)
     try:
         media_file = add_new_media_file(media, url=player_url)
     except StorageError, e:
         log.debug('Video Feed Error: Error storing video: %s at %s' \
             % (e.message, player_url))
         return None
Esempio n. 4
0
        def get_videos_from_feed(feed):
            for entry in feed.entry:
                # Occasionally, there are issues with a video in a feed
                # not being available (region restrictions, etc)
                # If this happens, just move along.
                if not entry.media.player:
                    log.debug('Video Feed Error: No player URL? %s' % entry)
                    continue
                video_url = unicode(entry.media.player.url, "utf-8")
                if video_already_has_media_file(video_url):
                    continue
                categories = kwargs.get('youtube.categories', None)
                tags = kwargs.get('youtube.tags', None)
                media = fetch_row(Media, u'new')
                user = request.environ['repoze.who.identity']['user']
                media.author = Author(user.display_name, user.email_address)
                media.reviewed = True
                media.title = unicode(entry.media.title.text, "utf-8")
                if entry.media.description.text:
                    encoded_description = unicode(entry.media.description.text,
                                                "utf-8")
                    media.description = clean_xhtml(encoded_description)
                media.slug = get_available_slug(Media, media.title, media)

                if tags:
                    media.set_tags(unicode(tags))
                if categories:
                    if not isinstance(categories, list):
                        categories = [categories]
                    media.set_categories(categories)
                try:
                    media_file = add_new_media_file(media,
                        url=video_url)
                except StorageError, e:
                    log.debug('Video Feed Error: Error storing video: %s at %s' \
                        % e.message, video_url)
                    continue
                if not has_thumbs(media):
                    create_default_thumbs_for(media)
                media.title = media_file.display_name
                media.update_status()
                if auto_publish:
                    media.reviewed = 1
                    media.encoded = 1
                    media.publishable = 1
                    media.created_on = datetime.now()
                    media.modified_on = datetime.now()
                    media.publish_on = datetime.now()
                DBSession.add(media)
                DBSession.flush()
Esempio n. 5
0
def add_new_media_file(media, file=None, url=None):
    """Create a MediaFile instance from the given file or URL.

    This function MAY modify the given media object.

    :type media: :class:`~mediacore.model.media.Media` instance
    :param media: The media object that this file or URL will belong to.
    :type file: :class:`cgi.FieldStorage` or None
    :param file: A freshly uploaded file object.
    :type url: unicode or None
    :param url: A remote URL string.
    :rtype: :class:`~mediacore.model.media.MediaFile`
    :returns: A newly created media file instance.
    :raises StorageError: If the input file or URL cannot be
        stored with any of the registered storage engines.

    """
    engines = DBSession.query(StorageEngine)\
        .filter(StorageEngine.enabled == True)\
        .all()
    sorted_engines = list(sort_engines(engines))

    for engine in sorted_engines:
        try:
            meta = engine.parse(file=file, url=url)
            log.debug('Engine %r returned meta %r', engine, meta)
            break
        except UnsuitableEngineError:
            log.debug('Engine %r unsuitable for %r/%r', engine, file, url)
            continue
    else:
        raise StorageError(_('Unusable file or URL provided.'), None, None)

    mf = MediaFile()
    mf.storage = engine
    mf.media = media

    mf.type = meta['type']
    mf.display_name = meta.get('display_name', default_display_name(file, url))
    mf.unique_id = meta.get('unique_id', None)

    mf.container = meta.get('container', None)
    mf.size = meta.get('size', None)
    mf.bitrate = meta.get('bitrate', None)
    mf.width = meta.get('width', None)
    mf.height = meta.get('height', None)

    media.files.append(mf)
    DBSession.flush()

    unique_id = engine.store(media_file=mf, file=file, url=url, meta=meta)

    if unique_id:
        mf.unique_id = unique_id
    elif not mf.unique_id:
        raise StorageError('Engine %r returned no unique ID.', engine)

    if not media.duration and meta.get('duration', 0):
        media.duration = meta['duration']
    if not media.description and meta.get('description'):
        media.description = clean_xhtml(meta['description'])
    if not media.title:
        media.title = meta.get('title', None) or mf.display_name
    if media.type is None:
        media.type = mf.type

    if ('thumbnail_url' in meta or 'thumbnail_file' in meta) \
    and (not has_thumbs(media) or has_default_thumbs(media)):
        thumb_file = meta.get('thumbnail_file', None)

        if thumb_file is not None:
            thumb_filename = thumb_file.filename
        else:
            thumb_url = meta['thumbnail_url']
            thumb_filename = os.path.basename(thumb_url)

            # Download the image to a buffer and wrap it as a file-like object
            try:
                temp_img = urlopen(thumb_url)
                thumb_file = StringIO(temp_img.read())
                temp_img.close()
            except URLError, e:
                log.exception(e)

        if thumb_file is not None:
            create_thumbs_for(media, thumb_file, thumb_filename)
            thumb_file.close()
Esempio n. 6
0
def add_new_media_file(media, file=None, url=None):
    """Create a MediaFile instance from the given file or URL.

    This function MAY modify the given media object.

    :type media: :class:`~mediacore.model.media.Media` instance
    :param media: The media object that this file or URL will belong to.
    :type file: :class:`cgi.FieldStorage` or None
    :param file: A freshly uploaded file object.
    :type url: unicode or None
    :param url: A remote URL string.
    :rtype: :class:`~mediacore.model.media.MediaFile`
    :returns: A newly created media file instance.
    :raises StorageError: If the input file or URL cannot be
        stored with any of the registered storage engines.

    """
    engines = DBSession.query(StorageEngine)\
        .filter(StorageEngine.enabled == True)\
        .all()
    sorted_engines = list(sort_engines(engines))

    for engine in sorted_engines:
        try:
            meta = engine.parse(file=file, url=url)
            log.debug('Engine %r returned meta %r', engine, meta)
            break
        except UnsuitableEngineError:
            log.debug('Engine %r unsuitable for %r/%r', engine, file, url)
            continue
    else:
        raise StorageError(_('Unusable file or URL provided.'), None, None)

    mf = MediaFile()
    mf.storage = engine
    mf.media = media

    mf.type = meta['type']
    mf.display_name = meta.get('display_name', default_display_name(file, url))
    mf.unique_id = meta.get('unique_id', None)

    mf.container = meta.get('container', None)
    mf.size = meta.get('size', None)
    mf.bitrate = meta.get('bitrate', None)
    mf.width = meta.get('width', None)
    mf.height = meta.get('height', None)

    media.files.append(mf)
    DBSession.flush()

    unique_id = engine.store(media_file=mf, file=file, url=url, meta=meta)

    if unique_id:
        mf.unique_id = unique_id
    elif not mf.unique_id:
        raise StorageError('Engine %r returned no unique ID.', engine)

    if not media.duration and meta.get('duration', 0):
        media.duration = meta['duration']
    if not media.description and meta.get('description'):
        media.description = clean_xhtml(meta['description'])
    if not media.title:
        media.title = meta.get('title', None) or mf.display_name
    if media.type is None:
        media.type = mf.type

    if ('thumbnail_url' in meta or 'thumbnail_file' in meta) \
    and (not has_thumbs(media) or has_default_thumbs(media)):
        thumb_file = meta.get('thumbnail_file', None)

        if thumb_file is not None:
            thumb_filename = thumb_file.filename
        else:
            thumb_url = meta['thumbnail_url']
            thumb_filename = os.path.basename(thumb_url)

            # Download the image to a buffer and wrap it as a file-like object
            try:
                temp_img = urlopen(thumb_url)
                thumb_file = StringIO(temp_img.read())
                temp_img.close()
            except URLError, e:
                log.exception(e)

        if thumb_file is not None:
            create_thumbs_for(media, thumb_file, thumb_filename)
            thumb_file.close()