Ver código fonte

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

add year support to engines which support it
Adam Tauber 8 anos atrás
pai
commit
e58949b76f

+ 2
- 1
searx/engines/bing_images.py Ver arquivo

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

+ 3
- 0
searx/engines/bing_news.py Ver arquivo

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

+ 3
- 0
searx/engines/deviantart.py Ver arquivo

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

+ 3
- 0
searx/engines/duckduckgo.py Ver arquivo

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

+ 2
- 1
searx/engines/flickr_noapi.py Ver arquivo

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 Ver arquivo

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 Ver arquivo

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 Ver arquivo

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

+ 3
- 0
searx/engines/yahoo.py Ver arquivo

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

+ 2
- 1
searx/engines/youtube_noapi.py Ver arquivo

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

+ 3
- 0
searx/templates/oscar/time-range.html Ver arquivo

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

+ 7
- 0
tests/unit/engines/test_bing_news.py Ver arquivo

23
         params = bing_news.request(query, dicto)
23
         params = bing_news.request(query, dicto)
24
         self.assertIn('en', params['url'])
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
     def test_response(self):
33
     def test_response(self):
27
         self.assertRaises(AttributeError, bing_news.response, None)
34
         self.assertRaises(AttributeError, bing_news.response, None)
28
         self.assertRaises(AttributeError, bing_news.response, [])
35
         self.assertRaises(AttributeError, bing_news.response, [])

+ 8
- 1
tests/unit/engines/test_deviantart.py Ver arquivo

7
 class TestDeviantartEngine(SearxTestCase):
7
 class TestDeviantartEngine(SearxTestCase):
8
 
8
 
9
     def test_request(self):
9
     def test_request(self):
10
-        query = 'test_query'
11
         dicto = defaultdict(dict)
10
         dicto = defaultdict(dict)
11
+        query = 'test_query'
12
         dicto['pageno'] = 0
12
         dicto['pageno'] = 0
13
         dicto['time_range'] = ''
13
         dicto['time_range'] = ''
14
         params = deviantart.request(query, dicto)
14
         params = deviantart.request(query, dicto)
16
         self.assertTrue(query in params['url'])
16
         self.assertTrue(query in params['url'])
17
         self.assertTrue('deviantart.com' in params['url'])
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
     def test_response(self):
26
     def test_response(self):
20
         self.assertRaises(AttributeError, deviantart.response, None)
27
         self.assertRaises(AttributeError, deviantart.response, None)
21
         self.assertRaises(AttributeError, deviantart.response, [])
28
         self.assertRaises(AttributeError, deviantart.response, [])

+ 7
- 0
tests/unit/engines/test_duckduckgo.py Ver arquivo

19
         self.assertIn('duckduckgo.com', params['url'])
19
         self.assertIn('duckduckgo.com', params['url'])
20
         self.assertIn('ch-de', params['url'])
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
     def test_response(self):
29
     def test_response(self):
23
         self.assertRaises(AttributeError, duckduckgo.response, None)
30
         self.assertRaises(AttributeError, duckduckgo.response, None)
24
         self.assertRaises(AttributeError, duckduckgo.response, [])
31
         self.assertRaises(AttributeError, duckduckgo.response, [])

+ 7
- 0
tests/unit/engines/test_yahoo.py Ver arquivo

46
         self.assertIn('en', params['cookies']['sB'])
46
         self.assertIn('en', params['cookies']['sB'])
47
         self.assertIn('en', params['url'])
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
     def test_response(self):
56
     def test_response(self):
50
         self.assertRaises(AttributeError, yahoo.response, None)
57
         self.assertRaises(AttributeError, yahoo.response, None)
51
         self.assertRaises(AttributeError, yahoo.response, [])
58
         self.assertRaises(AttributeError, yahoo.response, [])

+ 19
- 0
tests/unit/engines/test_youtube_noapi.py Ver arquivo

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