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 {}
评论列表
文章目录