aboutsummaryrefslogtreecommitdiff
path: root/modules/title.py
blob: ef4caf59d9b2c46e2109ed1b54e136f4aca351e6 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import re
from src import ModuleManager, utils

REGEX_URL = re.compile("https?://\S+", re.I)

class Module(ModuleManager.BaseModule):
    @utils.hook("received.command.t", alias_of="title")
    @utils.hook("received.command.title", usage="[URL]")
    def title(self, event):
        """
        :help: Get the title of a URL
        :usage: [URL]
        """
        url = None
        if len(event["args"]) > 0:
            url = event["args_split"][0]
        else:
            url = event["target"].buffer.find(REGEX_URL)
            if url:
                url = re.search(REGEX_URL, url.message).group(0)
        if not url:
            raise utils.EventError("No URL provided/found.")

        try:
            page = utils.http.request(url, soup=True)
        except:
            pass

        if not page:
            raise utils.EventError("Failed to get URL.")

        title = page.data.title
        if title:
            title = title.text.replace("\n", " ").replace("\r", ""
                ).replace("  ", " ").strip()
            event["stdout"].write(title)
        else:
            event["stderr"].write("No title found.")