bitbot-3.11-fork/modules/google.py

35 lines
1.3 KiB
Python
Raw Normal View History

2018-08-31 09:50:37 +00:00
# --require-config google-api-key
# --require-config google-search-id
2016-03-29 11:56:58 +00:00
import Utils
URL_GOOGLESEARCH = "https://www.googleapis.com/customsearch/v1"
2018-08-31 09:50:37 +00:00
2016-03-29 11:56:58 +00:00
class Module(object):
def __init__(self, bot):
self.bot = bot
bot.events.on("received").on("command").on("google",
2018-08-31 09:50:37 +00:00
"g").hook(self.google,
help="Google feeling lucky",
usage="[search term]")
2016-03-29 11:56:58 +00:00
def google(self, event):
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:50:37 +00:00
"google-api-key"], "cx": self.bot.config[
"google-search-id"], "prettyPrint": "true",
"num": 1, "gl": "gb"}, json=True)
2016-03-29 11:56:58 +00:00
if page:
if "items" in page and len(page["items"]):
event["stdout"].write(page["items"][0][
2018-08-31 09:50:37 +00:00
"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")