From bfb26c32d6633d9dd32521d94fe0dfa6b3c98cde Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Fri, 31 Dec 2010 19:08:42 -0500 Subject: [PATCH] Added Duck Duck Go search functionality (only supports zero-click API right now) --- modules/ddg.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 modules/ddg.py diff --git a/modules/ddg.py b/modules/ddg.py new file mode 100755 index 0000000..e06e6cc --- /dev/null +++ b/modules/ddg.py @@ -0,0 +1,45 @@ +#!/usr/bin/python2 +""" +ddg.py - duck duck go module +author: mutantmonkey +portions based on search.py by sean b palmer +""" + +import random + +from urllib import quote as urlquote +from urllib2 import urlopen, HTTPError +import lxml.html + +import web + +def search(query): + uri = 'https://api.duckduckgo.com/' + args = '?q=%s&o=json' % web.urllib.quote(query.encode('utf-8')) + bytes = web.get(uri + args) + return web.json(bytes) + +def result(query): + results = search(query) + try: + return results['Results'][0]['FirstURL'] + except IndexError: + return None + +def ddg(phenny, input, celsius=False): + """.tfw - Search Duck Duck Go for the specified query.""" + + query = input.group(2) + if not query: + return phenny.reply(".ddg what?") + + uri = result(query) + if uri: + phenny.reply("%s - Results from https://duckduckgo.com/" % uri) + else: + phenny.reply("No results found for '%s'." % query) +ddg.rule = (['ddg'], r'(.*)') + +if __name__ == '__main__': + print __doc__.strip() +