2016-03-29 11:56:58 +00:00
|
|
|
import re
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, utils
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
REGEX_URL = re.compile("https?://\S+", re.I)
|
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
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:
|
2018-09-30 10:43:34 +00:00
|
|
|
url = event["target"].buffer.find(REGEX_URL)
|
2018-08-31 11:55:52 +00:00
|
|
|
if url:
|
|
|
|
url = re.search(REGEX_URL, url.message).group(0)
|
|
|
|
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
|
|
|
|
|
|
|
try:
|
2018-12-11 22:26:38 +00:00
|
|
|
page = utils.http.request(url, soup=True)
|
2019-01-21 14:39:20 +00:00
|
|
|
except Exception as e:
|
|
|
|
self.log.error("failed to get URL title", exc_info=True)
|
|
|
|
page = None
|
2018-10-19 09:31:45 +00:00
|
|
|
pass
|
|
|
|
|
2018-12-11 22:26:38 +00:00
|
|
|
if not page:
|
2018-10-16 14:09:58 +00:00
|
|
|
raise utils.EventError("Failed to get URL.")
|
2018-10-19 09:31:45 +00:00
|
|
|
|
2018-12-11 22:26:38 +00:00
|
|
|
title = page.data.title
|
2018-08-31 11:55:52 +00:00
|
|
|
if title:
|
|
|
|
title = title.text.replace("\n", " ").replace("\r", ""
|
|
|
|
).replace(" ", " ").strip()
|
|
|
|
event["stdout"].write(title)
|
|
|
|
else:
|
|
|
|
event["stderr"].write("No title found.")
|