2019-04-25 14:58:58 +00:00
|
|
|
import hashlib, re, urllib.parse
|
2019-02-09 10:57:05 +00:00
|
|
|
from src import EventManager, ModuleManager, utils
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2019-02-09 10:35:37 +00:00
|
|
|
@utils.export("channelset", {"setting": "auto-title",
|
|
|
|
"help": "Disable/Enable automatically getting info titles from URLs",
|
|
|
|
"validate": utils.bool_or_none})
|
2019-04-27 23:35:41 +00:00
|
|
|
@utils.export("channelset", {"setting": "title-shorten",
|
|
|
|
"help": "Enable/disable shortening URLs when getting their title",
|
|
|
|
"validate": utils.bool_or_none})
|
2019-04-24 13:48:15 +00:00
|
|
|
@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})
|
2018-09-26 17:27:17 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2019-04-24 13:48:15 +00:00
|
|
|
def _url_hash(self, url):
|
|
|
|
return "sha256:%s" % hashlib.sha256(url.lower().encode("utf8")
|
|
|
|
).hexdigest()
|
|
|
|
|
2019-04-27 23:35:41 +00:00
|
|
|
def _get_title(self, channel, url):
|
2019-04-25 14:58:58 +00:00
|
|
|
if not urllib.parse.urlparse(url).scheme:
|
|
|
|
url = "http://%s" % url
|
|
|
|
|
|
|
|
hostname = urllib.parse.urlparse(url).hostname
|
|
|
|
if utils.http.is_localhost(hostname):
|
|
|
|
self.log.warn("tried to get title of localhost: %s", [url])
|
|
|
|
return None
|
|
|
|
|
2019-02-09 10:35:37 +00:00
|
|
|
try:
|
|
|
|
page = utils.http.request(url, soup=True)
|
2019-03-01 23:29:07 +00:00
|
|
|
except utils.http.HTTPWrongContentTypeException:
|
|
|
|
return None
|
2019-02-09 10:35:37 +00:00
|
|
|
except Exception as e:
|
2019-05-07 13:44:29 +00:00
|
|
|
self.log.error("failed to get URL title: %s", [url], exc_info=True)
|
2019-02-09 10:35:37 +00:00
|
|
|
return None
|
2019-02-09 10:38:44 +00:00
|
|
|
if page.data.title:
|
2019-04-27 23:35:41 +00:00
|
|
|
title = page.data.title.text.replace("\n", " ").replace(
|
2019-02-09 10:38:44 +00:00
|
|
|
"\r", "").replace(" ", " ").strip()
|
2019-04-27 23:35:41 +00:00
|
|
|
|
|
|
|
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
|
2019-02-09 10:38:44 +00:00
|
|
|
else:
|
|
|
|
return None
|
2019-02-09 10:35:37 +00:00
|
|
|
|
2019-05-18 17:35:47 +00:00
|
|
|
@utils.hook("command.regex",
|
2019-02-09 10:56:06 +00:00
|
|
|
priority=EventManager.PRIORITY_MONITOR)
|
2019-02-09 10:35:37 +00:00
|
|
|
def channel_message(self, event):
|
2019-05-18 17:35:47 +00:00
|
|
|
"""
|
|
|
|
:command: title
|
|
|
|
:pattern-url: 1
|
|
|
|
"""
|
2019-05-19 09:44:48 +00:00
|
|
|
if event["target"].get_setting("auto-title", False):
|
2019-05-21 08:54:51 +00:00
|
|
|
event.eat()
|
2019-05-19 09:44:48 +00:00
|
|
|
url = event["match"].group(0)
|
|
|
|
title = self._get_title(event["target"], event["match"].group(0))
|
2019-04-24 13:48:15 +00:00
|
|
|
|
2019-05-19 09:44:48 +00:00
|
|
|
if title:
|
|
|
|
message = title
|
|
|
|
if event["target"].get_setting("auto-title-first", False):
|
|
|
|
setting = "url-last-%s" % self._url_hash(url)
|
|
|
|
first_details = event["target"].get_setting(setting, None)
|
2019-04-24 13:48:15 +00:00
|
|
|
|
2019-05-19 09:44:48 +00:00
|
|
|
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["target"].set_setting(setting,
|
|
|
|
[event["user"].nickname, utils.iso8601_format_now(),
|
|
|
|
url])
|
|
|
|
event["stdout"].write(message)
|
2019-02-09 10:35:37 +00:00
|
|
|
|
2018-10-10 09:42:41 +00:00
|
|
|
@utils.hook("received.command.t", alias_of="title")
|
|
|
|
@utils.hook("received.command.title", usage="[URL]")
|
2018-08-31 11:55:52 +00:00
|
|
|
def title(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Get the title of a URL
|
|
|
|
:usage: [URL]
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-08-31 11:55:52 +00:00
|
|
|
url = None
|
|
|
|
if len(event["args"]) > 0:
|
|
|
|
url = event["args_split"][0]
|
|
|
|
else:
|
2019-04-24 14:46:54 +00:00
|
|
|
url = event["target"].buffer.find(utils.http.REGEX_URL)
|
2018-08-31 11:55:52 +00:00
|
|
|
if url:
|
2019-04-24 14:46:54 +00:00
|
|
|
url = re.search(utils.http.REGEX_URL, url.message).group(0)
|
2018-08-31 11:55:52 +00:00
|
|
|
if not url:
|
2018-10-16 14:09:58 +00:00
|
|
|
raise utils.EventError("No URL provided/found.")
|
2018-10-19 09:31:45 +00:00
|
|
|
|
2019-04-27 23:35:41 +00:00
|
|
|
title = self._get_title(event["target"], url)
|
2018-10-19 09:31:45 +00:00
|
|
|
|
2018-08-31 11:55:52 +00:00
|
|
|
if title:
|
|
|
|
event["stdout"].write(title)
|
|
|
|
else:
|
2019-02-09 10:35:37 +00:00
|
|
|
event["stderr"].write("Failed to get title")
|