wikipedia bot: Return up to three links from wikipedia for keyword.
This change includes updates to the docs and tests as well.
This commit is contained in:
parent
2597de87ef
commit
f947ff44f8
|
@ -9,8 +9,15 @@
|
|||
"query": {
|
||||
"search": [
|
||||
{
|
||||
"title":"Sky blue"
|
||||
"title":"Sky_blue"
|
||||
},
|
||||
{
|
||||
"title":"Sky_Blue_Sky"
|
||||
},
|
||||
{
|
||||
"title":"Blue_Sky"
|
||||
}
|
||||
|
||||
]
|
||||
}
|
||||
},
|
||||
|
|
|
@ -10,6 +10,12 @@
|
|||
"search": [
|
||||
{
|
||||
"title":"123"
|
||||
},
|
||||
{
|
||||
"title":"Japan_Airlines_Flight_123"
|
||||
},
|
||||
{
|
||||
"title":"Iodine-123"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -10,6 +10,12 @@
|
|||
"search": [
|
||||
{
|
||||
"title":"Happiness"
|
||||
},
|
||||
{
|
||||
"title":"Happy!"
|
||||
},
|
||||
{
|
||||
"title":"Happy,_Happy"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -9,25 +9,39 @@ class TestWikipediaBot(StubBotTestCase):
|
|||
|
||||
# Single-word query
|
||||
bot_request = 'happy'
|
||||
bot_response = "For search term \"happy\", https://en.wikipedia.org/wiki/Happiness"
|
||||
bot_response = ('''For search term:happy
|
||||
1:[Happiness](https://en.wikipedia.org/wiki/Happiness)
|
||||
2:[Happy!](https://en.wikipedia.org/wiki/Happy!)
|
||||
3:[Happy,_Happy](https://en.wikipedia.org/wiki/Happy,_Happy)
|
||||
''')
|
||||
with self.mock_http_conversation('test_single_word'):
|
||||
self.verify_reply(bot_request, bot_response)
|
||||
|
||||
# Multi-word query
|
||||
bot_request = 'The sky is blue'
|
||||
bot_response = "For search term \"The sky is blue\", https://en.wikipedia.org/wiki/Sky_blue"
|
||||
bot_response = ('''For search term:The sky is blue
|
||||
1:[Sky_blue](https://en.wikipedia.org/wiki/Sky_blue)
|
||||
2:[Sky_Blue_Sky](https://en.wikipedia.org/wiki/Sky_Blue_Sky)
|
||||
3:[Blue_Sky](https://en.wikipedia.org/wiki/Blue_Sky)
|
||||
''')
|
||||
with self.mock_http_conversation('test_multi_word'):
|
||||
self.verify_reply(bot_request, bot_response)
|
||||
|
||||
# Number query
|
||||
bot_request = '123'
|
||||
bot_response = "For search term \"123\", https://en.wikipedia.org/wiki/123"
|
||||
bot_response = ('''For search term:123
|
||||
1:[123](https://en.wikipedia.org/wiki/123)
|
||||
2:[Japan_Airlines_Flight_123](https://en.wikipedia.org/wiki/Japan_Airlines_Flight_123)
|
||||
3:[Iodine-123](https://en.wikipedia.org/wiki/Iodine-123)
|
||||
''')
|
||||
with self.mock_http_conversation('test_number_query'):
|
||||
self.verify_reply(bot_request, bot_response)
|
||||
|
||||
# Hash query
|
||||
bot_request = '#'
|
||||
bot_response = "For search term \"#\", https://en.wikipedia.org/wiki/Number_sign"
|
||||
bot_response = '''For search term:#
|
||||
1:[Number_sign](https://en.wikipedia.org/wiki/Number_sign)
|
||||
'''
|
||||
with self.mock_http_conversation('test_hash_query'):
|
||||
self.verify_reply(bot_request, bot_response)
|
||||
|
||||
|
@ -39,5 +53,5 @@ class TestWikipediaBot(StubBotTestCase):
|
|||
|
||||
# Empty query, no request made to the Internet.
|
||||
bot_request = ''
|
||||
bot_response = "Please enter your message after @mention-bot"
|
||||
bot_response = "Please enter your search term after @mention-bot"
|
||||
self.verify_reply(bot_request, bot_response)
|
||||
|
|
|
@ -10,7 +10,7 @@ from six.moves import urllib
|
|||
class WikipediaHandler(object):
|
||||
'''
|
||||
This plugin facilitates searching Wikipedia for a
|
||||
specific key term and returns the top article from the
|
||||
specific key term and returns the top 3 articles from the
|
||||
search. It looks for messages starting with '@mention-bot'
|
||||
|
||||
In this example, we write all Wikipedia searches into
|
||||
|
@ -21,47 +21,55 @@ class WikipediaHandler(object):
|
|||
|
||||
META = {
|
||||
'name': 'Wikipedia',
|
||||
'description': 'Searches Wikipedia for a term and returns the top article.',
|
||||
'description': 'Searches Wikipedia for a term and returns the top 3 articles.',
|
||||
}
|
||||
|
||||
def usage(self):
|
||||
return '''
|
||||
This plugin will allow users to directly search
|
||||
Wikipedia for a specific key term and get the top
|
||||
article that is returned from the search. Users
|
||||
Wikipedia for a specific key term and get the top 3
|
||||
articles that is returned from the search. Users
|
||||
should preface searches with "@mention-bot".
|
||||
'''
|
||||
@mention-bot <name of article>'''
|
||||
|
||||
def handle_message(self, message, bot_handler):
|
||||
bot_response = self.get_bot_wiki_response(message, bot_handler)
|
||||
bot_handler.send_reply(message, bot_response)
|
||||
|
||||
def get_bot_wiki_response(self, message, bot_handler):
|
||||
help_text = 'Please enter your message after @mention-bot'
|
||||
'''This function returns the URLs of the requested topic.'''
|
||||
|
||||
help_text = 'Please enter your search term after @mention-bot'
|
||||
|
||||
# Checking if the link exists.
|
||||
query = message['content']
|
||||
if query == '':
|
||||
return help_text
|
||||
|
||||
query_wiki_link = ('https://en.wikipedia.org/w/api.php?action=query&'
|
||||
'list=search&srsearch=%s&format=json'
|
||||
% (urllib.parse.quote(query),))
|
||||
try:
|
||||
data = requests.get(query_wiki_link)
|
||||
|
||||
except requests.exceptions.RequestException:
|
||||
logging.error('broken link')
|
||||
return
|
||||
|
||||
# Checking if the bot accessed the link.
|
||||
if data.status_code != 200:
|
||||
logging.error('Page not found.')
|
||||
return
|
||||
new_content = 'For search term:' + query + '\n'
|
||||
|
||||
new_content = 'For search term "' + query
|
||||
# Checking if there is content for the searched term
|
||||
if len(data.json()['query']['search']) == 0:
|
||||
new_content = 'I am sorry. The search term you provided is not found :slightly_frowning_face:'
|
||||
else:
|
||||
search_string = data.json()['query']['search'][0]['title'].replace(' ', '_')
|
||||
for i in range(min(3, len(data.json()['query']['search']))):
|
||||
search_string = data.json()['query']['search'][i]['title'].replace(' ', '_')
|
||||
url = 'https://en.wikipedia.org/wiki/' + search_string
|
||||
new_content = new_content + '", ' + url
|
||||
new_content += str(i+1) + ':' + '[' + search_string + ']' + '(' + url.replace('"', "%22") + ')\n'
|
||||
return new_content
|
||||
|
||||
|
||||
handler_class = WikipediaHandler
|
||||
|
|
Loading…
Reference in a new issue