Selaa lähdekoodia

Merge ec8223e22c244b7587a6ab7e3fcb9e73e8aed132 into 49b6206d6cbfdc19e2bb2c42e999fd7caa776a09

Alexandre Flament 6 vuotta sitten
vanhempi
commit
db0674c696
No account linked to committer's email

+ 11
- 5
searx/engines/currency_convert.py Näytä tiedosto

@@ -14,7 +14,7 @@ categories = []
14 14
 url = 'https://finance.google.com/finance/converter?a=1&from={0}&to={1}'
15 15
 weight = 100
16 16
 
17
-parser_re = re.compile(b'.*?(\\d+(?:\\.\\d+)?) ([^.0-9]+) (?:in|to) ([^.0-9]+)', re.I)
17
+parser_re = re.compile(b'.*?(\\d+(?:\\.\\d+)?) ([^.0-9]+) (?:in|to|en) ([^.0-9]+)', re.I)
18 18
 
19 19
 db = 1
20 20
 
@@ -35,15 +35,21 @@ def name_to_iso4217(name):
35 35
 
36 36
 def iso4217_to_name(iso4217, language):
37 37
     global db
38
-
39 38
     return db['iso4217'].get(iso4217, {}).get(language, iso4217)
40 39
 
41 40
 
42
-def request(query, params):
41
+def is_accepted(query, params):
43 42
     m = parser_re.match(query)
44 43
     if not m:
45 44
         # wrong query
46
-        return params
45
+        return False
46
+
47
+    params['parsed_regex'] = m
48
+    return True
49
+
50
+
51
+def request(query, params):
52
+    m = params['parsed_regex']
47 53
 
48 54
     amount, from_currency, to_currency = m.groups()
49 55
     amount = float(amount)
@@ -97,7 +103,7 @@ def load():
97 103
     current_dir = os.path.dirname(os.path.realpath(__file__))
98 104
     json_data = open(current_dir + "/../data/currencies.json", 'r', encoding='utf-8').read()
99 105
 
100
-    db = json.loads(json_data)
106
+    db = json.loads(json_data, encoding="utf-8")
101 107
 
102 108
 
103 109
 load()

+ 29
- 8
searx/engines/dictzone.py Näytä tiedosto

@@ -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 Näytä tiedosto

@@ -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 Näytä tiedosto

@@ -401,6 +401,11 @@ class Search(object):
401 401
             request_params['safesearch'] = search_query.safesearch
402 402
             request_params['time_range'] = search_query.time_range
403 403
 
404
+            # check if the engine accepts the request
405
+            if hasattr(engine, 'is_accepted'):
406
+                if not engine.is_accepted(search_query.query, request_params):
407
+                    continue
408
+
404 409
             # append request to list
405 410
             requests.append((selected_engine['name'], search_query.query, request_params))
406 411
 

+ 2
- 2
tests/unit/engines/test_currency_convert.py Näytä tiedosto

@@ -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.google.com', params['url'])