Browse Source

[mod] fix #903 : add a function is_accepted to engine. search.py calls this function creating a thread for the engine.

Alexandre Flament 7 years ago
parent
commit
ec8223e22c

+ 11
- 5
searx/engines/currency_convert.py View File

13
 url = 'https://download.finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s={query}=X'
13
 url = 'https://download.finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s={query}=X'
14
 weight = 100
14
 weight = 100
15
 
15
 
16
-parser_re = re.compile(b'.*?(\\d+(?:\\.\\d+)?) ([^.0-9]+) (?:in|to) ([^.0-9]+)', re.I)
16
+parser_re = re.compile(b'.*?(\\d+(?:\\.\\d+)?) ([^.0-9]+) (?:in|to|en) ([^.0-9]+)', re.I)
17
 
17
 
18
 db = 1
18
 db = 1
19
 
19
 
34
 
34
 
35
 def iso4217_to_name(iso4217, language):
35
 def iso4217_to_name(iso4217, language):
36
     global db
36
     global db
37
-
38
     return db['iso4217'].get(iso4217, {}).get(language, iso4217)
37
     return db['iso4217'].get(iso4217, {}).get(language, iso4217)
39
 
38
 
40
 
39
 
41
-def request(query, params):
40
+def is_accepted(query, params):
42
     m = parser_re.match(query)
41
     m = parser_re.match(query)
43
     if not m:
42
     if not m:
44
         # wrong query
43
         # wrong query
45
-        return params
44
+        return False
45
+
46
+    params['parsed_regex'] = m
47
+    return True
48
+
49
+
50
+def request(query, params):
51
+    m = params['parsed_regex']
46
 
52
 
47
     ammount, from_currency, to_currency = m.groups()
53
     ammount, from_currency, to_currency = m.groups()
48
     ammount = float(ammount)
54
     ammount = float(ammount)
99
     current_dir = os.path.dirname(os.path.realpath(__file__))
105
     current_dir = os.path.dirname(os.path.realpath(__file__))
100
     json_data = open(current_dir + "/../data/currencies.json").read()
106
     json_data = open(current_dir + "/../data/currencies.json").read()
101
 
107
 
102
-    db = json.loads(json_data)
108
+    db = json.loads(json_data, encoding="utf-8")
103
 
109
 
104
 
110
 
105
 load()
111
 load()

+ 29
- 8
searx/engines/dictzone.py View File

15
 from searx.url_utils import urljoin
15
 from searx.url_utils import urljoin
16
 
16
 
17
 categories = ['general']
17
 categories = ['general']
18
-url = u'http://dictzone.com/{from_lang}-{to_lang}-dictionary/{query}'
18
+url = u'https://dictzone.com/{from_lang}-{to_lang}-dictionary/{query}'
19
 weight = 100
19
 weight = 100
20
 
20
 
21
 parser_re = re.compile(b'.*?([a-z]+)-([a-z]+) ([^ ]+)$', re.I)
21
 parser_re = re.compile(b'.*?([a-z]+)-([a-z]+) ([^ ]+)$', re.I)
22
 results_xpath = './/table[@id="r"]/tr'
22
 results_xpath = './/table[@id="r"]/tr'
23
 
23
 
24
 
24
 
25
-def request(query, params):
26
-    m = parser_re.match(query)
25
+def is_accepted(query, params):
26
+    m = parser_re.match(unicode(query, 'utf8'))
27
     if not m:
27
     if not m:
28
-        return params
28
+        # wrong query
29
+        return False
30
+
31
+    params["parsed_regex"] = m
29
 
32
 
30
     from_lang, to_lang, query = m.groups()
33
     from_lang, to_lang, query = m.groups()
31
 
34
 
33
     to_lang = is_valid_lang(to_lang)
36
     to_lang = is_valid_lang(to_lang)
34
 
37
 
35
     if not from_lang or not to_lang:
38
     if not from_lang or not to_lang:
39
+        return False
40
+
41
+    params['from_lang'] = from_lang
42
+    params['to_lang'] = to_lang
43
+    params['query'] = query
44
+
45
+    return True
46
+
47
+
48
+def request(query, params):
49
+    m = params["parsed_regex"]
50
+    if not m:
36
         return params
51
         return params
37
 
52
 
38
-    params['url'] = url.format(from_lang=from_lang[2],
39
-                               to_lang=to_lang[2],
40
-                               query=query.decode('utf-8'))
53
+    params['url'] = url.format(from_lang=params['from_lang'][2],
54
+                               to_lang=params['to_lang'][2],
55
+                               query=params['query'])
41
 
56
 
42
     return params
57
     return params
43
 
58
 
