From c7d25350988e477be675fa49da6d423f11d06938 Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Sun, 23 Feb 2014 20:33:08 +1300 Subject: [PATCH] testpush suggest --- plugins/suggest.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 plugins/suggest.py diff --git a/plugins/suggest.py b/plugins/suggest.py new file mode 100644 index 0000000..6fd4fdd --- /dev/null +++ b/plugins/suggest.py @@ -0,0 +1,29 @@ +import json + +from util import hook, http, text +from bs4 import BeautifulSoup + + +@hook.command +def suggest(inp): + """suggest -- 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