Merge pull request #783 from kvch/time-range-search-year

add year support to engines which support it
This commit is contained in:
Adam Tauber 2016-12-12 10:06:43 +01:00 committed by GitHub
commit e58949b76f
16 changed files with 82 additions and 7 deletions

View File

@ -33,7 +33,8 @@ time_range_string = '&qft=+filterui:age-lt{interval}'
thumb_url = "https://www.bing.com/th?id={ihk}" thumb_url = "https://www.bing.com/th?id={ihk}"
time_range_dict = {'day': '1440', time_range_dict = {'day': '1440',
'week': '10080', 'week': '10080',
'month': '43200'} 'month': '43200',
'year': '525600'}
# safesearch definitions # safesearch definitions
safesearch_types = {2: 'STRICT', safesearch_types = {2: 'STRICT',

View File

@ -66,6 +66,9 @@ def _get_url(query, language, offset, time_range):
# do search-request # do search-request
def request(query, params): def request(query, params):
if params['time_range'] and params['time_range'] not in time_range_dict:
return params
offset = (params['pageno'] - 1) * 10 + 1 offset = (params['pageno'] - 1) * 10 + 1
if params['language'] == 'all': if params['language'] == 'all':

View File

@ -34,6 +34,9 @@ time_range_dict = {'day': 11,
# do search-request # do search-request
def request(query, params): def request(query, params):
if params['time_range'] and params['time_range'] not in time_range_dict:
return params
offset = (params['pageno'] - 1) * 24 offset = (params['pageno'] - 1) * 24
params['url'] = search_url.format(offset=offset, params['url'] = search_url.format(offset=offset,

View File

@ -41,6 +41,9 @@ content_xpath = './/a[@class="result__snippet"]'
# do search-request # do search-request
def request(query, params): def request(query, params):
if params['time_range'] and params['time_range'] not in time_range_dict:
return params
offset = (params['pageno'] - 1) * 30 offset = (params['pageno'] - 1) * 30
if params['language'] == 'all': if params['language'] == 'all':

View File

@ -34,7 +34,8 @@ paging = True
time_range_support = True time_range_support = True
time_range_dict = {'day': 60 * 60 * 24, time_range_dict = {'day': 60 * 60 * 24,
'week': 60 * 60 * 24 * 7, 'week': 60 * 60 * 24 * 7,
'month': 60 * 60 * 24 * 7 * 4} 'month': 60 * 60 * 24 * 7 * 4,
'year': 60 * 60 * 24 * 7 * 52}
def build_flickr_url(user_id, photo_id): def build_flickr_url(user_id, photo_id):

View File

@ -95,7 +95,8 @@ search_url = ('https://{hostname}' +
time_range_search = "&tbs=qdr:{range}" time_range_search = "&tbs=qdr:{range}"
time_range_dict = {'day': 'd', time_range_dict = {'day': 'd',
'week': 'w', 'week': 'w',
'month': 'm'} 'month': 'm',
'year': 'y'}
# other URLs # other URLs
map_hostname_start = 'maps.google.' map_hostname_start = 'maps.google.'

View File

@ -10,10 +10,12 @@
@parse url, title, img_src @parse url, title, img_src
""" """
from datetime import date, timedelta
from urllib import urlencode from urllib import urlencode
from json import loads from json import loads
from lxml import html from lxml import html
# engine dependent config # engine dependent config
categories = ['images'] categories = ['images']
paging = True paging = True
@ -29,6 +31,7 @@ search_url = 'https://www.google.com/search'\
'&yv=2'\ '&yv=2'\
'&{search_options}' '&{search_options}'
time_range_attr = "qdr:{range}" time_range_attr = "qdr:{range}"
time_range_custom_attr = "cdr:1,cd_min:{start},cd_max{end}"
time_range_dict = {'day': 'd', time_range_dict = {'day': 'd',
'week': 'w', 'week': 'w',
'month': 'm'} 'month': 'm'}
@ -36,7 +39,6 @@ time_range_dict = {'day': 'd',
# do search-request # do search-request
def request(query, params): def request(query, params):
search_options = { search_options = {
'ijn': params['pageno'] - 1, 'ijn': params['pageno'] - 1,
'start': (params['pageno'] - 1) * number_of_results 'start': (params['pageno'] - 1) * number_of_results
@ -44,6 +46,12 @@ def request(query, params):
if params['time_range'] in time_range_dict: if params['time_range'] in time_range_dict:
search_options['tbs'] = time_range_attr.format(range=time_range_dict[params['time_range']]) search_options['tbs'] = time_range_attr.format(range=time_range_dict[params['time_range']])
elif params['time_range'] == 'year':
now = date.today()
then = now - timedelta(days=365)
start = then.strftime('%m/%d/%Y')
end = now.strftime('%m/%d/%Y')
search_options['tbs'] = time_range_custom_attr.format(start=start, end=end)
if safesearch and params['safesearch']: if safesearch and params['safesearch']:
search_options['safe'] = 'on' search_options['safe'] = 'on'

View File

@ -29,7 +29,8 @@ search_url = 'https://www.google.com/search'\
time_range_attr = "qdr:{range}" time_range_attr = "qdr:{range}"
time_range_dict = {'day': 'd', time_range_dict = {'day': 'd',
'week': 'w', 'week': 'w',
'month': 'm'} 'month': 'm',
'year': 'y'}
# do search-request # do search-request

View File

@ -77,6 +77,9 @@ def _get_language(params):
# do search-request # do search-request
def request(query, params): def request(query, params):
if params['time_range'] and params['time_range'] not in time_range_dict:
return params
offset = (params['pageno'] - 1) * 10 + 1 offset = (params['pageno'] - 1) * 10 + 1
language = _get_language(params) language = _get_language(params)

View File

@ -25,7 +25,8 @@ search_url = base_url + '?search_query={query}&page={page}'
time_range_url = '&sp=EgII{time_range}%253D%253D' time_range_url = '&sp=EgII{time_range}%253D%253D'
time_range_dict = {'day': 'Ag', time_range_dict = {'day': 'Ag',
'week': 'Aw', 'week': 'Aw',
'month': 'BA'} 'month': 'BA',
'year': 'BQ'}
embedded_url = '<iframe width="540" height="304" ' +\ embedded_url = '<iframe width="540" height="304" ' +\
'data-src="//www.youtube-nocookie.com/embed/{videoid}" ' +\ 'data-src="//www.youtube-nocookie.com/embed/{videoid}" ' +\

View File

@ -11,4 +11,7 @@
<option id="time-range-month" value="month" {{ "selected" if time_range=="month" else ""}}> <option id="time-range-month" value="month" {{ "selected" if time_range=="month" else ""}}>
{{ _('Last month') }} {{ _('Last month') }}
</option> </option>
<option id="time-range-year" value="year" {{ "selected" if time_range=="year" else ""}}>
{{ _('Last year') }}
</option>
</select> </select>

View File

@ -23,6 +23,13 @@ class TestBingNewsEngine(SearxTestCase):
params = bing_news.request(query, dicto) params = bing_news.request(query, dicto)
self.assertIn('en', params['url']) self.assertIn('en', params['url'])
def test_no_url_in_request_year_time_range(self):
dicto = defaultdict(dict)
query = 'test_query'
dicto['time_range'] = 'year'
params = bing_news.request(query, dicto)
self.assertEqual({}, params['url'])
def test_response(self): def test_response(self):
self.assertRaises(AttributeError, bing_news.response, None) self.assertRaises(AttributeError, bing_news.response, None)
self.assertRaises(AttributeError, bing_news.response, []) self.assertRaises(AttributeError, bing_news.response, [])

View File

@ -7,8 +7,8 @@ from searx.testing import SearxTestCase
class TestDeviantartEngine(SearxTestCase): class TestDeviantartEngine(SearxTestCase):
def test_request(self): def test_request(self):
query = 'test_query'
dicto = defaultdict(dict) dicto = defaultdict(dict)
query = 'test_query'
dicto['pageno'] = 0 dicto['pageno'] = 0
dicto['time_range'] = '' dicto['time_range'] = ''
params = deviantart.request(query, dicto) params = deviantart.request(query, dicto)
@ -16,6 +16,13 @@ class TestDeviantartEngine(SearxTestCase):
self.assertTrue(query in params['url']) self.assertTrue(query in params['url'])
self.assertTrue('deviantart.com' in params['url']) self.assertTrue('deviantart.com' in params['url'])
def test_no_url_in_request_year_time_range(self):
dicto = defaultdict(dict)
query = 'test_query'
dicto['time_range'] = 'year'
params = deviantart.request(query, dicto)
self.assertEqual({}, params['url'])
def test_response(self): def test_response(self):
self.assertRaises(AttributeError, deviantart.response, None) self.assertRaises(AttributeError, deviantart.response, None)
self.assertRaises(AttributeError, deviantart.response, []) self.assertRaises(AttributeError, deviantart.response, [])

View File

@ -19,6 +19,13 @@ class TestDuckduckgoEngine(SearxTestCase):
self.assertIn('duckduckgo.com', params['url']) self.assertIn('duckduckgo.com', params['url'])
self.assertIn('ch-de', params['url']) self.assertIn('ch-de', params['url'])
def test_no_url_in_request_year_time_range(self):
dicto = defaultdict(dict)
query = 'test_query'
dicto['time_range'] = 'year'
params = duckduckgo.request(query, dicto)
self.assertEqual({}, params['url'])
def test_response(self): def test_response(self):
self.assertRaises(AttributeError, duckduckgo.response, None) self.assertRaises(AttributeError, duckduckgo.response, None)
self.assertRaises(AttributeError, duckduckgo.response, []) self.assertRaises(AttributeError, duckduckgo.response, [])

View File

@ -46,6 +46,13 @@ class TestYahooEngine(SearxTestCase):
self.assertIn('en', params['cookies']['sB']) self.assertIn('en', params['cookies']['sB'])
self.assertIn('en', params['url']) self.assertIn('en', params['url'])
def test_no_url_in_request_year_time_range(self):
dicto = defaultdict(dict)
query = 'test_query'
dicto['time_range'] = 'year'
params = yahoo.request(query, dicto)
self.assertEqual({}, params['url'])
def test_response(self): def test_response(self):
self.assertRaises(AttributeError, yahoo.response, None) self.assertRaises(AttributeError, yahoo.response, None)
self.assertRaises(AttributeError, yahoo.response, []) self.assertRaises(AttributeError, yahoo.response, [])

View File

@ -17,6 +17,25 @@ class TestYoutubeNoAPIEngine(SearxTestCase):
self.assertIn(query, params['url']) self.assertIn(query, params['url'])
self.assertIn('youtube.com', params['url']) self.assertIn('youtube.com', params['url'])
def test_time_range_search(self):
dicto = defaultdict(dict)
query = 'test_query'
dicto['time_range'] = 'year'
params = youtube_noapi.request(query, dicto)
self.assertIn('&sp=EgIIBQ%253D%253D', params['url'])
dicto['time_range'] = 'month'
params = youtube_noapi.request(query, dicto)
self.assertIn('&sp=EgIIBA%253D%253D', params['url'])
dicto['time_range'] = 'week'
params = youtube_noapi.request(query, dicto)
self.assertIn('&sp=EgIIAw%253D%253D', params['url'])
dicto['time_range'] = 'day'
params = youtube_noapi.request(query, dicto)
self.assertIn('&sp=EgIIAg%253D%253D', params['url'])
def test_response(self): def test_response(self):
self.assertRaises(AttributeError, youtube_noapi.response, None) self.assertRaises(AttributeError, youtube_noapi.response, None)
self.assertRaises(AttributeError, youtube_noapi.response, []) self.assertRaises(AttributeError, youtube_noapi.response, [])