From d8264c18d8f5f8526d1cb654ecd733ddf2bf50aa Mon Sep 17 00:00:00 2001 From: Edward Powell Date: Sun, 11 Nov 2012 12:51:20 -0500 Subject: [PATCH] [search] Enable !bang for .ddg Not for .search, since it wouldn't make sense --- search.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/search.py b/search.py index 3f54a7225a..2612f63331 100644 --- a/search.py +++ b/search.py @@ -142,13 +142,32 @@ def duck_search(query): m = r_duck.search(bytes) if m: return web.decode(m.group(1)) +def duck_api(query): + uri = web.quote(query) + uri = 'http://api.duckduckgo.com/?q=%s&format=json&no_html=1&no_redirect=1'%query + results = json.loads(web.get(uri)) + print results + if results['Redirect']: + return results['Redirect'] + else: + return None + def duck(willie, trigger): """Queries Duck Duck Go for the specified input.""" query = trigger.group(2) - if not query: return willie.reply('.ddg what?') - + if not query: + return willie.reply('.ddg what?') query = query.encode('utf-8') - uri = duck_search(query) + + #If the API gives us something, say it and stop + result = duck_api(query) + if result: + willie.reply(result) + return + + #Otherwise, look it up on the HTMl version + uri= duck_search(query) + if uri: willie.reply(uri) if not hasattr(willie.bot, 'last_seen_uri'):