diff --git a/plugins/suggest.py b/plugins/suggest.py index decd04a..a42dcdc 100644 --- a/plugins/suggest.py +++ b/plugins/suggest.py @@ -1,14 +1,24 @@ import random import urllib import urllib2 +import re from util import hook, yaml @hook.command def suggest(inp): - ".suggest -- returns a random suggested google search" + ".suggest [#n] -- gets a random/the nth suggested google search" if not inp.strip(): return suggest.__doc__ + + m = re.match('^#(\d+) (.+)$', inp) + if m: + num, inp = m.groups() + num = int(num) + if num > 10: + return 'can only get first ten suggestions' + else: + num = 0 url = 'http://google.com/complete/search?q=' + urllib.quote(inp, safe='') json = urllib2.urlopen(url).read() @@ -16,5 +26,10 @@ def suggest(inp): suggestions = yaml.load(json)[1] if not suggestions: return 'no suggestions found' - out = random.choice(suggestions) + if num: + if len(suggestions) + 1 <= num: + return 'only got %d suggestions' % len(suggestions) + out = suggestions[num - 1] + else: + out = random.choice(suggestions) return '#%d: %s (%s)' % (int(out[2][0]) + 1, out[0], out[1])