Skip to content

Commit

Permalink
Add ability to disable generic URL handling
Browse files Browse the repository at this point in the history
Closes #188. This allows you to handle service-specific URLs (e.g. via
the `youtube`, `wikipedia`, etc. plugins) without handling every URL we
see.
  • Loading branch information
johnmaguire committed Apr 18, 2021
1 parent b036433 commit a3ce425
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
5 changes: 3 additions & 2 deletions plugins/urls/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"timeout": 10,
"timeout": 5,
"read_bytes": 524288,
"lookup_cooloff": 10
"lookup_cooloff": 10,
"handle_generic_urls": true
}
18 changes: 11 additions & 7 deletions plugins/urls/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def get_urls(message):


class URLsPlugin:
TIMEOUT = 10
TIMEOUT = 5
"""Timeout in seconds before bailing on loading page"""

READ_BYTES = 524288
Expand All @@ -66,6 +66,9 @@ def __init__(self, cardinal, config):
self.timeout = config.get('timeout', self.TIMEOUT)
self.read_bytes = config.get('read_bytes', self.READ_BYTES)
self.lookup_cooloff = config.get('lookup_cooloff', self.LOOKUP_COOLOFF)
# Whether to attempt to grab a title if no other plugin handles it
self.generic_handler_enabled = config.get(
'handle_generic_urls', True)

cardinal.event_manager.register('urls.detection', 2)

Expand All @@ -83,7 +86,7 @@ def get_title(self, cardinal, user, channel, msg):
if (url == self.last_url and self.last_url_at and
(datetime.now() - self.last_url_at).seconds <
self.lookup_cooloff):
return None
return

self.last_url = url
self.last_url_at = datetime.now()
Expand All @@ -93,8 +96,10 @@ def get_title(self, cardinal, user, channel, msg):
hooked = yield cardinal.event_manager.fire(
'urls.detection', channel, url)

if hooked:
return None
# Move to the next URL if a plugin has handled it or generic
# handling is disabled
if hooked or not self.generic_handler_enabled:
continue

try:
o = request.build_opener()
Expand All @@ -105,13 +110,13 @@ def get_title(self, cardinal, user, channel, msg):
f = yield deferToThread(o.open, url, timeout=self.timeout)
except Exception:
self.logger.exception("Unable to load URL: %s" % url)
return None
return

# Attempt to find the title
content_type = f.info()['content-type']
if not ('text/html' in content_type or
'text/xhtml' in content_type):
return None
return
content = f.read(self.read_bytes).decode('utf-8')
f.close()

Expand All @@ -126,7 +131,6 @@ def get_title(self, cardinal, user, channel, msg):
title_to_send = title[:200] if len(title) >= 200 else title

cardinal.sendMsg(channel, "URL Found: %s" % title_to_send)
continue

def close(self, cardinal):
cardinal.event_manager.remove('urls.detection')
Expand Down

0 comments on commit a3ce425

Please sign in to comment.