2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on commands
|
|
|
|
#--depends-on config
|
2018-08-31 09:51:47 +00:00
|
|
|
#--require-config google-api-key
|
|
|
|
#--require-config google-search-id
|
2016-03-30 18:31:23 +00:00
|
|
|
|
2018-09-26 13:36:33 +00:00
|
|
|
import json
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import ModuleManager, utils
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
URL_GOOGLESEARCH = "https://www.googleapis.com/customsearch/v1"
|
2018-09-26 13:36:33 +00:00
|
|
|
URL_GOOGLESUGGEST = "http://google.com/complete/search"
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2019-06-28 22:16:05 +00:00
|
|
|
@utils.export("channelset", utils.BoolSetting("google-safesearch",
|
|
|
|
"Turn safe search off/on"))
|
2018-09-27 10:46:10 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2018-10-10 09:42:41 +00:00
|
|
|
@utils.hook("received.command.g", alias_of="google")
|
|
|
|
@utils.hook("received.command.google")
|
2016-03-29 11:56:58 +00:00
|
|
|
def google(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Get first Google result for a given search term
|
|
|
|
:usage: [search term]
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-12-15 22:53:27 +00:00
|
|
|
|
2018-09-30 10:43:34 +00:00
|
|
|
phrase = event["args"] or event["target"].buffer.get()
|
2016-03-29 11:56:58 +00:00
|
|
|
if phrase:
|
2018-12-15 22:57:38 +00:00
|
|
|
safe_setting = event["target"].get_setting("google-safesearch",
|
2018-12-15 22:53:27 +00:00
|
|
|
True)
|
|
|
|
safe = "active" if safe_setting else "off"
|
|
|
|
|
2018-12-11 22:26:38 +00:00
|
|
|
page = utils.http.request(URL_GOOGLESEARCH, get_params={
|
2019-11-26 13:41:40 +00:00
|
|
|
"q": phrase, "prettyPrint": "true", "num": 1, "gl": "gb",
|
|
|
|
"key": self.bot.config["google-api-key"],
|
|
|
|
"cx": self.bot.config["google-search-id"],
|
|
|
|
"safe": safe}).json()
|
2016-03-29 11:56:58 +00:00
|
|
|
if page:
|
2019-11-26 13:41:40 +00:00
|
|
|
if "items" in page and len(page["items"]):
|
|
|
|
item = page["items"][0]
|
2019-11-25 17:48:31 +00:00
|
|
|
link = item["link"]
|
|
|
|
title = utils.parse.line_normalise(item["title"])
|
2018-10-03 12:22:37 +00:00
|
|
|
event["stdout"].write(
|
2019-11-25 17:48:31 +00:00
|
|
|
"(%s) %s - %s" % (phrase, title, link))
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("No results found")
|
|
|
|
else:
|
2019-11-18 12:06:59 +00:00
|
|
|
raise utils.EventResultsError()
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("No phrase provided")
|
2018-09-26 13:36:33 +00:00
|
|
|
|
2018-10-03 14:59:49 +00:00
|
|
|
@utils.hook("received.command.suggest")
|
2018-09-26 13:36:33 +00:00
|
|
|
def suggest(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-10-03 14:59:49 +00:00
|
|
|
:help: Get suggested phrases from Google
|
|
|
|
:usage: [phrase]
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 10:43:34 +00:00
|
|
|
phrase = event["args"] or event["target"].buffer.get()
|
2018-09-26 13:36:33 +00:00
|
|
|
if phrase:
|
2018-12-11 22:26:38 +00:00
|
|
|
page = utils.http.request(URL_GOOGLESUGGEST, get_params={
|
2019-11-26 13:41:40 +00:00
|
|
|
"output": "json", "client": "hp", "gl": "gb", "q": phrase}
|
|
|
|
).json()
|
2018-09-26 13:36:33 +00:00
|
|
|
if page:
|
|
|
|
# google gives us jsonp, so we need to unwrap it.
|
2019-11-26 13:41:40 +00:00
|
|
|
page = page.split("(", 1)[1][:-1]
|
2018-09-26 13:36:33 +00:00
|
|
|
page = json.loads(page)
|
|
|
|
suggestions = page[1]
|
2018-10-08 22:34:22 +00:00
|
|
|
suggestions = [utils.http.strip_html(s[0]) for s in suggestions]
|
2018-09-26 13:36:33 +00:00
|
|
|
|
|
|
|
if suggestions:
|
|
|
|
event["stdout"].write("%s: %s" % (phrase,
|
|
|
|
", ".join(suggestions)))
|
|
|
|
else:
|
|
|
|
event["stderr"].write("No suggestions found")
|
|
|
|
else:
|
2019-11-18 12:06:59 +00:00
|
|
|
raise utils.EventResultsError()
|
2018-09-26 13:36:33 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("No phrase provided")
|