From f70d405202ccec44849f8cd8d30ba37b254f2b69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9mi=20V=C3=A1nyi?= Date: Sun, 30 Oct 2016 18:14:42 +0100 Subject: [PATCH] add time range search for bing news --- searx/engines/bing_news.py | 24 +++++++++++++++++++----- tests/unit/engines/test_bing_news.py | 1 + 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/searx/engines/bing_news.py b/searx/engines/bing_news.py index a2397c48..906afe66 100644 --- a/searx/engines/bing_news.py +++ b/searx/engines/bing_news.py @@ -22,10 +22,15 @@ from searx.utils import list_get categories = ['news'] paging = True language_support = True +time_range_support = True # search-url base_url = 'https://www.bing.com/' search_string = 'news/search?{query}&first={offset}&format=RSS' +search_string_with_time = 'news/search?{query}&first={offset}&qft=interval%3d"{interval}"&format=RSS' +time_range_dict = {'day': '7', + 'week': '8', + 'month': '9'} # remove click @@ -46,6 +51,19 @@ def image_url_cleanup(url_string): return url_string +def _get_url(query, language, offset, time_range): + if time_range in time_range_dict: + search_path = search_string_with_time.format( + query=urlencode({'q': query, 'setmkt': language}), + offset=offset, + interval=time_range_dict[time_range]) + else: + search_path = search_string.format( + query=urlencode({'q': query, 'setmkt': language}), + offset=offset) + return base_url + search_path + + # do search-request def request(query, params): offset = (params['pageno'] - 1) * 10 + 1 @@ -55,11 +73,7 @@ def request(query, params): else: language = params['language'].replace('_', '-') - search_path = search_string.format( - query=urlencode({'q': query, 'setmkt': language}), - offset=offset) - - params['url'] = base_url + search_path + params['url'] = _get_url(query, language, offset, params['time_range']) return params diff --git a/tests/unit/engines/test_bing_news.py b/tests/unit/engines/test_bing_news.py index a64d59b7..a909defe 100644 --- a/tests/unit/engines/test_bing_news.py +++ b/tests/unit/engines/test_bing_news.py @@ -12,6 +12,7 @@ class TestBingNewsEngine(SearxTestCase): dicto = defaultdict(dict) dicto['pageno'] = 1 dicto['language'] = 'fr_FR' + dicto['time_range'] = '' params = bing_news.request(query, dicto) self.assertIn('url', params) self.assertIn(query, params['url'])