From bccbda49f23192ffa0eeab2c7e61235cb05129ac Mon Sep 17 00:00:00 2001 From: novokrest Date: Fri, 11 May 2018 08:09:22 +0300 Subject: [PATCH] wikipedia: Pass API query parameters as `params` dict in requests.get(). --- .../bots/wikipedia/fixtures/test_hash_query.json | 8 +++++++- .../wikipedia/fixtures/test_incorrect_query.json | 8 +++++++- .../bots/wikipedia/fixtures/test_multi_word.json | 8 +++++++- .../bots/wikipedia/fixtures/test_number_query.json | 8 +++++++- .../bots/wikipedia/fixtures/test_single_word.json | 8 +++++++- .../bots/wikipedia/fixtures/test_status_code.json | 8 +++++++- zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py | 12 ++++++++---- 7 files changed, 50 insertions(+), 10 deletions(-) diff --git a/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_hash_query.json b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_hash_query.json index 7441f4d..7219e6f 100644 --- a/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_hash_query.json +++ b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_hash_query.json @@ -1,6 +1,12 @@ { "request": { - "api_url":"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=%23&format=json" + "api_url":"https://en.wikipedia.org/w/api.php", + "params": { + "action": "query", + "list": "search", + "srsearch": "#", + "format": "json" + } }, "response": { "data": { diff --git a/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_incorrect_query.json b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_incorrect_query.json index b8e4cc7..4650db0 100644 --- a/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_incorrect_query.json +++ b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_incorrect_query.json @@ -1,6 +1,12 @@ { "request": { - "api_url":"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=sssssss%20kkkkk&format=json" + "api_url":"https://en.wikipedia.org/w/api.php", + "params": { + "action": "query", + "list": "search", + "srsearch": "sssssss kkkkk", + "format": "json" + } }, "response": { "data": { diff --git a/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_multi_word.json b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_multi_word.json index 51ea282..8d27356 100644 --- a/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_multi_word.json +++ b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_multi_word.json @@ -1,6 +1,12 @@ { "request": { - "api_url":"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=The%20sky%20is%20blue&format=json" + "api_url":"https://en.wikipedia.org/w/api.php", + "params": { + "action": "query", + "list": "search", + "srsearch": "The sky is blue", + "format": "json" + } }, "response": { "data": { diff --git a/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_number_query.json b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_number_query.json index 170b45f..7ff083e 100644 --- a/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_number_query.json +++ b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_number_query.json @@ -1,6 +1,12 @@ { "request": { - "api_url":"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=123&format=json" + "api_url":"https://en.wikipedia.org/w/api.php", + "params": { + "action": "query", + "list": "search", + "srsearch": "123", + "format": "json" + } }, "response": { "data": { diff --git a/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_single_word.json b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_single_word.json index 9b88417..8830c7d 100644 --- a/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_single_word.json +++ b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_single_word.json @@ -1,6 +1,12 @@ { "request": { - "api_url":"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=happy&format=json" + "api_url":"https://en.wikipedia.org/w/api.php", + "params": { + "action": "query", + "list": "search", + "srsearch": "happy", + "format": "json" + } }, "response": { "data": { diff --git a/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_status_code.json b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_status_code.json index b94e0fc..c718ffb 100644 --- a/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_status_code.json +++ b/zulip_bots/zulip_bots/bots/wikipedia/fixtures/test_status_code.json @@ -1,7 +1,13 @@ { "request": { - "api_url":"https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=Zulip&format=json" + "api_url":"https://en.wikipedia.org/w/api.php", + "params": { + "action": "query", + "list": "search", + "srsearch": "Zulip", + "format": "json" + } }, "response": { "data": { diff --git a/zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py b/zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py index 28a1f25..65013b1 100644 --- a/zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py +++ b/zulip_bots/zulip_bots/bots/wikipedia/wikipedia.py @@ -47,11 +47,15 @@ class WikipediaHandler(object): 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),)) + query_wiki_url = 'https://en.wikipedia.org/w/api.php' + query_wiki_params = dict( + action='query', + list='search', + srsearch=query, + format='json' + ) try: - data = requests.get(query_wiki_link) + data = requests.get(query_wiki_url, params=query_wiki_params) except requests.exceptions.RequestException: logging.error('broken link')