mirror of
				https://github.com/searxng/searxng
				synced 2024-01-01 19:24:07 +01:00 
			
		
		
		
	Merge pull request #1279 from rinpatch/duckduckgo-currency
Switched currency converter to duckduckgo and added Bitcoin to currency list
This commit is contained in:
		
						commit
						5f3ada4721
					
				
					 3 changed files with 35 additions and 15 deletions
				
			
		| 
						 | 
					@ -6163,9 +6163,21 @@
 | 
				
			||||||
        ], 
 | 
					        ], 
 | 
				
			||||||
        "scellino keniota": [
 | 
					        "scellino keniota": [
 | 
				
			||||||
            "KES"
 | 
					            "KES"
 | 
				
			||||||
        ]
 | 
					        ],
 | 
				
			||||||
 | 
						"bitcoin": [
 | 
				
			||||||
 | 
							"XBT"
 | 
				
			||||||
 | 
						]	
 | 
				
			||||||
    }, 
 | 
					    }, 
 | 
				
			||||||
    "iso4217": {
 | 
					    "iso4217": {
 | 
				
			||||||
 | 
						"XBT":{
 | 
				
			||||||
 | 
						    "fr": "Bitcoin", 
 | 
				
			||||||
 | 
					            "en": "Bitcoin", 
 | 
				
			||||||
 | 
					            "nl": "Bitcoin", 
 | 
				
			||||||
 | 
					            "de": "Bitcoin", 
 | 
				
			||||||
 | 
					            "it": "Bitcoin", 
 | 
				
			||||||
 | 
					            "hu": "Bitcoin", 
 | 
				
			||||||
 | 
					            "es": "Bitcoin"
 | 
				
			||||||
 | 
						},	
 | 
				
			||||||
        "DZD": {
 | 
					        "DZD": {
 | 
				
			||||||
            "fr": "Dinar alg\u00e9rien", 
 | 
					            "fr": "Dinar alg\u00e9rien", 
 | 
				
			||||||
            "en": "Algerian dinar", 
 | 
					            "en": "Algerian dinar", 
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -11,7 +11,7 @@ if sys.version_info[0] == 3:
 | 
				
			||||||
    unicode = str
 | 
					    unicode = str
 | 
				
			||||||
 | 
					
 | 
				
			||||||
categories = []
 | 
					categories = []
 | 
				
			||||||
url = 'https://finance.google.com/finance/converter?a=1&from={0}&to={1}'
 | 
					url = 'https://duckduckgo.com/js/spice/currency/1/{0}/{1}'
 | 
				
			||||||
weight = 100
 | 
					weight = 100
 | 
				
			||||||
 | 
					
 | 
				
			||||||
parser_re = re.compile(b'.*?(\\d+(?:\\.\\d+)?) ([^.0-9]+) (?:in|to) ([^.0-9]+)', re.I)
 | 
					parser_re = re.compile(b'.*?(\\d+(?:\\.\\d+)?) ([^.0-9]+) (?:in|to) ([^.0-9]+)', re.I)
 | 
				
			||||||
| 
						 | 
					@ -44,7 +44,6 @@ def request(query, params):
 | 
				
			||||||
    if not m:
 | 
					    if not m:
 | 
				
			||||||
        # wrong query
 | 
					        # wrong query
 | 
				
			||||||
        return params
 | 
					        return params
 | 
				
			||||||
 | 
					 | 
				
			||||||
    amount, from_currency, to_currency = m.groups()
 | 
					    amount, from_currency, to_currency = m.groups()
 | 
				
			||||||
    amount = float(amount)
 | 
					    amount = float(amount)
 | 
				
			||||||
    from_currency = name_to_iso4217(from_currency.strip())
 | 
					    from_currency = name_to_iso4217(from_currency.strip())
 | 
				
			||||||
| 
						 | 
					@ -63,16 +62,13 @@ def request(query, params):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def response(resp):
 | 
					def response(resp):
 | 
				
			||||||
 | 
					    """remove first and last lines to get only json"""
 | 
				
			||||||
 | 
					    json_resp = resp.text[resp.text.find('\n') + 1:resp.text.rfind('\n') - 2]
 | 
				
			||||||
    results = []
 | 
					    results = []
 | 
				
			||||||
    pat = '<span class=bld>(.+) {0}</span>'.format(
 | 
					 | 
				
			||||||
        resp.search_params['to'].upper())
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        conversion_rate = re.findall(pat, resp.text)[0]
 | 
					        conversion_rate = float(json.loads(json_resp)['conversion']['converted-amount'])
 | 
				
			||||||
        conversion_rate = float(conversion_rate)
 | 
					 | 
				
			||||||
    except:
 | 
					    except:
 | 
				
			||||||
        return results
 | 
					        return results
 | 
				
			||||||
 | 
					 | 
				
			||||||
    answer = '{0} {1} = {2} {3}, 1 {1} ({5}) = {4} {3} ({6})'.format(
 | 
					    answer = '{0} {1} = {2} {3}, 1 {1} ({5}) = {4} {3} ({6})'.format(
 | 
				
			||||||
        resp.search_params['amount'],
 | 
					        resp.search_params['amount'],
 | 
				
			||||||
        resp.search_params['from'],
 | 
					        resp.search_params['from'],
 | 
				
			||||||
| 
						 | 
					@ -83,7 +79,7 @@ def response(resp):
 | 
				
			||||||
        resp.search_params['to_name'],
 | 
					        resp.search_params['to_name'],
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    url = 'https://finance.google.com/finance?q={0}{1}'.format(
 | 
					    url = 'https://duckduckgo.com/js/spice/currency/1/{0}/{1}'.format(
 | 
				
			||||||
        resp.search_params['from'].upper(), resp.search_params['to'])
 | 
					        resp.search_params['from'].upper(), resp.search_params['to'])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    results.append({'answer': answer, 'url': url})
 | 
					    results.append({'answer': answer, 'url': url})
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -17,7 +17,7 @@ class TestCurrencyConvertEngine(SearxTestCase):
 | 
				
			||||||
        query = b'convert 10 Pound Sterlings to United States Dollars'
 | 
					        query = b'convert 10 Pound Sterlings to United States Dollars'
 | 
				
			||||||
        params = currency_convert.request(query, dicto)
 | 
					        params = currency_convert.request(query, dicto)
 | 
				
			||||||
        self.assertIn('url', params)
 | 
					        self.assertIn('url', params)
 | 
				
			||||||
        self.assertIn('finance.google.com', params['url'])
 | 
					        self.assertIn('duckduckgo.com', params['url'])
 | 
				
			||||||
        self.assertIn('GBP', params['url'])
 | 
					        self.assertIn('GBP', params['url'])
 | 
				
			||||||
        self.assertIn('USD', params['url'])
 | 
					        self.assertIn('USD', params['url'])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -30,8 +30,20 @@ class TestCurrencyConvertEngine(SearxTestCase):
 | 
				
			||||||
        dicto['to_name'] = "United States dollar"
 | 
					        dicto['to_name'] = "United States dollar"
 | 
				
			||||||
        response = mock.Mock(text='a,b,c,d', search_params=dicto)
 | 
					        response = mock.Mock(text='a,b,c,d', search_params=dicto)
 | 
				
			||||||
        self.assertEqual(currency_convert.response(response), [])
 | 
					        self.assertEqual(currency_convert.response(response), [])
 | 
				
			||||||
 | 
					        body = """ddg_spice_currency(
 | 
				
			||||||
        body = "<span class=bld>0.5 {}</span>".format(dicto['to'])
 | 
					            {
 | 
				
			||||||
 | 
					                "conversion":{
 | 
				
			||||||
 | 
					                    "converted-amount": "0.5"
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                "topConversions":[
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                    },
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                ]
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
        response = mock.Mock(text=body, search_params=dicto)
 | 
					        response = mock.Mock(text=body, search_params=dicto)
 | 
				
			||||||
        results = currency_convert.response(response)
 | 
					        results = currency_convert.response(response)
 | 
				
			||||||
        self.assertEqual(type(results), list)
 | 
					        self.assertEqual(type(results), list)
 | 
				
			||||||
| 
						 | 
					@ -39,6 +51,6 @@ class TestCurrencyConvertEngine(SearxTestCase):
 | 
				
			||||||
        self.assertEqual(results[0]['answer'], '10.0 GBP = 5.0 USD, 1 GBP (pound sterling)' +
 | 
					        self.assertEqual(results[0]['answer'], '10.0 GBP = 5.0 USD, 1 GBP (pound sterling)' +
 | 
				
			||||||
                         ' = 0.5 USD (United States dollar)')
 | 
					                         ' = 0.5 USD (United States dollar)')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        target_url = 'https://finance.google.com/finance?q={}{}'.format(
 | 
					        target_url = 'https://duckduckgo.com/js/spice/currency/1/{}/{}'.format(
 | 
				
			||||||
            dicto['from'], dicto['to'])
 | 
					            dicto['from'], dicto['to'])
 | 
				
			||||||
        self.assertEqual(results[0]['url'], target_url)
 | 
					        self.assertEqual(results[0]['url'], target_url)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue