aboutsummaryrefslogtreecommitdiff
path: root/modules/duckduckgo.py
blob: f43d23761e04c14c2ff8d9e6c369304bd9f7cf61 (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
#--depends-on commands

from src import ModuleManager, utils

URL_DDG = "https://api.duckduckgo.com"

class Module(ModuleManager.BaseModule):
    _name = "DDG"

    @utils.hook("received.command.ddg", min_args=1)
    def duckduckgo(self, event):
        """
        :help: Get first DuckDuckGo result for a given search term
        :usage: [search term]
        """

        phrase = event["args"] or event["target"].buffer.get()
        if phrase:
            page = utils.http.request(URL_DDG, get_params={
                "q": phrase, "format": "json", "no_html": "1",
                "no_redirect": "1"}).json()

            if page and page["AbstractURL"]:
                event["stdout"].write(page["AbstractURL"])
            else:
                event["stderr"].write("No results found")