aboutsummaryrefslogtreecommitdiff
path: root/modules/wolframalpha.py
blob: 89f3527da01fdc9bcd9f4dd1f03ec6af319dd63e (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# --require-config wolframalpha-api-key

import re
import Utils

URL_WA = "http://api.wolframalpha.com/v2/query"
REGEX_CHARHEX = re.compile("\\\\:(\S{4})")


class Module(object):
    _name = "Wolfram|Alpha"

    def __init__(self, bot):
        bot.events.on("received").on("command").on("wolframalpha", "wa"
                                                   ).hook(self.wa, min_args=1,
                                                          help=
                                                          "Evauate a given string on Wolfram|Alpha",
                                                          usage="<query>")
        self.bot = bot

    def wa(self, event):
        soup = Utils.get_url(URL_WA, get_params={"input": event["args"],
                                                 "appid": self.bot.config[
                                                     "wolframalpha-api-key"],
                                                 "format": "plaintext",
                                                 "reinterpret": "true"},
                             soup=True)

        if soup:
            if int(soup.find("queryresult").get("numpods")) > 0:
                input = soup.find(id="Input").find("subpod").find("plaintext"
                                                                  ).text
                answered = False
                for pod in soup.find_all("pod"):
                    if pod.get("primary") == "true":
                        answer = pod.find("subpod").find("plaintext")
                        text = "(%s) %s" % (input.replace(" | ", ": "),
                                            answer.text.strip().replace(" | ",
                                                                        ": "
                                                                        ).replace(
                                                "\n", " | ").replace("\r", ""))
                        while True:
                            match = re.search(REGEX_CHARHEX, text)
                            if match:
                                text = re.sub(REGEX_CHARHEX, chr(int(
                                    match.group(1), 16)), text)
                            else:
                                break
                        answered = True
                        event["stdout"].write(text)
                        break
                if not answered:
                    event["stderr"].write("No results found")
            else:
                event["stderr"].write("No results found")
        else:
            event["stderr"].write("Failed to load results")