aboutsummaryrefslogtreecommitdiff
path: root/modules/define.py
blob: 7c3a816b11d90ffdaddee6a8883f7f66498c87de (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
58
59
60
61
62
63
64
65
66
#--depends-on commands
#--require-config wordnik-api-key

import time
from src import ModuleManager, utils

URL_WORDNIK = "https://api.wordnik.com/v4/word.json/%s/definitions"
URL_WORDNIK_RANDOM = "https://api.wordnik.com/v4/words.json/randomWord"

RANDOM_DELAY_SECONDS = 3

class Module(ModuleManager.BaseModule):
    _last_called = 0

    def _get_definition(self, word):
        page = utils.http.request(URL_WORDNIK % word, get_params={
            "useCanonical": "true", "limit": 1,
            "sourceDictionaries": "wiktionary",
            "api_key": self.bot.config["wordnik-api-key"]})

        if page:
            if page.code == 200:
                return True, page.json()[0]
            else:
                return True, None
        else:
            return False, None

    @utils.hook("received.command.define")
    @utils.kwarg("help", "Define a provided term")
    @utils.spec("!<term>lstring")
    def define(self, event):
        word = event["spec"][0].replace(" ", "+")

        success, definition = self._get_definition(word)
        if success:
            if not definition == None:
                text = utils.http.strip_html(definition["text"])
                event["stdout"].write("%s: %s" % (definition["word"], text))
            else:
                event["stderr"].write("No definitions found")
        else:
            raise utils.EventResultsError()

    @utils.hook("received.command.randomword")
    @utils.kwarg("help", "Define a random word")
    def random_word(self, event):
        if not self._last_called or (time.time()-self._last_called >=
                RANDOM_DELAY_SECONDS):
            self._last_called = time.time()

            page = utils.http.request(URL_WORDNIK_RANDOM, get_params={
                "api_key": self.bot.config["wordnik-api-key"],
                "min_dictionary_count": 1}).json()
            if page:
                success, definition = self._get_definition(page["word"])
                if not success:
                    raise utils.EventError("Try again in a couple of seconds")

                text = utils.http.strip_html(definition["text"])
                event["stdout"].write("Random Word: %s - Definition: %s" % (
                    page["word"], text))
            else:
                raise utils.EventResultsError()
        else:
            event["stderr"].write("Try again in a couple of seconds")