2020-04-11 13:24:06 +02:00
|
|
|
import json
|
2020-06-11 00:54:34 +02:00
|
|
|
from typing import Any, Dict, Optional
|
|
|
|
|
|
|
|
from pyoembed import PyOembedException, oEmbed
|
|
|
|
|
2016-10-27 12:06:44 +02:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def get_oembed_data(url: str,
|
2017-11-05 11:15:10 +01:00
|
|
|
maxwidth: Optional[int]=640,
|
2018-06-16 23:00:17 +02:00
|
|
|
maxheight: Optional[int]=480) -> Optional[Dict[str, Any]]:
|
2016-10-27 12:06:44 +02:00
|
|
|
try:
|
|
|
|
data = oEmbed(url, maxwidth=maxwidth, maxheight=maxheight)
|
2020-04-11 13:24:06 +02:00
|
|
|
except (PyOembedException, json.decoder.JSONDecodeError):
|
2016-10-27 12:06:44 +02:00
|
|
|
return None
|
|
|
|
|
2019-06-01 13:05:30 +02:00
|
|
|
oembed_resource_type = data.get('type', '')
|
2019-05-26 06:27:01 +02:00
|
|
|
image = data.get('url', data.get('image'))
|
2019-05-02 18:58:39 +02:00
|
|
|
thumbnail = data.get('thumbnail_url')
|
|
|
|
html = data.pop('html', '')
|
2019-06-01 13:05:30 +02:00
|
|
|
if oembed_resource_type == 'photo' and image:
|
2019-12-12 02:10:50 +01:00
|
|
|
return dict(
|
|
|
|
oembed=True,
|
|
|
|
image=image,
|
|
|
|
type=oembed_resource_type,
|
|
|
|
title=data.get('title'),
|
|
|
|
description=data.get('description'),
|
|
|
|
)
|
2019-05-02 18:58:39 +02:00
|
|
|
|
2019-12-12 02:10:50 +01:00
|
|
|
if oembed_resource_type == 'video' and html and thumbnail:
|
|
|
|
return dict(
|
|
|
|
oembed=True,
|
|
|
|
image=thumbnail,
|
|
|
|
type=oembed_resource_type,
|
|
|
|
html=strip_cdata(html),
|
|
|
|
title=data.get('title'),
|
|
|
|
description=data.get('description'),
|
|
|
|
)
|
2019-05-26 06:27:01 +02:00
|
|
|
|
2019-12-12 02:10:50 +01:00
|
|
|
# Otherwise, start with just the embed type.
|
|
|
|
return dict(
|
|
|
|
type=oembed_resource_type,
|
|
|
|
title=data.get('title'),
|
|
|
|
description=data.get('description'),
|
|
|
|
)
|
2019-05-02 18:58:39 +02:00
|
|
|
|
2019-12-12 09:39:41 +01:00
|
|
|
def strip_cdata(html: str) -> str:
|
|
|
|
# Work around a bug in SoundCloud's XML generation:
|
|
|
|
# <html><![CDATA[<iframe ...></iframe>]]></html>
|
2019-05-02 18:58:39 +02:00
|
|
|
if html.startswith('<![CDATA[') and html.endswith(']]>'):
|
|
|
|
html = html[9:-3]
|
2019-12-12 09:39:41 +01:00
|
|
|
return html
|