浏览代码

Merge pull request #1279 from rinpatch/duckduckgo-currency

Switched currency converter to duckduckgo and added Bitcoin to currency list
Adam Tauber 6 年前
父节点
当前提交
5f3ada4721
没有帐户链接到提交者的电子邮件
共有 3 个文件被更改,包括 35 次插入15 次删除
  1. 14
    2
      searx/data/currencies.json
  2. 5
    9
      searx/engines/currency_convert.py
  3. 16
    4
      tests/unit/engines/test_currency_convert.py

+ 14
- 2
searx/data/currencies.json 查看文件

@@ -6163,9 +6163,21 @@
6163 6163
         ], 
6164 6164
         "scellino keniota": [
6165 6165
             "KES"
6166
-        ]
6166
+        ],
6167
+	"bitcoin": [
6168
+		"XBT"
6169
+	]	
6167 6170
     }, 
6168 6171
     "iso4217": {
6172
+	"XBT":{
6173
+	    "fr": "Bitcoin", 
6174
+            "en": "Bitcoin", 
6175
+            "nl": "Bitcoin", 
6176
+            "de": "Bitcoin", 
6177
+            "it": "Bitcoin", 
6178
+            "hu": "Bitcoin", 
6179
+            "es": "Bitcoin"
6180
+	},	
6169 6181
         "DZD": {
6170 6182
             "fr": "Dinar alg\u00e9rien", 
6171 6183
             "en": "Algerian dinar", 
@@ -7652,4 +7664,4 @@
7652 7664
             "es": "Libra sudanesa"
7653 7665
         }
7654 7666
     }
7655
-}
7667
+}

+ 5
- 9
searx/engines/currency_convert.py 查看文件

@@ -11,7 +11,7 @@ if sys.version_info[0] == 3:
11 11
     unicode = str
12 12
 
13 13
 categories = []
14
-url = 'https://finance.google.com/finance/converter?a=1&from={0}&to={1}'
14
+url = 'https://duckduckgo.com/js/spice/currency/1/{0}/{1}'
15 15
 weight = 100
16 16
 
17 17
 parser_re = re.compile(b'.*?(\\d+(?:\\.\\d+)?) ([^.0-9]+) (?:in|to) ([^.0-9]+)', re.I)
@@ -44,7 +44,6 @@ def request(query, params):
44 44
     if not m:
45 45
         # wrong query
46 46
         return params
47
-
48 47
     amount, from_currency, to_currency = m.groups()
49 48
     amount = float(amount)
50 49
     from_currency = name_to_iso4217(from_currency.strip())
@@ -63,16 +62,13 @@ def request(query, params):
63 62
 
64 63
 
65 64
 def response(resp):
65
+    """remove first and last lines to get only json"""
66
+    json_resp = resp.text[resp.text.find('\n') + 1:resp.text.rfind('\n') - 2]
66 67
     results = []
67
-    pat = '<span class=bld>(.+) {0}</span>'.format(
68
-        resp.search_params['to'].upper())
69
-
70 68
     try:
71
-        conversion_rate = re.findall(pat, resp.text)[0]
72
-        conversion_rate = float(conversion_rate)
69
+        conversion_rate = float(json.loads(json_resp)['conversion']['converted-amount'])
73 70
     except:
74 71
         return results
75
-
76 72
     answer = '{0} {1} = {2} {3}, 1 {1} ({5}) = {4} {3} ({6})'.format(
77 73
         resp.search_params['amount'],
78 74
         resp.search_params['from'],
@@ -83,7 +79,7 @@ def response(resp):
83 79
         resp.search_params['to_name'],
84 80
     )
85 81
 
86
-    url = 'https://finance.google.com/finance?q={0}{1}'.format(
82
+    url = 'https://duckduckgo.com/js/spice/currency/1/{0}/{1}'.format(
87 83
         resp.search_params['from'].upper(), resp.search_params['to'])
88 84
 
89 85
     results.append({'answer': answer, 'url': url})

+ 16
- 4
tests/unit/engines/test_currency_convert.py 查看文件

@@ -17,7 +17,7 @@ class TestCurrencyConvertEngine(SearxTestCase):
17 17
         query = b'convert 10 Pound Sterlings to United States Dollars'
18 18
         params = currency_convert.request(query, dicto)
19 19
         self.assertIn('url', params)
20
-        self.assertIn('finance.google.com', params['url'])
20
+        self.assertIn('duckduckgo.com', params['url'])
21 21
         self.assertIn('GBP', params['url'])
22 22
         self.assertIn('USD', params['url'])
23 23
 
@@ -30,8 +30,20 @@ class TestCurrencyConvertEngine(SearxTestCase):
30 30
         dicto['to_name'] = "United States dollar"
31 31
         response = mock.Mock(text='a,b,c,d', search_params=dicto)
32 32
         self.assertEqual(currency_convert.response(response), [])
33
-
34
-        body = "<span class=bld>0.5 {}</span>".format(dicto['to'])
33
+        body = """ddg_spice_currency(
34
+            {
35
+                "conversion":{
36
+                    "converted-amount": "0.5"
37
+                },
38
+                "topConversions":[
39
+                    {
40
+                    },
41
+                    {
42
+                    }
43
+                ]
44
+            }
45
+        );
46
+        """
35 47
         response = mock.Mock(text=body, search_params=dicto)
36 48
         results = currency_convert.response(response)
37 49
         self.assertEqual(type(results), list)
@@ -39,6 +51,6 @@ class TestCurrencyConvertEngine(SearxTestCase):
39 51
         self.assertEqual(results[0]['answer'], '10.0 GBP = 5.0 USD, 1 GBP (pound sterling)' +
40 52
                          ' = 0.5 USD (United States dollar)')
41 53
 
42
-        target_url = 'https://finance.google.com/finance?q={}{}'.format(
54
+        target_url = 'https://duckduckgo.com/js/spice/currency/1/{}/{}'.format(
43 55
             dicto['from'], dicto['to'])
44 56
         self.assertEqual(results[0]['url'], target_url)