guppy /plugins/ddg.py

Language Python Lines 56
MD5 Hash 977c147a837af7c8c9a96dcf6f7328b9 Estimated Cost $609 (why?)
Repository https://bitbucket.org/dwfreed/guppy.git View Raw File View Project SPDX
 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
#    guppy Copyright (C) 2010-2011 guppy team members.
#
#    This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
#    This is free software, and you are welcome to redistribute it
#    under certain conditions; type `show c' for details.
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.

# ddg module
# --gry

import urllib.request
from urllib.error import HTTPError,URLError
import socket
import json

@plugin
class ddg(object):
    """DuckDuckGo search tools. Currently just 0-click info."""
    def __init__(self, server):
        self.server = server
        self.commands = ["ddg"]
        self.server.handle("command", self.handle_command, self.commands)
    
    def handle_command(self, channel, user, cmd, args):
        if len(args) < 1:
            self.server.doMessage(channel, user+": DuckDuckGo.com Zero-Click infoboxes search. Syntax: ddg <query>.")
            return
        try:
            request = "+".join(args)
            sock = urllib.request.urlopen("http://api.duckduckgo.com/?q=%s&o=json" % request)
            data = sock.read()
            sock.close()
            data = data.decode('utf-8')
            jl = json.loads(data)
            if str(jl["AbstractText"]) != "":
                self.server.doMessage(channel, user+": "+"%s %s)"%(str(jl["AbstractURL"]),str(jl["AbstractText"][0:200])))
            elif str(jl["Definition"]) != "":
                self.server.doMessage(channel, user+": "+"%s %s"%(str(jl["DefinitionURL"]),str(jl["Definition"])))
        except Exception as e:
            self.server.doMessage(channel, user+": "+str(e))
Back to Top