add _ to googletranslate and googlesearch
This commit is contained in:
parent
657c6d7b9c
commit
424a4bb631
21 changed files with 19 additions and 19 deletions
0
zulip_bots/zulip_bots/bots/google_search/__init__.py
Normal file
0
zulip_bots/zulip_bots/bots/google_search/__init__.py
Normal file
23
zulip_bots/zulip_bots/bots/google_search/doc.md
Normal file
23
zulip_bots/zulip_bots/bots/google_search/doc.md
Normal file
|
@ -0,0 +1,23 @@
|
|||
# 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://zulipchat.com/api/running-bots#running-a-bot).
|
||||
|
||||
Use this bot with the following command
|
||||
|
||||
`@mentioned-bot <search terms>`
|
||||
|
||||
This will return the first link found by Google for `<search terms>`
|
||||
and print the resulting URL.
|
||||
|
||||
If no `<search terms>` 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.
|
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"request": {
|
||||
"api_url": "http://www.google.com/search",
|
||||
"params": {
|
||||
"q": "test"
|
||||
}
|
||||
},
|
||||
"response": "<head></head><body><div id='foo'></div></body>",
|
||||
"response-headers": {
|
||||
"status": 200,
|
||||
"content-type": "text/html; charset=utf-8"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"request": {
|
||||
"api_url": "http://www.google.com/search",
|
||||
"params": {
|
||||
"q": "zulip"
|
||||
}
|
||||
},
|
||||
"response": "<head></head><body><div id='search'><a href='/url?url=webcache.googleusercontent.com/foo/bar'>Cached</a><a href='/?url'></a><a href='foo bar'></a><a></a><a href='/url?url=https%3A%2F%2Fzulipchat.com%2F'>Zulip</a></div></body>",
|
||||
"response-headers": {
|
||||
"status": 200,
|
||||
"content-type": "text/html; charset=utf-8"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"request": {
|
||||
"api_url": "http://www.google.com/search",
|
||||
"params": {
|
||||
"q": "no res"
|
||||
}
|
||||
},
|
||||
"response": "<head></head><body><div id='search'></div></body>",
|
||||
"response-headers": {
|
||||
"status": 200,
|
||||
"content-type": "text/html; charset=utf-8"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"request": {
|
||||
"api_url": "http://www.google.com/search",
|
||||
"params": {
|
||||
"q": "zulip"
|
||||
}
|
||||
},
|
||||
"response": "<head></head><body><div id='search'><a href='/url?url=https%3A%2F%2Fzulipchat.com%2F'>Zulip</a></div></body>",
|
||||
"response-headers": {
|
||||
"status": 200,
|
||||
"content-type": "text/html; charset=utf-8"
|
||||
}
|
||||
}
|
91
zulip_bots/zulip_bots/bots/google_search/google_search.py
Normal file
91
zulip_bots/zulip_bots/bots/google_search/google_search.py
Normal file
|
@ -0,0 +1,91 @@
|
|||
# See readme.md for instructions on running this code.
|
||||
from __future__ import print_function
|
||||
import logging
|
||||
from six.moves.urllib import parse
|
||||
|
||||
import requests
|
||||
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
from typing import Dict, Any, Union, List
|
||||
|
||||
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
|
||||
anchors = soup.find(id='search').findAll('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': "https://www.google.com{}".format(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 == 'help':
|
||||
return help_message
|
||||
elif search_keywords == '' or search_keywords is None:
|
||||
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(str(e))
|
||||
return 'Error: Search failed. {}.'.format(e)
|
||||
|
||||
class GoogleSearchHandler(object):
|
||||
'''
|
||||
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: Any) -> None:
|
||||
original_content = message['content']
|
||||
result = get_google_result(original_content)
|
||||
bot_handler.send_reply(message, result)
|
||||
|
||||
handler_class = GoogleSearchHandler
|
BIN
zulip_bots/zulip_bots/bots/google_search/logo.png
Normal file
BIN
zulip_bots/zulip_bots/bots/google_search/logo.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 17 KiB |
|
@ -0,0 +1,48 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
from zulip_bots.test_lib import StubBotTestCase
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
class TestGoogleSearchBot(StubBotTestCase):
|
||||
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%2Fzulipchat.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. \'NoneType\' object has no attribute \'findAll\'.')
|
||||
|
||||
# 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%2Fzulipchat.com%2F)'
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue