Add a setting to automatically shorten URLs when their title is retrieved

This commit is contained in:
jesopo 2019-04-28 00:35:41 +01:00
parent 98deba18ed
commit 347253249e

View file

@ -4,6 +4,9 @@ from src import EventManager, ModuleManager, utils
@utils.export("channelset", {"setting": "auto-title", @utils.export("channelset", {"setting": "auto-title",
"help": "Disable/Enable automatically getting info titles from URLs", "help": "Disable/Enable automatically getting info titles from URLs",
"validate": utils.bool_or_none}) "validate": utils.bool_or_none})
@utils.export("channelset", {"setting": "title-shorten",
"help": "Enable/disable shortening URLs when getting their title",
"validate": utils.bool_or_none})
@utils.export("channelset", {"setting": "auto-title-first", @utils.export("channelset", {"setting": "auto-title-first",
"help": ("Enable/disable showing who first posted a URL that was " "help": ("Enable/disable showing who first posted a URL that was "
"auto-titled"), "auto-titled"),
@ -13,7 +16,7 @@ class Module(ModuleManager.BaseModule):
return "sha256:%s" % hashlib.sha256(url.lower().encode("utf8") return "sha256:%s" % hashlib.sha256(url.lower().encode("utf8")
).hexdigest() ).hexdigest()
def _get_title(self, url): def _get_title(self, channel, url):
if not urllib.parse.urlparse(url).scheme: if not urllib.parse.urlparse(url).scheme:
url = "http://%s" % url url = "http://%s" % url
@ -30,8 +33,14 @@ class Module(ModuleManager.BaseModule):
self.log.error("failed to get URL title", [], exc_info=True) self.log.error("failed to get URL title", [], exc_info=True)
return None return None
if page.data.title: if page.data.title:
return page.data.title.text.replace("\n", " ").replace( title = page.data.title.text.replace("\n", " ").replace(
"\r", "").replace(" ", " ").strip() "\r", "").replace(" ", " ").strip()
if channel.get_setting("title-shorten", False):
short_url = self.exports.get_one("shortlink", lambda x: x
)(url)
return "%s - %s" % (title, short_url)
return title
else: else:
return None return None
@ -46,7 +55,7 @@ class Module(ModuleManager.BaseModule):
return return
url = match.group(0) url = match.group(0)
title = self._get_title(match.group(0)) title = self._get_title(event["channel"], match.group(0))
if title: if title:
message = title message = title
@ -87,7 +96,7 @@ class Module(ModuleManager.BaseModule):
if not url: if not url:
raise utils.EventError("No URL provided/found.") raise utils.EventError("No URL provided/found.")
title = self._get_title(url) title = self._get_title(event["target"], url)
if title: if title:
event["stdout"].write(title) event["stdout"].write(title)