2016-03-29 11:56:58 +00:00
|
|
|
import re
|
2018-09-26 17:27:17 +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):
|
|
|
|
@Utils.hook("received.command.title|t", usage="[URL]")
|
2018-08-31 11:55:52 +00:00
|
|
|
def title(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
|
|
|
Get the title of a URL
|
|
|
|
"""
|
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:
|
|
|
|
event["stderr"].write("No URL provided/found.")
|
|
|
|
return
|
|
|
|
soup = Utils.get_url(url, soup=True)
|
|
|
|
if not soup:
|
|
|
|
event["stderr"].write("Failed to get URL.")
|
|
|
|
return
|
|
|
|
title = soup.title
|
|
|
|
if title:
|
|
|
|
title = title.text.replace("\n", " ").replace("\r", ""
|
|
|
|
).replace(" ", " ").strip()
|
|
|
|
event["stdout"].write(title)
|
|
|
|
else:
|
|
|
|
event["stderr"].write("No title found.")
|