Support turning safe search on/off in google.py

This commit is contained in:
jesopo 2018-12-15 22:53:27 +00:00
parent 1ffc7863a0
commit 9658d0285e

View file

@ -7,6 +7,9 @@ from src import ModuleManager, utils
URL_GOOGLESEARCH = "https://www.googleapis.com/customsearch/v1" URL_GOOGLESEARCH = "https://www.googleapis.com/customsearch/v1"
URL_GOOGLESUGGEST = "http://google.com/complete/search" URL_GOOGLESUGGEST = "http://google.com/complete/search"
@utils.export("channelset", {"setting": "google-safesearch",
"help": "Turn safe search off/on",
"validate": utils.bool_or_none})
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
@utils.hook("received.command.g", alias_of="google") @utils.hook("received.command.g", alias_of="google")
@utils.hook("received.command.google") @utils.hook("received.command.google")
@ -15,13 +18,18 @@ class Module(ModuleManager.BaseModule):
:help: Get first Google result for a given search term :help: Get first Google result for a given search term
:usage: [search term] :usage: [search term]
""" """
phrase = event["args"] or event["target"].buffer.get() phrase = event["args"] or event["target"].buffer.get()
if phrase: if phrase:
safe_setting = event["channel"].get_setting("google-safesearch",
True)
safe = "active" if safe_setting else "off"
page = utils.http.request(URL_GOOGLESEARCH, get_params={ page = utils.http.request(URL_GOOGLESEARCH, get_params={
"q": phrase, "key": self.bot.config[ "q": phrase, "key": self.bot.config[
"google-api-key"], "cx": self.bot.config[ "google-api-key"], "cx": self.bot.config[
"google-search-id"], "prettyPrint": "true", "google-search-id"], "prettyPrint": "true",
"num": 1, "gl": "gb"}, json=True) "num": 1, "gl": "gb", "safe": safe}, json=True)
if page: if page:
if "items" in page.data and len(page.data["items"]): if "items" in page.data and len(page.data["items"]):
event["stdout"].write( event["stdout"].write(