diff options
| author | 2016-03-29 12:56:58 +0100 | |
|---|---|---|
| committer | 2016-03-29 12:56:58 +0100 | |
| commit | f943d63098a50746f4e470e403a991a4d9713030 (patch) | |
| tree | deeb98058917d0155227211d72576f0cbab28d3f /modules/google.py | |
| parent | Initial commit (diff) | |
first commit.
Diffstat (limited to 'modules/google.py')
| -rw-r--r-- | modules/google.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/modules/google.py b/modules/google.py new file mode 100644 index 00000000..8d00945f --- /dev/null +++ b/modules/google.py @@ -0,0 +1,28 @@ +import Utils + +URL_GOOGLESEARCH = "https://www.googleapis.com/customsearch/v1" + +class Module(object): + def __init__(self, bot): + self.bot = bot + bot.events.on("received").on("command").on("google", + "g").hook(self.google, help="Google feeling lucky") + + def google(self, event): + phrase = event["args"] or event["log"].get() + if phrase: + page = Utils.get_url(URL_GOOGLESEARCH, get_params={ + "q": phrase, "key": self.bot.config[ + "google-api-key"], "cx": self.bot.config[ + "google-search-id"], "prettyPrint": "true", + "num": 1, "gl": "us"}, json=True) + if page: + if "items" in page and len(page["items"]): + event["stdout"].write(page["items"][0][ + "link"]) + else: + event["stderr"].write("No results found") + else: + event["stderr"].write("Failed to load results") + else: + event["stderr"].write("No phrase provided") |
