浏览代码

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 年前
父节点
当前提交
c59c76e6ee

+ 2
- 1
searx/engines/bing_images.py 查看文件

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

+ 2
- 1
searx/engines/flickr_noapi.py 查看文件

34
 time_range_support = True
34
 time_range_support = True
35
 time_range_dict = {'day': 60 * 60 * 24,
35
 time_range_dict = {'day': 60 * 60 * 24,
36
                    'week': 60 * 60 * 24 * 7,
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
 def build_flickr_url(user_id, photo_id):
41
 def build_flickr_url(user_id, photo_id):

+ 2
- 1
searx/engines/google.py 查看文件

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

+ 9
- 1
searx/engines/google_images.py 查看文件

10
  @parse       url, title, img_src
10
  @parse       url, title, img_src
11
 """
11
 """
12
 
12
 
13
+from datetime import date, timedelta
13
 from urllib import urlencode
14
 from urllib import urlencode
14
 from json import loads
15
 from json import loads
15
 from lxml import html
16
 from lxml import html
16
 
17
 
18
+
17
 # engine dependent config
19
 # engine dependent config
18
 categories = ['images']
20
 categories = ['images']
19
 paging = True
21
 paging = True
29
     '&yv=2'\
31
     '&yv=2'\
30
     '&{search_options}'
32
     '&{search_options}'
31
 time_range_attr = "qdr:{range}"
33
 time_range_attr = "qdr:{range}"
34
+time_range_custom_attr = "cdr:1,cd_min:{start},cd_max{end}"
32
 time_range_dict = {'day': 'd',
35
 time_range_dict = {'day': 'd',
33
                    'week': 'w',
36
                    'week': 'w',
34
                    'month': 'm'}
37
                    'month': 'm'}
36
 
39
 
37
 # do search-request
40
 # do search-request
38
 def request(query, params):
41
 def request(query, params):
39
-
40
     search_options = {
42
     search_options = {
41
         'ijn': params['pageno'] - 1,
43
         'ijn': params['pageno'] - 1,
42
         'start': (params['pageno'] - 1) * number_of_results
44
         'start': (params['pageno'] - 1) * number_of_results
44
 
46
 
45
     if params['time_range'] in time_range_dict:
47
     if params['time_range'] in time_range_dict:
46
         search_options['tbs'] = time_range_attr.format(range=time_range_dict[params['time_range']])
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
     if safesearch and params['safesearch']:
56
     if safesearch and params['safesearch']:
49
         search_options['safe'] = 'on'
57
         search_options['safe'] = 'on'

+ 2
- 1
searx/engines/google_news.py 查看文件

29
 time_range_attr = "qdr:{range}"
29
 time_range_attr = "qdr:{range}"
30
 time_range_dict = {'day': 'd',
30
 time_range_dict = {'day': 'd',
31
                    'week': 'w',
31
                    'week': 'w',
32
-                   'month': 'm'}
32
+                   'month': 'm',
33
+                   'year': 'y'}
33
 
34
 
34
 
35
 
35
 # do search-request
36
 # do search-request

+ 19
- 0
tests/unit/engines/test_youtube_noapi.py 查看文件

17
         self.assertIn(query, params['url'])
17
         self.assertIn(query, params['url'])
18
         self.assertIn('youtube.com', params['url'])
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
     def test_response(self):
39
     def test_response(self):
21
         self.assertRaises(AttributeError, youtube_noapi.response, None)
40
         self.assertRaises(AttributeError, youtube_noapi.response, None)
22
         self.assertRaises(AttributeError, youtube_noapi.response, [])
41
         self.assertRaises(AttributeError, youtube_noapi.response, [])