external.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:feincms3 作者: matthiask 项目源码 文件源码
def oembed_json(url, cache_failures=True):
    """oembed_json(url, *, cache_failures=True)
    Asks Noembed_ for the embedding HTML code for arbitrary URLs. Sites
    supported include Youtube, Vimeo, Twitter and many others.

    Successful embeds are always cached for 30 days.

    Failures are cached if ``cache_failures`` is ``True`` (the default). The
    durations are as follows:

    - Connection errors are cached 60 seconds with the hope that the connection
      failure is only transient.
    - HTTP errors codes and responses in an unexpected format (no JSON) are
      cached for 24 hours.

    The return value is always a dictionary, but it may be empty.
    """
    # Thundering herd problem etc...
    key = 'oembed-url-%s-data' % md5(url.encode('utf-8')).hexdigest()
    data = cache.get(key)
    if data is not None:
        return data

    try:
        data = requests.get(
            'https://noembed.com/embed',
            params={
                'url': url,
                'nowrap': 'on',
                'maxwidth': 1200,
                'maxheight': 800,
            },
            timeout=2,
        ).json()
    except (requests.ConnectionError, requests.ReadTimeout):
        # Connection failed? Hopefully temporary, try again soon.
        timeout = 60
    except (ValueError, requests.HTTPError):
        # Oof... HTTP error code, or no JSON? Try again tomorrow,
        # and we should really log this.
        timeout = 86400
    else:
        # Perfect, cache for 30 days
        cache.set(key, data, timeout=30 * 86400)
        return data

    if cache_failures:
        cache.set(key, {}, timeout=timeout)
    return {}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号