diff --git a/requirements.txt b/requirements.txt index c5d735436..3c2af8284 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,7 +10,6 @@ ruff~=0.2.2 -e ./zulip_botserver git+https://github.com/zulip/zulint@417b4e4971fdd5ca8e84847f1391b657b188631a#egg=zulint==1.0.0 mypy==1.8.0 -types-beautifulsoup4 types-httplib2 types-python-dateutil types-pytz diff --git a/tools/run-mypy b/tools/run-mypy index 907488aeb..e0ac87756 100755 --- a/tools/run-mypy +++ b/tools/run-mypy @@ -33,8 +33,6 @@ force_include = [ "zulip_bots/zulip_bots/bots/giphy/test_giphy.py", "zulip_bots/zulip_bots/bots/github_detail/github_detail.py", "zulip_bots/zulip_bots/bots/github_detail/test_github_detail.py", - "zulip_bots/zulip_bots/bots/google_search/google_search.py", - "zulip_bots/zulip_bots/bots/google_search/test_google_search.py", "zulip_bots/zulip_bots/bots/help/help.py", "zulip_bots/zulip_bots/bots/help/test_help.py", "zulip_bots/zulip_bots/bots/incrementor/incrementor.py", diff --git a/zulip_bots/setup.py b/zulip_bots/setup.py index 490017ece..296ebfed9 100644 --- a/zulip_bots/setup.py +++ b/zulip_bots/setup.py @@ -53,8 +53,6 @@ "pip", "zulip", "html2text", - "lxml", - "BeautifulSoup4", "typing_extensions>=4.5.0", "importlib-metadata>=3.6", ], diff --git a/zulip_bots/zulip_bots/bots/google_search/__init__.py b/zulip_bots/zulip_bots/bots/google_search/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/zulip_bots/zulip_bots/bots/google_search/doc.md b/zulip_bots/zulip_bots/bots/google_search/doc.md deleted file mode 100644 index 4c4bea5b5..000000000 --- a/zulip_bots/zulip_bots/bots/google_search/doc.md +++ /dev/null @@ -1,23 +0,0 @@ -# Google Search bot - -This bot allows users to do Google search queries and have the bot -respond with the first search result. It is by default set to the -highest safe-search setting. - -## Usage - -Run this bot as described -[here](https://zulip.com/api/running-bots#running-a-bot). - -Use this bot with the following command - -`@mentioned-bot ` - -This will return the first link found by Google for `` -and print the resulting URL. - -If no `` are entered, a help message is printed instead. - -If there was an error in the process of running the search (socket -errors, Google search function failed, or general failures), an error -message is returned. diff --git a/zulip_bots/zulip_bots/bots/google_search/fixtures/test_attribute_error.json b/zulip_bots/zulip_bots/bots/google_search/fixtures/test_attribute_error.json deleted file mode 100644 index 6b3f28fb3..000000000 --- a/zulip_bots/zulip_bots/bots/google_search/fixtures/test_attribute_error.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "request": { - "api_url": "http://www.google.com/search", - "params": { - "q": "test" - } - }, - "response": "
", - "response-headers": { - "status": 200, - "content-type": "text/html; charset=utf-8" - } -} diff --git a/zulip_bots/zulip_bots/bots/google_search/fixtures/test_ignore_links.json b/zulip_bots/zulip_bots/bots/google_search/fixtures/test_ignore_links.json deleted file mode 100644 index 39fecece3..000000000 --- a/zulip_bots/zulip_bots/bots/google_search/fixtures/test_ignore_links.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "request": { - "api_url": "http://www.google.com/search", - "params": { - "q": "zulip" - } - }, - "response": "", - "response-headers": { - "status": 200, - "content-type": "text/html; charset=utf-8" - } -} diff --git a/zulip_bots/zulip_bots/bots/google_search/fixtures/test_no_result.json b/zulip_bots/zulip_bots/bots/google_search/fixtures/test_no_result.json deleted file mode 100644 index f3355af0f..000000000 --- a/zulip_bots/zulip_bots/bots/google_search/fixtures/test_no_result.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "request": { - "api_url": "http://www.google.com/search", - "params": { - "q": "no res" - } - }, - "response": "", - "response-headers": { - "status": 200, - "content-type": "text/html; charset=utf-8" - } - } diff --git a/zulip_bots/zulip_bots/bots/google_search/fixtures/test_normal.json b/zulip_bots/zulip_bots/bots/google_search/fixtures/test_normal.json deleted file mode 100644 index 413625cd4..000000000 --- a/zulip_bots/zulip_bots/bots/google_search/fixtures/test_normal.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "request": { - "api_url": "http://www.google.com/search", - "params": { - "q": "zulip" - } - }, - "response": "", - "response-headers": { - "status": 200, - "content-type": "text/html; charset=utf-8" - } -} diff --git a/zulip_bots/zulip_bots/bots/google_search/google_search.py b/zulip_bots/zulip_bots/bots/google_search/google_search.py deleted file mode 100644 index 4b45a8946..000000000 --- a/zulip_bots/zulip_bots/bots/google_search/google_search.py +++ /dev/null @@ -1,92 +0,0 @@ -# See readme.md for instructions on running this code. -import logging -from typing import Dict, List - -import requests -from bs4 import BeautifulSoup, Tag - -from zulip_bots.lib import AbstractBotHandler - - -def google_search(keywords: str) -> List[Dict[str, str]]: - query = {"q": keywords} - # Gets the page - page = requests.get("http://www.google.com/search", params=query) - # Parses the page into BeautifulSoup - soup = BeautifulSoup(page.text, "lxml") - - # Gets all search URLs - search = soup.find(id="search") - assert isinstance(search, Tag) - anchors = search.find_all("a") - results = [] - - for a in anchors: - try: - # Tries to get the href property of the URL - link = a["href"] - except KeyError: - continue - # Link must start with '/url?', as these are the search result links - if not link.startswith("/url?"): - continue - # Makes sure a hidden 'cached' result isn't displayed - if a.text.strip() == "Cached" and "webcache.googleusercontent.com" in a["href"]: - continue - # a.text: The name of the page - result = {"url": f"https://www.google.com{link}", "name": a.text} - results.append(result) - return results - - -def get_google_result(search_keywords: str) -> str: - help_message = "To use this bot, start messages with @mentioned-bot, \ - followed by what you want to search for. If \ - found, Zulip will return the first search result \ - on Google.\ - \ - An example message that could be sent is:\ - '@mentioned-bot zulip' or \ - '@mentioned-bot how to create a chatbot'." - - search_keywords = search_keywords.strip() - - if search_keywords in ("help", ""): - return help_message - else: - try: - results = google_search(search_keywords) - if len(results) == 0: - return "Found no results." - return "Found Result: [{}]({})".format(results[0]["name"], results[0]["url"]) - except Exception as e: - logging.exception("Error fetching Google results") - return f"Error: Search failed. {e}." - - -class GoogleSearchHandler: - """ - This plugin allows users to enter a search - term in Zulip and get the top URL sent back - to the context (stream or private) in which - it was called. It looks for messages starting - with @mentioned-bot. - """ - - def usage(self) -> str: - return """ - This plugin will allow users to search - for a given search term on Google from - Zulip. Use '@mentioned-bot help' to get - more information on the bot usage. Users - should preface messages with - @mentioned-bot. - """ - - def handle_message(self, message: Dict[str, str], bot_handler: AbstractBotHandler) -> None: - original_content = message["content"] - result = get_google_result(original_content) - bot_handler.send_reply(message, result) - - -handler_class = GoogleSearchHandler diff --git a/zulip_bots/zulip_bots/bots/google_search/logo.png b/zulip_bots/zulip_bots/bots/google_search/logo.png deleted file mode 100644 index f39297ab3..000000000 Binary files a/zulip_bots/zulip_bots/bots/google_search/logo.png and /dev/null differ diff --git a/zulip_bots/zulip_bots/bots/google_search/test_google_search.py b/zulip_bots/zulip_bots/bots/google_search/test_google_search.py deleted file mode 100644 index 950188bcb..000000000 --- a/zulip_bots/zulip_bots/bots/google_search/test_google_search.py +++ /dev/null @@ -1,46 +0,0 @@ -from unittest.mock import patch - -from zulip_bots.test_lib import BotTestCase, DefaultTests - - -class TestGoogleSearchBot(BotTestCase, DefaultTests): - bot_name = "google_search" - - # Simple query - def test_normal(self) -> None: - with self.mock_http_conversation("test_normal"): - self.verify_reply( - "zulip", - "Found Result: [Zulip](https://www.google.com/url?url=https%3A%2F%2Fzulip.com%2F)", - ) - - def test_bot_help(self) -> None: - help_message = "To use this bot, start messages with @mentioned-bot, \ - followed by what you want to search for. If \ - found, Zulip will return the first search result \ - on Google.\ - \ - An example message that could be sent is:\ - '@mentioned-bot zulip' or \ - '@mentioned-bot how to create a chatbot'." - self.verify_reply("", help_message) - self.verify_reply("help", help_message) - - def test_bot_no_results(self) -> None: - with self.mock_http_conversation("test_no_result"): - self.verify_reply("no res", "Found no results.") - - def test_attribute_error(self) -> None: - with self.mock_http_conversation("test_attribute_error"), patch("logging.exception"): - self.verify_reply("test", "Error: Search failed. .") - - # Makes sure cached results, irrelevant links, or empty results are not displayed - def test_ignore_links(self) -> None: - with self.mock_http_conversation("test_ignore_links"): - # The bot should ignore all links, apart from the zulip link at the end (googlesearch.py lines 23-38) - # Then it should send the zulip link - # See test_ignore_links.json - self.verify_reply( - "zulip", - "Found Result: [Zulip](https://www.google.com/url?url=https%3A%2F%2Fzulip.com%2F)", - )