2009-07-10 07:10:14 +00:00
|
|
|
import random
|
|
|
|
import urllib
|
|
|
|
import urllib2
|
2009-07-17 22:06:45 +00:00
|
|
|
import re
|
2009-07-10 07:10:14 +00:00
|
|
|
|
|
|
|
from util import hook, yaml
|
|
|
|
|
|
|
|
@hook.command
|
|
|
|
def suggest(inp):
|
2009-07-17 22:06:45 +00:00
|
|
|
".suggest [#n] <phrase> -- gets a random/the nth suggested google search"
|
2009-07-10 07:10:14 +00:00
|
|
|
if not inp.strip():
|
|
|
|
return suggest.__doc__
|
2009-07-17 22:06:45 +00:00
|
|
|
|
|
|
|
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
|
2009-07-10 07:10:14 +00:00
|
|
|
|
|
|
|
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'
|
2009-07-17 22:06:45 +00:00
|
|
|
if num:
|
|
|
|
if len(suggestions) + 1 <= num:
|
|
|
|
return 'only got %d suggestions' % len(suggestions)
|
|
|
|
out = suggestions[num - 1]
|
|
|
|
else:
|
|
|
|
out = random.choice(suggestions)
|
2009-07-10 07:18:02 +00:00
|
|
|
return '#%d: %s (%s)' % (int(out[2][0]) + 1, out[0], out[1])
|