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-24 14:13:27 +00:00
|
|
|
from src import Utils
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
URL_GOOGLESEARCH = "https://www.googleapis.com/customsearch/v1"
|
|
|
|
|
|
|
|
class Module(object):
|
2018-09-02 18:54:45 +00:00
|
|
|
def __init__(self, bot, events, exports):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.bot = bot
|
2018-09-19 12:25:12 +00:00
|
|
|
events.on("received.command").on("google", "g").hook(self.google,
|
|
|
|
help="Google feeling lucky", usage="[search term]")
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
def google(self, event):
|
2018-08-28 11:23:57 +00:00
|
|
|
phrase = event["args"] or event["buffer"].get()
|
2016-03-29 11:56:58 +00:00
|
|
|
if phrase:
|
|
|
|
page = Utils.get_url(URL_GOOGLESEARCH, get_params={
|
|
|
|
"q": phrase, "key": self.bot.config[
|
2018-08-31 09:51:47 +00:00
|
|
|
"google-api-key"], "cx": self.bot.config[
|
|
|
|
"google-search-id"], "prettyPrint": "true",
|
2018-08-31 03:14:56 +00:00
|
|
|
"num": 1, "gl": "gb"}, json=True)
|
2016-03-29 11:56:58 +00:00
|
|
|
if page:
|
|
|
|
if "items" in page and len(page["items"]):
|
2018-09-08 21:53:31 +00:00
|
|
|
event["stdout"].write("(" + Utils.bold(phrase) + ") " \
|
|
|
|
+ page["items"][0]["link"])
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("No results found")
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Failed to load results")
|
|
|
|
else:
|
|
|
|
event["stderr"].write("No phrase provided")
|