Browse Source

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

add year support to engines which support it
Adam Tauber 8 years ago
parent
commit
e58949b76f

+ 2
- 1
searx/engines/bing_images.py View File

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

+ 3
- 0
searx/engines/bing_news.py View File

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

+ 3
- 0
searx/engines/deviantart.py View File

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

+ 3
- 0
searx/engines/duckduckgo.py View File

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

+ 2
- 1
searx/engines/flickr_noapi.py View File

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

+ 2
- 1
searx/engines/google.py View File

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

+ 9
- 1
searx/engines/google_images.py View File

@@ -10,10 +10,12 @@
10 10
  @parse       url, title, img_src
11 11
 """
12 12
 
13
+from datetime import date, timedelta
13 14
 from urllib import urlencode
14 15
 from json import loads
15 16
 from lxml import html
16 17
 
18
+
17 19
 # engine dependent config
18 20
 categories = ['images']
19 21
 paging = True
@@ -29,6 +31,7 @@ search_url = 'https://www.google.com/search'\
29 31
     '&yv=2'\
30 32
     '&{search_options}'
31 33
 time_range_attr = "qdr:{range}"
34
+time_range_custom_attr = "cdr:1,cd_min:{start},cd_max{end}"
32 35
 time_range_dict = {'day': 'd',
33 36
                    'week': 'w',
34 37
                    'month': 'm'}
@@ -36,7 +39,6 @@ time_range_dict = {'day': 'd',
36 39
 
37 40
 # do search-request
38 41
 def request(query, params):
39
-
40 42
     search_options = {
41 43
         'ijn': params['pageno'] - 1,
42 44
         'start': (params['pageno'] - 1) * number_of_results
@@ -44,6 +46,12 @@ def request(query, params):
44 46
 
45 47
     if params['time_range'] in time_range_dict:
46 48
         search_options['tbs'] = time_range_attr.format(range=time_range_dict[params['time_range']])
49
+    elif params['time_range'] == 'year':
50
+        now = date.today()
51
+        then = now - timedelta(days=365)
52
+        start = then.strftime('%m/%d/%Y')
53
+        end = now.strftime('%m/%d/%Y')
54
+        search_options['tbs'] = time_range_custom_attr.format(start=start, end=end)
47 55
 
48 56
     if safesearch and params['safesearch']:
49 57
         search_options['safe'] = 'on'

+ 2
- 1
searx/engines/google_news.py View File

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

+ 3
- 0
searx/engines/yahoo.py View File

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

+ 2
- 1
searx/engines/youtube_noapi.py View File

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

+ 3
- 0
searx/templates/oscar/time-range.html View File

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

+ 7
- 0
tests/unit/engines/test_bing_news.py View File

@@ -23,6 +23,13 @@ class TestBingNewsEngine(SearxTestCase):
23 23
         params = bing_news.request(query, dicto)
24 24
         self.assertIn('en', params['url'])
25 25
 
26
+    def test_no_url_in_request_year_time_range(self):
27
+        dicto = defaultdict(dict)
28
+        query = 'test_query'
29
+        dicto['time_range'] = 'year'
30
+        params = bing_news.request(query, dicto)
31
+        self.assertEqual({}, params['url'])
32
+
26 33
     def test_response(self):
27 34
         self.assertRaises(AttributeError, bing_news.response, None)
28 35
         self.assertRaises(AttributeError, bing_news.response, [])

+ 8
- 1
tests/unit/engines/test_deviantart.py View File

@@ -7,8 +7,8 @@ from searx.testing import SearxTestCase
7 7
 class TestDeviantartEngine(SearxTestCase):
8 8
 
9 9
     def test_request(self):
10
-        query = 'test_query'
11 10
         dicto = defaultdict(dict)
11
+        query = 'test_query'
12 12
         dicto['pageno'] = 0
13 13
         dicto['time_range'] = ''
14 14
         params = deviantart.request(query, dicto)
@@ -16,6 +16,13 @@ class TestDeviantartEngine(SearxTestCase):
16 16
         self.assertTrue(query in params['url'])
17 17
         self.assertTrue('deviantart.com' in params['url'])
18 18
 
19
+    def test_no_url_in_request_year_time_range(self):
20
+        dicto = defaultdict(dict)
21
+        query = 'test_query'
22
+        dicto['time_range'] = 'year'
23
+        params = deviantart.request(query, dicto)
24
+        self.assertEqual({}, params['url'])
25
+
19 26
     def test_response(self):
20 27
         self.assertRaises(AttributeError, deviantart.response, None)
21 28
         self.assertRaises(AttributeError, deviantart.response, [])

+ 7
- 0
tests/unit/engines/test_duckduckgo.py View File

@@ -19,6 +19,13 @@ class TestDuckduckgoEngine(SearxTestCase):
19 19
         self.assertIn('duckduckgo.com', params['url'])
20 20
         self.assertIn('ch-de', params['url'])
21 21
 
22
+    def test_no_url_in_request_year_time_range(self):
23
+        dicto = defaultdict(dict)
24
+        query = 'test_query'
25
+        dicto['time_range'] = 'year'
26
+        params = duckduckgo.request(query, dicto)
27
+        self.assertEqual({}, params['url'])
28
+
22 29
     def test_response(self):
23 30
         self.assertRaises(AttributeError, duckduckgo.response, None)
24 31
         self.assertRaises(AttributeError, duckduckgo.response, [])

+ 7
- 0
tests/unit/engines/test_yahoo.py View File

@@ -46,6 +46,13 @@ class TestYahooEngine(SearxTestCase):
46 46
         self.assertIn('en', params['cookies']['sB'])
47 47
         self.assertIn('en', params['url'])
48 48
 
49
+    def test_no_url_in_request_year_time_range(self):
50
+        dicto = defaultdict(dict)
51
+        query = 'test_query'
52
+        dicto['time_range'] = 'year'
53
+        params = yahoo.request(query, dicto)
54
+        self.assertEqual({}, params['url'])
55
+
49 56
     def test_response(self):
50 57
         self.assertRaises(AttributeError, yahoo.response, None)
51 58
         self.assertRaises(AttributeError, yahoo.response, [])

+ 19
- 0
tests/unit/engines/test_youtube_noapi.py View File

@@ -17,6 +17,25 @@ class TestYoutubeNoAPIEngine(SearxTestCase):
17 17
         self.assertIn(query, params['url'])
18 18
         self.assertIn('youtube.com', params['url'])
19 19
 
20
+    def test_time_range_search(self):
21
+        dicto = defaultdict(dict)
22
+        query = 'test_query'
23
+        dicto['time_range'] = 'year'
24
+        params = youtube_noapi.request(query, dicto)
25
+        self.assertIn('&sp=EgIIBQ%253D%253D', params['url'])
26
+
27
+        dicto['time_range'] = 'month'
28
+        params = youtube_noapi.request(query, dicto)
29
+        self.assertIn('&sp=EgIIBA%253D%253D', params['url'])
30
+
31
+        dicto['time_range'] = 'week'
32
+        params = youtube_noapi.request(query, dicto)
33
+        self.assertIn('&sp=EgIIAw%253D%253D', params['url'])
34
+
35
+        dicto['time_range'] = 'day'
36
+        params = youtube_noapi.request(query, dicto)
37
+        self.assertIn('&sp=EgIIAg%253D%253D', params['url'])
38
+
20 39
     def test_response(self):
21 40
         self.assertRaises(AttributeError, youtube_noapi.response, None)
22 41
         self.assertRaises(AttributeError, youtube_noapi.response, [])