浏览代码

switched currency convert to duckduckgo and added Bitcoin to currency list

rinpatch 6 年前
父节点
当前提交
a305dae9d8
共有 2 个文件被更改,包括 19 次插入10 次删除
  1. 14
    2
      searx/data/currencies.json
  2. 5
    8
      searx/engines/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
- 8
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)
@@ -63,16 +63,13 @@ def request(query, params):
63 63
 
64 64
 
65 65
 def response(resp):
66
+    """remove first and last lines to get only json"""
67
+    json_resp = resp.text[resp.text.find('\n')+1:resp.text.rfind('\n')-2]
66 68
     results = []
67
-    pat = '<span class=bld>(.+) {0}</span>'.format(
68
-        resp.search_params['to'].upper())
69
-
70 69
     try:
71
-        conversion_rate = re.findall(pat, resp.text)[0]
72
-        conversion_rate = float(conversion_rate)
70
+        conversion_rate = float(json.loads(json_resp)['conversion']['converted-amount'])
73 71
     except:
74 72
         return results
75
-
76 73
     answer = '{0} {1} = {2} {3}, 1 {1} ({5}) = {4} {3} ({6})'.format(
77 74
         resp.search_params['amount'],
78 75
         resp.search_params['from'],
@@ -83,7 +80,7 @@ def response(resp):
83 80
         resp.search_params['to_name'],
84 81
     )
85 82
 
86
-    url = 'https://finance.google.com/finance?q={0}{1}'.format(
83
+    url = 'https://duckduckgo.com/js/spice/currency/1/{0}/{1}'.format(
87 84
         resp.search_params['from'].upper(), resp.search_params['to'])
88 85
 
89 86
     results.append({'answer': answer, 'url': url})