2010-01-17 04:24:36 +00:00
|
|
|
import random
|
|
|
|
import re
|
|
|
|
|
2010-04-23 03:47:41 +00:00
|
|
|
from util import hook, http
|
2010-01-17 04:24:36 +00:00
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2010-01-17 04:24:36 +00:00
|
|
|
@hook.command
|
2014-03-13 17:38:18 +00:00
|
|
|
def suggest(inp, inp_unstripped=None):
|
2010-01-17 04:24:36 +00:00
|
|
|
".suggest [#n] <phrase> -- gets a random/the nth suggested google search"
|
2010-08-30 03:35:27 +00:00
|
|
|
|
2014-03-13 17:38:18 +00:00
|
|
|
if inp_unstripped is not None:
|
|
|
|
inp = inp_unstripped
|
2010-01-17 04:24:36 +00:00
|
|
|
m = re.match('^#(\d+) (.+)$', inp)
|
2014-03-13 17:38:18 +00:00
|
|
|
num = 0
|
2010-01-17 04:24:36 +00:00
|
|
|
if m:
|
|
|
|
num, inp = m.groups()
|
|
|
|
num = int(num)
|
|
|
|
|
2014-03-13 17:38:18 +00:00
|
|
|
json = http.get_json('http://suggestqueries.google.com/complete/search', client='firefox', q=inp)
|
|
|
|
suggestions = json[1]
|
2010-01-17 04:24:36 +00:00
|
|
|
if not suggestions:
|
|
|
|
return 'no suggestions found'
|
2014-03-13 17:38:18 +00:00
|
|
|
|
|
|
|
if not num:
|
|
|
|
num = random.randint(1, len(suggestions))
|
|
|
|
if len(suggestions) + 1 <= num:
|
|
|
|
return 'only got %d suggestions' % len(suggestions)
|
|
|
|
out = suggestions[num - 1]
|
|
|
|
return '#%d: %s' % (num, out)
|