Browse Source

switched currency convert to duckduckgo and added Bitcoin to currency list

rinpatch 6 years ago
parent
commit
a305dae9d8
2 changed files with 19 additions and 10 deletions
  1. 14
    2
      searx/data/currencies.json
  2. 5
    8
      searx/engines/currency_convert.py

+ 14
- 2
searx/data/currencies.json View File

6163
         ], 
6163
         ], 
6164
         "scellino keniota": [
6164
         "scellino keniota": [
6165
             "KES"
6165
             "KES"
6166
-        ]
6166
+        ],
6167
+	"bitcoin": [
6168
+		"XBT"
6169
+	]	
6167
     }, 
6170
     }, 
6168
     "iso4217": {
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
         "DZD": {
6181
         "DZD": {
6170
             "fr": "Dinar alg\u00e9rien", 
6182
             "fr": "Dinar alg\u00e9rien", 
6171
             "en": "Algerian dinar", 
6183
             "en": "Algerian dinar", 
7652
             "es": "Libra sudanesa"
7664
             "es": "Libra sudanesa"
7653
         }
7665
         }
7654
     }
7666
     }
7655
-}
7667
+}

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

11
     unicode = str
11
     unicode = str
12
 
12
 
13
 categories = []
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
 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) ([^.0-9]+)', re.I)
63
 
63
 
64
 
64
 
65
 def response(resp):
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
     results = []
68
     results = []
67
-    pat = '<span class=bld>(.+) {0}</span>'.format(
68
-        resp.search_params['to'].upper())
69
-
70
     try:
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
     except:
71
     except:
74
         return results
72
         return results
75
-
76
     answer = '{0} {1} = {2} {3}, 1 {1} ({5}) = {4} {3} ({6})'.format(
73
     answer = '{0} {1} = {2} {3}, 1 {1} ({5}) = {4} {3} ({6})'.format(
77
         resp.search_params['amount'],
74
         resp.search_params['amount'],
78
         resp.search_params['from'],
75
         resp.search_params['from'],
83
         resp.search_params['to_name'],
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
         resp.search_params['from'].upper(), resp.search_params['to'])
84
         resp.search_params['from'].upper(), resp.search_params['to'])
88
 
85
 
89
     results.append({'answer': answer, 'url': url})
86
     results.append({'answer': answer, 'url': url})