Example #1
0
def media_file_from_url(url):
    """Create and return a MediaFile object representing a given URL.
    Also returns the URL to a suitable thumbnail image (or None) and the
    duration of the media file in seconds (or None).

    Does not add the created MediaFile to the database.
    """
    thumb_url = None
    duration = None
    title = None
    media_file = MediaFile()
    # Parse the URL checking for known embeddables like YouTube
    embed = parse_embed_url(url)
    if embed:
        media_file.type = embed['type']
        media_file.container = embed['container']
        media_file.embed = embed['id']
        title = embed['title']
        if title:
            media_file.display_name = title
        else:
            media_file.display_name = '%s ID: %s' % \
                (embed['container'].capitalize(), media_file.embed)
        thumb_url = embed['thumb_url']
        duration = embed['duration']
    else:
        # Check for types we can play ourselves
        name, ext, container = base_ext_container_from_uri(url)
        media_file.type = guess_media_type(ext)
        media_file.container = container
        media_file.url = url
        media_file.display_name = os.path.basename(url)

    return media_file, thumb_url, duration, title
Example #2
0
    def _to_python(self, value, state):
        if value == '':
            return value

        embed = parse_embed_url(value)
        if embed:
            return value

        ext = os.path.splitext(value)[1].lower()[1:]
        container = guess_container_format(ext)
        if container in accepted_extensions():
            return value

        raise formencode.Invalid(
            _("This isn't a valid YouTube, Google Video, Vimeo or direct link."),
            value, state
        )