From 3cea2b5f1c5986d5e1ff9dd7e1e91433e07f89f9 Mon Sep 17 00:00:00 2001 From: Tommy Ip Date: Sun, 8 Jan 2017 16:09:23 +0000 Subject: [PATCH] contrib_bots: Fix bare except clause. --- contrib_bots/lib/wikipedia.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/contrib_bots/lib/wikipedia.py b/contrib_bots/lib/wikipedia.py index e980408..419b43a 100644 --- a/contrib_bots/lib/wikipedia.py +++ b/contrib_bots/lib/wikipedia.py @@ -43,17 +43,18 @@ class WikipediaHandler(object): return is_wikipedia def handle_message(self, message, client, state_handler): - original_content = message['content'] + query = message['content'] for prefix in ['@wikipedia', '@wiki']: - if original_content.startswith(prefix): - original_content = original_content[len(prefix)+1:] + if query.startswith(prefix): + query = query[len(prefix)+1:] break - query_wiki_link = 'https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=' + query_wiki_link = ('https://en.wikipedia.org/w/api.php?action=query&' + 'list=search&srsearch=%s&format=json' % (query,)) try: - data = requests.get(query_wiki_link + original_content + '&format=json') - except: + data = requests.get(query_wiki_link) + except requests.exceptions.RequestException: logging.error('broken link') return @@ -63,7 +64,7 @@ class WikipediaHandler(object): search_string = data.json()['query']['search'][0]['title'].replace(' ', '_') url = 'https://wikipedia.org/wiki/' + search_string - new_content = 'For search term "' + original_content + new_content = 'For search term "' + query if len(data.json()['query']['search']) == 0: new_content = 'I am sorry. The search term you provided is not found :slightly_frowning_face:' else: