on master help sign in
Merge pull request #1279 from rinpatch/duckduckgo-currency

Switched currency converter to duckduckgo and added Bitcoin to currency list

commit5f3ada4721831b5dee5db2704cfcb45fdfda04db authorAdam Tauber <asciimoo@gmail.com> dateMon 23 Apr 2018, 14:08 (9 years ago) parent829d47906e070a
3 files changed+35−15
searx/data/currencies.json+14−2 searx/engines/currency_convert.py+5−9 tests/unit/engines/test_currency_convert.py+16−4
searx/data/currencies.json+14−2
@@ -6163,9 +6163,21 @@
61636163 ],
61646164 "scellino keniota": [
61656165 "KES"
6166− ]
6166+ ],
6167+ "bitcoin": [
6168+ "XBT"
6169+ ]
61676170 },
61686171 "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+ },
61696181 "DZD": {
61706182 "fr": "Dinar alg\u00e9rien",
61716183 "en": "Algerian dinar",
@@ -7652,4 +7664,4 @@
76527664 "es": "Libra sudanesa"
76537665 }
76547666 }
7655−}
7667+}
searx/engines/currency_convert.py+5−9
@@ -11,7 +11,7 @@ if sys.version_info[0] == 3:
1111 unicode = str
1212
1313categories = []
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}'
1515weight = 100
1616
1717parser_re = re.compile(b'.*?(\\d+(?:\\.\\d+)?) ([^.0-9]+) (?:in|to) ([^.0-9]+)', re.I)
@@ -44,7 +44,6 @@ def request(query, params):
4444 if not m:
4545 # wrong query
4646 return params
47−
4847 amount, from_currency, to_currency = m.groups()
4948 amount = float(amount)
5049 from_currency = name_to_iso4217(from_currency.strip())
@@ -63,16 +62,13 @@ def request(query, params):
6362
6463
6564def 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]
6667 results = []
67− pat = '<span class=bld>(.+) {0}</span>'.format(
68− resp.search_params['to'].upper())
69−
7068 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'])
7370 except:
7471 return results
75−
7672 answer = '{0} {1} = {2} {3}, 1 {1} ({5}) = {4} {3} ({6})'.format(
7773 resp.search_params['amount'],
7874 resp.search_params['from'],
@@ -83,7 +79,7 @@ def response(resp):
8379 resp.search_params['to_name'],
8480 )
8581
86− url = 'https://finance.google.com/finance?q={0}{1}'.format(
82+ url = 'https://duckduckgo.com/js/spice/currency/1/{0}/{1}'.format(
8783 resp.search_params['from'].upper(), resp.search_params['to'])
8884
8985 results.append({'answer': answer, 'url': url})
tests/unit/engines/test_currency_convert.py+16−4
@@ -17,7 +17,7 @@ class TestCurrencyConvertEngine(SearxTestCase):
1717 query = b'convert 10 Pound Sterlings to United States Dollars'
1818 params = currency_convert.request(query, dicto)
1919 self.assertIn('url', params)
20− self.assertIn('finance.google.com', params['url'])
20+ self.assertIn('duckduckgo.com', params['url'])
2121 self.assertIn('GBP', params['url'])
2222 self.assertIn('USD', params['url'])
2323
@@ -30,8 +30,20 @@ class TestCurrencyConvertEngine(SearxTestCase):
3030 dicto['to_name'] = "United States dollar"
3131 response = mock.Mock(text='a,b,c,d', search_params=dicto)
3232 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+ """
3547 response = mock.Mock(text=body, search_params=dicto)
3648 results = currency_convert.response(response)
3749 self.assertEqual(type(results), list)
@@ -39,6 +51,6 @@ class TestCurrencyConvertEngine(SearxTestCase):
3951 self.assertEqual(results[0]['answer'], '10.0 GBP = 5.0 USD, 1 GBP (pound sterling)' +
4052 ' = 0.5 USD (United States dollar)')
4153
42− target_url = 'https://finance.google.com/finance?q={}{}'.format(
54+ target_url = 'https://duckduckgo.com/js/spice/currency/1/{}/{}'.format(
4355 dicto['from'], dicto['to'])
4456 self.assertEqual(results[0]['url'], target_url)