Просмотр исходного кода

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

Alexandre Flament 7 лет назад
Родитель
Сommit
ec8223e22c

+ 11
- 5
searx/engines/currency_convert.py Просмотреть файл

@@ -13,7 +13,7 @@ categories = []
13 13
 url = 'https://download.finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s={query}=X'
14 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 18
 db = 1
19 19
 
@@ -34,15 +34,21 @@ def name_to_iso4217(name):
34 34
 
35 35
 def iso4217_to_name(iso4217, language):
36 36
     global db
37
-
38 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 41
     m = parser_re.match(query)
43 42
     if not m:
44 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 53
     ammount, from_currency, to_currency = m.groups()
48 54
     ammount = float(ammount)
@@ -99,7 +105,7 @@ def load():
99 105
     current_dir = os.path.dirname(os.path.realpath(__file__))
100 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 111
 load()

+ 29
- 8
searx/engines/dictzone.py Просмотреть файл

@@ -15,17 +15,20 @@ from searx.utils import is_valid_lang
15 15
 from searx.url_utils import urljoin
16 16
 
17 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 19
 weight = 100
20 20
 
21 21
 parser_re = re.compile(b'.*?([a-z]+)-([a-z]+) ([^ ]+)$', re.I)
22 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 27
     if not m:
28
-        return params
28
+        # wrong query
29
+        return False
30
+
31
+    params["parsed_regex"] = m
29 32
 
30 33
     from_lang, to_lang, query = m.groups()
31 34
 
@@ -33,11 +36,23 @@ def request(query, params):
33 36
     to_lang = is_valid_lang(to_lang)
34 37
 
35 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 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 57
     return params
43 58
 
@@ -45,6 +60,9 @@ def request(query, params):
45 60
 def response(resp):
46 61
     results = []
47 62
 
63
+    if resp.status_code != 200:
64
+        return results
65
+
48 66
     dom = html.fromstring(resp.text)
49 67
 
50 68
     for k, result in enumerate(dom.xpath(results_xpath)[1:]):
@@ -61,7 +79,10 @@ def response(resp):
61 79
 
62 80
         results.append({
63 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 86
             'content': '; '.join(to_results)
66 87
         })
67 88
 

+ 19
- 10
searx/engines/translated.py Просмотреть файл

@@ -24,10 +24,13 @@ parser_re = re.compile(u'.*?([a-z]+)-([a-z]+) (.{2,})$', re.I)
24 24
 api_key = ''
25 25
 
26 26
 
27
-def request(query, params):
27
+def is_accepted(query, params):
28 28
     m = parser_re.match(unicode(query, 'utf8'))
29 29
     if not m:
30
-        return params
30
+        # wrong query
31
+        return False
32
+
33
+    params["parsed_regex"] = m
31 34
 
32 35
     from_lang, to_lang, query = m.groups()
33 36
 
@@ -35,20 +38,26 @@ def request(query, params):
35 38
     to_lang = is_valid_lang(to_lang)
36 39
 
37 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 53
     if api_key:
41 54
         key_form = '&key=' + api_key
42 55
     else:
43 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 60
                                key=key_form)
48
-    params['query'] = query
49
-    params['from_lang'] = from_lang
50
-    params['to_lang'] = to_lang
51
-
52 61
     return params
53 62
 
54 63
 

+ 5
- 0
searx/search.py Просмотреть файл

@@ -399,6 +399,11 @@ class Search(object):
399 399
             request_params['safesearch'] = search_query.safesearch
400 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 407
             # append request to list
403 408
             requests.append((selected_engine['name'], search_query.query, request_params))
404 409
 

+ 2
- 2
tests/unit/engines/test_currency_convert.py Просмотреть файл

@@ -11,10 +11,10 @@ class TestCurrencyConvertEngine(SearxTestCase):
11 11
         query = b'test_query'
12 12
         dicto = defaultdict(dict)
13 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 16
         query = b'convert 10 Pound Sterlings to United States Dollars'
17
+        self.assertTrue(currency_convert.is_accepted(query, dicto))
18 18
         params = currency_convert.request(query, dicto)
19 19
         self.assertIn('url', params)
20 20
         self.assertIn('finance.yahoo.com', params['url'])