Browse Source

Merge ec8223e22c244b7587a6ab7e3fcb9e73e8aed132 into 49b6206d6cbfdc19e2bb2c42e999fd7caa776a09

Alexandre Flament 6 years ago
parent
commit
db0674c696
No account linked to committer's email

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

14
 url = 'https://finance.google.com/finance/converter?a=1&from={0}&to={1}'
14
 url = 'https://finance.google.com/finance/converter?a=1&from={0}&to={1}'
15
 weight = 100
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
 db = 1
19
 db = 1
20
 
20
 
35
 
35
 
36
 def iso4217_to_name(iso4217, language):
36
 def iso4217_to_name(iso4217, language):
37
     global db
37
     global db
38
-
39
     return db['iso4217'].get(iso4217, {}).get(language, iso4217)
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
     m = parser_re.match(query)
42
     m = parser_re.match(query)
44
     if not m:
43
     if not m:
45
         # wrong query
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
     amount, from_currency, to_currency = m.groups()
54
     amount, from_currency, to_currency = m.groups()
49
     amount = float(amount)
55
     amount = float(amount)
97
     current_dir = os.path.dirname(os.path.realpath(__file__))
103
     current_dir = os.path.dirname(os.path.realpath(__file__))
98
     json_data = open(current_dir + "/../data/currencies.json", 'r', encoding='utf-8').read()
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
 load()
109
 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

401
             request_params['safesearch'] = search_query.safesearch
401
             request_params['safesearch'] = search_query.safesearch
402
             request_params['time_range'] = search_query.time_range
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
             # append request to list
409
             # append request to list
405
             requests.append((selected_engine['name'], search_query.query, request_params))
410
             requests.append((selected_engine['name'], search_query.query, request_params))
406
 
411
 

+ 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.google.com', params['url'])
20
         self.assertIn('finance.google.com', params['url'])