suggest.py: returns a random google suggestion
This commit is contained in:
parent
1115f8d1d9
commit
9f0addbca7
|
@ -0,0 +1,20 @@
|
|||
import random
|
||||
import urllib
|
||||
import urllib2
|
||||
|
||||
from util import hook, yaml
|
||||
|
||||
@hook.command
|
||||
def suggest(inp):
|
||||
".suggest <phrase> -- returns a random suggested google search"
|
||||
if not inp.strip():
|
||||
return suggest.__doc__
|
||||
|
||||
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'
|
||||
suggestion = random.choice(suggestions)
|
||||
return '#%d: %s (%s)' % tuple([int(suggestion[2]) + 1] + suggestion[0:2])
|
Loading…
Reference in New Issue