45
 def response(resp):
60
 def response(resp):
46
     results = []
61
     results = []
47
 
62
 
63
+    if resp.status_code != 200:
64
+        return results
65
+
48
     dom = html.fromstring(resp.text)
66
     dom = html.fromstring(resp.text)
49
 
67
 
50
     for k, result in enumerate(dom.xpath(results_xpath)[1:]):
68
     for k, result in enumerate(dom.xpath(results_xpath)[1:]):
61
 
79
 
62
         results.append({
80
         results.append({
63
             'url': urljoin(resp.url, '?%d' % k),
81
             'url': urljoin(resp.url, '?%d' % k),
64
-            'title': from_result.text_content(),
82
+            'title': '[{0}-{1}] {2}'.format(
83
+                resp.search_params['from_lang'][1],
84
+                resp.search_params['to_lang'][1],
85
+                from_result.text_content()),
65
             'content': '; '.join(to_results)
86
             'content': '; '.join(to_results)
66
         })
87
         })
67
 
88
 

+ 19
- 10
searx/engines/translated.py View File

24
 api_key = ''
24
 api_key = ''
25
 
25
 
26
 
26
 
27
-def request(query, params):
27
+def is_accepted(query, params):
28
     m = parser_re.match(unicode(query, 'utf8'))
28
     m = parser_re.match(unicode(query, 'utf8'))
29
     if not m:
29
     if not m:
30
-        return params
30
+        # wrong query
31
+        return False
32
+
33
+    params["parsed_regex"] = m
31
 
34
 
32
     from_lang, to_lang, query = m.groups()
35
     from_lang, to_lang, query = m.groups()
33
 
36
 
35
     to_lang = is_valid_lang(to_lang)
38
     to_lang = is_valid_lang(to_lang)
36
 
39
 
37
     if not from_lang or not to_lang:
40
     if not from_lang or not to_lang:
38
-        return params
41
+        return False
42
+
43
+    params['from_lang'] = from_lang
44
+    params['to_lang'] = to_lang
45
+    params['query'] = query
46
+
47
+    return True
48
+
49
+
50
+def request(query, params):
51
+    m = params["parsed_regex"]
39
 
52
 
40
     if api_key:
53
     if api_key:
41
         key_form = '&key=' + api_key
54
         key_form = '&key=' + api_key
42
     else:
55
     else:
43
         key_form = ''
56
         key_form = ''
44
-    params['url'] = url.format(from_lang=from_lang[1],
45
-                               to_lang=to_lang[1],
46
-                               query=query,
57
+    params['url'] = url.format(from_lang=params['from_lang'][1],
58
+                               to_lang=params['to_lang'][1],
59
+                               query=params['query'],
47
                                key=key_form)
60
                                key=key_form)
48
-    params['query'] = query
49
-    params['from_lang'] = from_lang
50
-    params['to_lang'] = to_lang
51
-
52
     return params
61
     return params
53
 
62
 
54
 
63
 

+ 5
- 0
searx/search.py View File

399
             request_params['safesearch'] = search_query.safesearch
399
             request_params['safesearch'] = search_query.safesearch
400
             request_params['time_range'] = search_query.time_range
400
             request_params['time_range'] = search_query.time_range
401
 
401
 
402
+            # check if the engine accepts the request
403
+            if hasattr(engine, 'is_accepted'):
404
+                if not engine.is_accepted(search_query.query, request_params):
405
+                    continue
406
+
402
             # append request to list
407
             # append request to list
403
             requests.append((selected_engine['name'], search_query.query, request_params))
408
             requests.append((selected_engine['name'], search_query.query, request_params))
404
 
409
 

+ 2
- 2
tests/unit/engines/test_currency_convert.py View File

11
         query = b'test_query'
11
         query = b'test_query'
12
         dicto = defaultdict(dict)
12
         dicto = defaultdict(dict)
13
         dicto['pageno'] = 1
13
         dicto['pageno'] = 1
14
-        params = currency_convert.request(query, dicto)
15
-        self.assertNotIn('url', params)
14
+        self.assertFalse(currency_convert.is_accepted(query, dicto))
16
 
15
 
17
         query = b'convert 10 Pound Sterlings to United States Dollars'
16
         query = b'convert 10 Pound Sterlings to United States Dollars'
17
+        self.assertTrue(currency_convert.is_accepted(query, dicto))
18
         params = currency_convert.request(query, dicto)
18
         params = currency_convert.request(query, dicto)
19
         self.assertIn('url', params)
19
         self.assertIn('url', params)
20
         self.assertIn('finance.yahoo.com', params['url'])
20
         self.assertIn('finance.yahoo.com', params['url'])