aboutsummaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorGravatar jesopo2018-09-26 14:36:33 +0100
committerGravatar jesopo2018-09-26 14:36:33 +0100
commit8d946fb60ba0ce641cc870ab35ba4115b34fe70e (patch)
tree208ca59fc7b436ab8c61d75d7d2fcad3e59b56ac /modules
parentComment config options! (diff)
signature
Add !suggest, to get suggested searches from google
Diffstat (limited to 'modules')
-rw-r--r--modules/google.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/modules/google.py b/modules/google.py
index 42acf954..1d7b6e8c 100644
--- a/modules/google.py
+++ b/modules/google.py
@@ -1,15 +1,19 @@
#--require-config google-api-key
#--require-config google-search-id
+import json
from src import Utils
URL_GOOGLESEARCH = "https://www.googleapis.com/customsearch/v1"
+URL_GOOGLESUGGEST = "http://google.com/complete/search"
class Module(object):
def __init__(self, bot, events, exports):
self.bot = bot
events.on("received.command").on("google", "g").hook(self.google,
help="Google feeling lucky", usage="[search term]")
+ events.on("received.command.suggest").hook(self.suggest,
+ help="Get suggested phrases from Google", usage="[phrase]")
def google(self, event):
phrase = event["args"] or event["buffer"].get()
@@ -29,3 +33,25 @@ class Module(object):
event["stderr"].write("Failed to load results")
else:
event["stderr"].write("No phrase provided")
+
+ def suggest(self, event):
+ phrase = event["args"] or event["buffer"].get()
+ if phrase:
+ page = Utils.get_url(URL_GOOGLESUGGEST, get_params={
+ "output": "json", "client": "hp", "q": phrase})
+ if page:
+ # google gives us jsonp, so we need to unwrap it.
+ page = page.split("(", 1)[1][:-1]
+ page = json.loads(page)
+ suggestions = page[1]
+ suggestions = [Utils.strip_html(s[0]) for s in suggestions]
+
+ if suggestions:
+ event["stdout"].write("%s: %s" % (phrase,
+ ", ".join(suggestions)))
+ else:
+ event["stderr"].write("No suggestions found")
+ else:
+ event["stderr"].write("Failed to load results")
+ else:
+ event["stderr"].write("No phrase provided")