コード例 #1
0
ファイル: embeds.py プロジェクト: dvska/wagtail
def oembed(url, max_width=None):
    # Find provider
    provider = get_oembed_provider(url)
    if provider is None:
        raise EmbedNotFoundException

    # Work out params
    params = {'url': url, 'format': 'json', }
    if max_width:
        params['maxwidth'] = max_width

    # Perform request
    request = urllib2.Request(provider + '?' + urllib.urlencode(params))
    request.add_header('User-agent', 'Mozilla/5.0')
    try:
        r = urllib2.urlopen(request)
    except urllib2.URLError:
        raise EmbedNotFoundException
    oembed = json.loads(r.read())

    # Convert photos into HTML
    if oembed['type'] == 'photo':
        html = '<img src="%s" />' % (oembed['url'], )
    else:
        html = oembed.get('html')

    # Return embed as a dict
    return {
        'title': oembed['title'],
        'type': oembed['type'],
        'thumbnail_url': oembed.get('thumbnail_url'),
        'width': oembed.get('width'),
        'height': oembed.get('height'),
        'html': html,
    }
コード例 #2
0
def oembed(url, max_width=None):
    # Find provider
    provider = get_oembed_provider(url)
    if provider is None:
        raise EmbedNotFoundException

    # Work out params
    params = {
        'url': url,
        'format': 'json',
    }
    if max_width:
        params['maxwidth'] = max_width

    # Perform request
    request = Request(provider + '?' + urlencode(params))
    request.add_header('User-agent', 'Mozilla/5.0')
    try:
        r = urllib_request.urlopen(request)
    except URLError:
        raise EmbedNotFoundException
    oembed = json.loads(r.read().decode('utf-8'))

    # Convert photos into HTML
    if oembed['type'] == 'photo':
        html = '<img src="%s" />' % (oembed['url'], )
    else:
        html = oembed.get('html')

    # Return embed as a dict
    return {
        'title': oembed['title'] if 'title' in oembed else '',
        'author_name':
        oembed['author_name'] if 'author_name' in oembed else '',
        'provider_name':
        oembed['provider_name'] if 'provider_name' in oembed else '',
        'type': oembed['type'],
        'thumbnail_url': oembed.get('thumbnail_url'),
        'width': oembed.get('width'),
        'height': oembed.get('height'),
        'html': html,
    }
コード例 #3
0
ファイル: oembed.py プロジェクト: thrawny/wagtail
def oembed(url, max_width=None):
    # Find provider
    provider = get_oembed_provider(url)
    if provider is None:
        raise EmbedNotFoundException

    # Work out params
    params = {"url": url, "format": "json"}
    if max_width:
        params["maxwidth"] = max_width

    # Perform request
    request = Request(provider + "?" + urlencode(params))
    request.add_header("User-agent", "Mozilla/5.0")
    try:
        r = urllib_request.urlopen(request)
    except URLError:
        raise EmbedNotFoundException
    oembed = json.loads(r.read().decode("utf-8"))

    # Convert photos into HTML
    if oembed["type"] == "photo":
        html = '<img src="%s" />' % (oembed["url"],)
    else:
        html = oembed.get("html")

    # Return embed as a dict
    return {
        "title": oembed["title"] if "title" in oembed else "",
        "author_name": oembed["author_name"] if "author_name" in oembed else "",
        "provider_name": oembed["provider_name"] if "provider_name" in oembed else "",
        "type": oembed["type"],
        "thumbnail_url": oembed.get("thumbnail_url"),
        "width": oembed.get("width"),
        "height": oembed.get("height"),
        "html": html,
    }