2010-04-23 03:47:41 +00:00
|
|
|
import json
|
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
|
2010-02-02 04:42:34 +00:00
|
|
|
def suggest(inp, inp_unstripped=''):
|
2010-01-17 04:24:36 +00:00
|
|
|
".suggest [#n] <phrase> -- gets a random/the nth suggested google search"
|
2010-05-07 23:16:44 +00:00
|
|
|
|
2010-02-02 04:42:34 +00:00
|
|
|
inp = inp_unstripped
|
2010-01-17 04:24:36 +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
|
|
|
|
|
2010-04-23 03:47:41 +00:00
|
|
|
page = http.get('http://google.com/complete/search', q=inp)
|
2010-01-17 04:24:36 +00:00
|
|
|
page_json = page.split('(', 1)[1][:-1]
|
|
|
|
suggestions = json.loads(page_json)[1]
|
|
|
|
if not suggestions:
|
|
|
|
return 'no suggestions found'
|
|
|
|
if num:
|
|
|
|
if len(suggestions) + 1 <= num:
|
|
|
|
return 'only got %d suggestions' % len(suggestions)
|
|
|
|
out = suggestions[num - 1]
|
|
|
|
else:
|
|
|
|
out = random.choice(suggestions)
|
2010-08-10 19:38:09 +00:00
|
|
|
return '#%d: %s' % (int(out[2][0]) + 1, out[0])
|