From 9f0addbca7c121170d23bb18005cbd284214df5f Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Fri, 10 Jul 2009 01:10:14 -0600 Subject: [PATCH] suggest.py: returns a random google suggestion --- plugins/suggest.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 plugins/suggest.py diff --git a/plugins/suggest.py b/plugins/suggest.py new file mode 100644 index 0000000..2febde3 --- /dev/null +++ b/plugins/suggest.py @@ -0,0 +1,20 @@ +import random +import urllib +import urllib2 + +from util import hook, yaml + +@hook.command +def suggest(inp): + ".suggest -- returns a random suggested google search" + if not inp.strip(): + return suggest.__doc__ + + url = 'http://google.com/complete/search?q=' + urllib.quote(inp, safe='') + json = urllib2.urlopen(url).read() + json = json[json.find('(') + 1: -1] + suggestions = yaml.load(json)[1] + if not suggestions: + return 'no suggestions found' + suggestion = random.choice(suggestions) + return '#%d: %s (%s)' % tuple([int(suggestion[2]) + 1] + suggestion[0:2])