aboutsummaryrefslogtreecommitdiff
path: root/modules/wikipedia.py
blob: 63def32dc2b4330c6b9d8550038101d0c104877d (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
from src import ModuleManager, utils

URL_WIKIPEDIA = "https://en.wikipedia.org/w/api.php"

class Module(ModuleManager.BaseModule):
    @utils.hook("received.command.wi", alias_of="wiki")
    @utils.hook("received.command.wiki", min_args=1)
    def wikipedia(self, event):
        """
        :help: Get information from wikipedia
        :usage: <term>
        """
        page = utils.http.get_url(URL_WIKIPEDIA, get_params={
            "action": "query", "prop": "extracts",
            "titles": event["args"], "exintro": "",
            "explaintext": "", "exchars": "500",
            "redirects": "", "format": "json"}, json=True)
        if page:
            pages = page["query"]["pages"]
            article = list(pages.items())[0][1]
            if not "missing" in article:
                title, info = article["title"], article["extract"]
                info = info.replace("\n\n", " ").split("\n")[0]
                event["stdout"].write("%s: %s" % (title, info))
            else:
                event["stderr"].write("No results found")
        else:
            raise utils.EventsResultsError()