suggest.py: support requesting specific suggestion numbers
This commit is contained in:
parent
23cb9343d0
commit
1c177e12b8
|
@ -1,20 +1,35 @@
|
||||||
import random
|
import random
|
||||||
import urllib
|
import urllib
|
||||||
import urllib2
|
import urllib2
|
||||||
|
import re
|
||||||
|
|
||||||
from util import hook, yaml
|
from util import hook, yaml
|
||||||
|
|
||||||
@hook.command
|
@hook.command
|
||||||
def suggest(inp):
|
def suggest(inp):
|
||||||
".suggest <phrase> -- returns a random suggested google search"
|
".suggest [#n] <phrase> -- gets a random/the nth suggested google search"
|
||||||
if not inp.strip():
|
if not inp.strip():
|
||||||
return suggest.__doc__
|
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='')
|
url = 'http://google.com/complete/search?q=' + urllib.quote(inp, safe='')
|
||||||
json = urllib2.urlopen(url).read()
|
json = urllib2.urlopen(url).read()
|
||||||
json = json[json.find('(') + 1: -1]
|
json = json[json.find('(') + 1: -1]
|
||||||
suggestions = yaml.load(json)[1]
|
suggestions = yaml.load(json)[1]
|
||||||
if not suggestions:
|
if not suggestions:
|
||||||
return 'no suggestions found'
|
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)
|
out = random.choice(suggestions)
|
||||||
return '#%d: %s (%s)' % (int(out[2][0]) + 1, out[0], out[1])
|
return '#%d: %s (%s)' % (int(out[2][0]) + 1, out[0], out[1])
|
||||||
|
|
Loading…
Reference in New Issue