Add ability to track (and show) who first send a specific link that was

auto-titled
This commit is contained in:
jesopo 2019-04-24 14:48:15 +01:00
parent e095c56f77
commit 71eb640fbd

View file

@ -1,4 +1,4 @@
import re import hashlib, re
from src import EventManager, ModuleManager, utils from src import EventManager, ModuleManager, utils
REGEX_URL = re.compile("https?://\S+", re.I) REGEX_URL = re.compile("https?://\S+", re.I)
@ -6,7 +6,15 @@ REGEX_URL = re.compile("https?://\S+", re.I)
@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": "auto-title-first",
"help": ("Enable/disable showing who first posted a URL that was "
"auto-titled"),
"validate": utils.bool_or_none})
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
def _url_hash(self, url):
return "sha256:%s" % hashlib.sha256(url.lower().encode("utf8")
).hexdigest()
def _get_title(self, url): def _get_title(self, url):
try: try:
page = utils.http.request(url, soup=True) page = utils.http.request(url, soup=True)
@ -26,10 +34,29 @@ class Module(ModuleManager.BaseModule):
def channel_message(self, event): def channel_message(self, event):
match = re.search(REGEX_URL, event["message"]) match = re.search(REGEX_URL, event["message"])
if match and event["channel"].get_setting("auto-title", False): if match and event["channel"].get_setting("auto-title", False):
url = match.group(0)
title = self._get_title(match.group(0)) title = self._get_title(match.group(0))
if title: if title:
message = title
if event["channel"].get_setting("auto-title-first", False):
setting = "url-last-%s" % self._url_hash(url)
first_details = event["channel"].get_setting(setting, None)
if first_details:
first_nickname, first_timestamp = first_details
timestamp_parsed = utils.iso8601_parse(first_timestamp)
timestamp_human = utils.datetime_human(timestamp_parsed)
message = "%s (first posted by %s at %s)" % (title,
first_nickname, timestamp_human)
else:
event["channel"].set_setting(setting,
[event["user"].nickname,
utils.iso8601_format_now()])
self.events.on("send.stdout").call(target=event["channel"], self.events.on("send.stdout").call(target=event["channel"],
message=title, module_name="Title", message=message, module_name="Title",
server=event["server"]) server=event["server"])
@utils.hook("received.command.t", alias_of="title") @utils.hook("received.command.t", alias_of="title")