diff options
author | Patrick Cloke <clokep@users.noreply.github.com> | 2021-10-08 14:14:42 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-08 14:14:42 -0400 |
commit | 1b112840d2c6dafa131eba4f0285409bb7345661 (patch) | |
tree | cead0015dbaceb5a188b3b95e8a6cfe2d82960dc /synapse/rest/media/v1/oembed.py | |
parent | Revert accidental push to develop. (diff) | |
download | synapse-1b112840d2c6dafa131eba4f0285409bb7345661.tar.xz |
Autodiscover oEmbed endpoint from returned HTML (#10822)
Searches the returned HTML for an oEmbed endpoint using the autodiscovery mechanism (`<link rel=...>`), and will request it to generate the preview.
Diffstat (limited to 'synapse/rest/media/v1/oembed.py')
-rw-r--r-- | synapse/rest/media/v1/oembed.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/synapse/rest/media/v1/oembed.py b/synapse/rest/media/v1/oembed.py index e04671fb95..6d7e1f9064 100644 --- a/synapse/rest/media/v1/oembed.py +++ b/synapse/rest/media/v1/oembed.py @@ -96,6 +96,32 @@ class OEmbedProvider: # No match. return None + def autodiscover_from_html(self, tree: "etree.Element") -> Optional[str]: + """ + Search an HTML document for oEmbed autodiscovery information. + + Args: + tree: The parsed HTML body. + + Returns: + The URL to use for oEmbed information, or None if no URL was found. + """ + # Search for link elements with the proper rel and type attributes. + for tag in tree.xpath( + "//link[@rel='alternate'][@type='application/json+oembed']" + ): + if "href" in tag.attrib: + return tag.attrib["href"] + + # Some providers (e.g. Flickr) use alternative instead of alternate. + for tag in tree.xpath( + "//link[@rel='alternative'][@type='application/json+oembed']" + ): + if "href" in tag.attrib: + return tag.attrib["href"] + + return None + def parse_oembed_response(self, url: str, raw_body: bytes) -> OEmbedResult: """ Parse the oEmbed response into an Open Graph response. |