mirror of
https://github.com/progval/Limnoria.git
synced 2025-04-25 20:41:18 -05:00
add config options useOembedRegistry and useOembedDiscovery
This commit is contained in:
parent
eadac11ab6
commit
427845a358
@ -95,4 +95,12 @@ conf.registerGlobalValue(Web.fetch, 'timeout',
|
||||
seconds the bot will wait for the site to respond, when using the 'fetch'
|
||||
command in this plugin. If 0, will use socket.defaulttimeout"""))
|
||||
|
||||
conf.registerGlobalValue(Web, 'useOembedRegistry',
|
||||
registry.Boolean(False, _("""Determines whether the bot will use the
|
||||
oembed.com providers registry.""")))
|
||||
|
||||
conf.registerGlobalValue(Web, 'useOembedDiscovery',
|
||||
registry.Boolean(False, _("""Determines whether the bot will use HTML
|
||||
discovery to find oEmbed endpoints.""")))
|
||||
|
||||
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|
||||
|
@ -287,29 +287,31 @@ class Web(callbacks.PluginRegexp):
|
||||
First tries the providers registry if enabled, then falls back to
|
||||
HTML discovery if needed and enabled.
|
||||
"""
|
||||
providers = self._loadOEmbedProviders()
|
||||
for provider in providers:
|
||||
for pattern in provider.get('endpoints', []):
|
||||
schemes = pattern.get('schemes', [])
|
||||
endpoint = pattern.get('url', '')
|
||||
for scheme in schemes:
|
||||
regex = re.escape(scheme).replace(r'\*', '.*')
|
||||
if re.match(regex, url):
|
||||
return endpoint
|
||||
try:
|
||||
timeout = self.registryValue('timeout')
|
||||
response = utils.web.getUrl(url, timeout=timeout)
|
||||
text = response.decode('utf8', errors='replace')
|
||||
match = re.search(
|
||||
r'<link[^>]+?type="application/json\+oembed"[^>]+?href="([^"]+)"',
|
||||
text,
|
||||
re.IGNORECASE)
|
||||
if match:
|
||||
endpoint = match.group(1)
|
||||
endpoint = endpoint.split('?')[0]
|
||||
return endpoint
|
||||
except Exception as e:
|
||||
self.log.debug(f"Failed to discover oEmbed endpoint in HTML: {e}")
|
||||
if self.registryValue('useOembedRegistry'):
|
||||
providers = self._loadOEmbedProviders()
|
||||
for provider in providers:
|
||||
for pattern in provider.get('endpoints', []):
|
||||
schemes = pattern.get('schemes', [])
|
||||
endpoint = pattern.get('url', '')
|
||||
for scheme in schemes:
|
||||
regex = re.escape(scheme).replace(r'\*', '.*')
|
||||
if re.match(regex, url):
|
||||
return endpoint
|
||||
if self.registryValue('useOembedDiscovery'):
|
||||
try:
|
||||
timeout = self.registryValue('timeout')
|
||||
response = utils.web.getUrl(url, timeout=timeout)
|
||||
text = response.decode('utf8', errors='replace')
|
||||
match = re.search(
|
||||
r'<link[^>]+?type="application/json\+oembed"[^>]+?href="([^"]+)"',
|
||||
text,
|
||||
re.IGNORECASE)
|
||||
if match:
|
||||
endpoint = match.group(1)
|
||||
endpoint = endpoint.split('?')[0]
|
||||
return endpoint
|
||||
except Exception as e:
|
||||
self.log.debug(f"Failed to discover oEmbed endpoint in HTML: {e}")
|
||||
return None
|
||||
|
||||
def getOEmbedTitle(self, url):
|
||||
|
Loading…
x
Reference in New Issue
Block a user