2010-12-31 19:08:42 -05:00
|
|
|
#!/usr/bin/python2
|
|
|
|
"""
|
|
|
|
ddg.py - duck duck go module
|
|
|
|
author: mutantmonkey <mutantmonkey@gmail.com>
|
|
|
|
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
|
|
|
|
|
2011-04-19 09:20:07 -04:00
|
|
|
def ddg(phenny, input):
|
2011-04-18 21:31:38 -04:00
|
|
|
""".ddg <query> - Search Duck Duck Go for the specified query."""
|
2010-12-31 19:08:42 -05:00
|
|
|
|
|
|
|
query = input.group(2)
|
|
|
|
if not query:
|
|
|
|
return phenny.reply(".ddg what?")
|
|
|
|
|
|
|
|
uri = result(query)
|
2011-01-01 12:18:47 -05:00
|
|
|
resultsuri = "https://duckduckgo.com/?q=" + web.urllib.quote(query.encode('utf-8'))
|
2010-12-31 19:08:42 -05:00
|
|
|
if uri:
|
2011-01-01 12:18:47 -05:00
|
|
|
phenny.reply("%s - Results from %s" % (uri, resultsuri))
|
2010-12-31 19:08:42 -05:00
|
|
|
else:
|
2011-01-01 12:18:47 -05:00
|
|
|
phenny.reply("No results found for '%s'; try %s" % (query, resultsuri))
|
2010-12-31 19:08:42 -05:00
|
|
|
ddg.rule = (['ddg'], r'(.*)')
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
print __doc__.strip()
|
|
|
|
|