From 1894a134f8bf91935296fddaf3972c0f727f6b1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yago=20Gonz=C3=A1lez?= Date: Fri, 17 Mar 2017 19:44:43 +0000 Subject: [PATCH] bots: Move Google search bot and fix bugs. Now this bot follows our latest structure for contrib_bots. Switched the dependencies instructions, to install "google" rather than "google-api-python-client". Added all the search terms to the query (not only the first one). --- .../googlesearch}/googlesearch.py | 26 +++++-------------- 1 file changed, 6 insertions(+), 20 deletions(-) rename contrib_bots/{lib => bots/googlesearch}/googlesearch.py (78%) diff --git a/contrib_bots/lib/googlesearch.py b/contrib_bots/bots/googlesearch/googlesearch.py similarity index 78% rename from contrib_bots/lib/googlesearch.py rename to contrib_bots/bots/googlesearch/googlesearch.py index c5bdd37..97e3d13 100644 --- a/contrib_bots/lib/googlesearch.py +++ b/contrib_bots/bots/googlesearch/googlesearch.py @@ -3,13 +3,12 @@ from __future__ import print_function import logging import http.client from six.moves.urllib.request import urlopen + +# Uses the Google search engine bindings +# pip install --upgrade google from google import search -# Uses Google's Client Library -# pip install --upgrade google-api-python-client - -def get_google_result(original_content): - search_keywords = original_content.strip().split(' ', 1)[1] +def get_google_result(search_keywords): if search_keywords == 'help': help_message = "To use this bot start message with @google \ followed by what you want to search for. If \ @@ -29,7 +28,7 @@ def get_google_result(original_content): return 'Error: Search failed. {}.'.format(e) if not urls: - return "No URLs returned by google." + return 'No URLs returned by google.' url = next(urls) @@ -53,19 +52,6 @@ class GoogleSearchHandler(object): should preface messages with @google. ''' - def triage_message(self, message, client): - # return True if we want to (possibly) respond to this message - - original_content = message['content'] - - # This next line of code is defensive, as we - # never want to get into an infinite loop of posting google - # search links. - if message['display_recipient'] == 'google': - return False - - return original_content.startswith('@google') - def handle_message(self, message, client, state_handler): original_content = message['content'] original_sender = message['sender_email'] @@ -90,7 +76,7 @@ handler_class = GoogleSearchHandler def test(): try: urlopen('http://216.58.192.142', timeout=1) - print("Success") + print('Success') return True except http.client.RemoteDisconnected as e: print('Error: {}'.format(e))