Browse Source

add year to time range to engines which support "Last year"

Engines:
 * Bing images
 * Flickr (noapi)
 * Google
 * Google Images
 * Google News
Noémi Ványi 8 years ago
parent
commit
c59c76e6ee

+ 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',

+ 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

+ 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, [])