testpush suggest

This commit is contained in:
Luke Rogers 2014-02-23 20:33:08 +13:00
parent 44cb335bc0
commit c7d2535098
1 changed files with 29 additions and 0 deletions

29
plugins/suggest.py Normal file
View File

@ -0,0 +1,29 @@
import json
from util import hook, http, text
from bs4 import BeautifulSoup
@hook.command
def suggest(inp):
"""suggest <phrase> -- Gets suggested phrases for a google search"""
page = http.get('http://google.com/complete/search',
output='json', client='hp', q=inp)
page_json = page.split('(', 1)[1][:-1]
suggestions = json.loads(page_json)[1]
suggestions = [suggestion[0] for suggestion in suggestions]
if not suggestions:
return 'no suggestions found'
out = u", ".join(suggestions)
# defuckify text
soup = BeautifulSoup(out)
out = soup.get_text()
out = text.truncate_str(out, 200)
return out