forked from zaclys/searxng
Merge pull request #1079 from pyrrh0n1c/master
Fixed the currency_convert engine.
This commit is contained in:
commit
d00aaeedfa
|
@ -10,7 +10,7 @@ if sys.version_info[0] == 3:
|
|||
unicode = str
|
||||
|
||||
categories = []
|
||||
url = 'https://download.finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s={query}=X'
|
||||
url = 'https://finance.google.com/finance/converter?a=1&from={0}&to={1}'
|
||||
weight = 100
|
||||
|
||||
parser_re = re.compile(b'.*?(\\d+(?:\\.\\d+)?) ([^.0-9]+) (?:in|to) ([^.0-9]+)', re.I)
|
||||
|
@ -44,15 +44,15 @@ def request(query, params):
|
|||
# wrong query
|
||||
return params
|
||||
|
||||
ammount, from_currency, to_currency = m.groups()
|
||||
ammount = float(ammount)
|
||||
amount, from_currency, to_currency = m.groups()
|
||||
amount = float(amount)
|
||||
from_currency = name_to_iso4217(from_currency.strip())
|
||||
to_currency = name_to_iso4217(to_currency.strip())
|
||||
|
||||
q = (from_currency + to_currency).upper()
|
||||
|
||||
params['url'] = url.format(query=q)
|
||||
params['ammount'] = ammount
|
||||
params['url'] = url.format(from_currency, to_currency)
|
||||
params['amount'] = amount
|
||||
params['from'] = from_currency
|
||||
params['to'] = to_currency
|
||||
params['from_name'] = iso4217_to_name(from_currency, 'en')
|
||||
|
@ -63,30 +63,27 @@ def request(query, params):
|
|||
|
||||
def response(resp):
|
||||
results = []
|
||||
pat = '<span class=bld>(.+) {0}</span>'.format(
|
||||
resp.search_params['to'].upper())
|
||||
|
||||
try:
|
||||
_, conversion_rate, _ = resp.text.split(',', 2)
|
||||
conversion_rate = re.findall(pat, resp.text)[0]
|
||||
conversion_rate = float(conversion_rate)
|
||||
except:
|
||||
return results
|
||||
|
||||
answer = '{0} {1} = {2} {3}, 1 {1} ({5}) = {4} {3} ({6})'.format(
|
||||
resp.search_params['ammount'],
|
||||
resp.search_params['amount'],
|
||||
resp.search_params['from'],
|
||||
resp.search_params['ammount'] * conversion_rate,
|
||||
resp.search_params['amount'] * conversion_rate,
|
||||
resp.search_params['to'],
|
||||
conversion_rate,
|
||||
resp.search_params['from_name'],
|
||||
resp.search_params['to_name'],
|
||||
)
|
||||
|
||||
now_date = datetime.now().strftime('%Y%m%d')
|
||||
url = 'https://finance.yahoo.com/currency/converter-results/{0}/{1}-{2}-to-{3}.html' # noqa
|
||||
url = url.format(
|
||||
now_date,
|
||||
resp.search_params['ammount'],
|
||||
resp.search_params['from'].lower(),
|
||||
resp.search_params['to'].lower()
|
||||
)
|
||||
url = 'https://finance.google.com/finance?q={0}{1}'.format(
|
||||
resp.search_params['from'].upper(), resp.search_params['to'])
|
||||
|
||||
results.append({'answer': answer, 'url': url})
|
||||
|
||||
|
|
|
@ -17,13 +17,13 @@ class TestCurrencyConvertEngine(SearxTestCase):
|
|||
query = b'convert 10 Pound Sterlings to United States Dollars'
|
||||
params = currency_convert.request(query, dicto)
|
||||
self.assertIn('url', params)
|
||||
self.assertIn('finance.yahoo.com', params['url'])
|
||||
self.assertIn('finance.google.com', params['url'])
|
||||
self.assertIn('GBP', params['url'])
|
||||
self.assertIn('USD', params['url'])
|
||||
|
||||
def test_response(self):
|
||||
dicto = defaultdict(dict)
|
||||
dicto['ammount'] = float(10)
|
||||
dicto['amount'] = float(10)
|
||||
dicto['from'] = "GBP"
|
||||
dicto['to'] = "USD"
|
||||
dicto['from_name'] = "pound sterling"
|
||||
|
@ -31,13 +31,14 @@ class TestCurrencyConvertEngine(SearxTestCase):
|
|||
response = mock.Mock(text='a,b,c,d', search_params=dicto)
|
||||
self.assertEqual(currency_convert.response(response), [])
|
||||
|
||||
csv = "2,0.5,1"
|
||||
response = mock.Mock(text=csv, search_params=dicto)
|
||||
body = "<span class=bld>0.5 {}</span>".format(dicto['to'])
|
||||
response = mock.Mock(text=body, search_params=dicto)
|
||||
results = currency_convert.response(response)
|
||||
self.assertEqual(type(results), list)
|
||||
self.assertEqual(len(results), 1)
|
||||
self.assertEqual(results[0]['answer'], '10.0 GBP = 5.0 USD, 1 GBP (pound sterling)' +
|
||||
' = 0.5 USD (United States dollar)')
|
||||
now_date = datetime.now().strftime('%Y%m%d')
|
||||
self.assertEqual(results[0]['url'], 'https://finance.yahoo.com/currency/converter-results/' +
|
||||
now_date + '/10.0-gbp-to-usd.html')
|
||||
|
||||
target_url = 'https://finance.google.com/finance?q={}{}'.format(
|
||||
dicto['from'], dicto['to'])
|
||||
self.assertEqual(results[0]['url'], target_url)
|
||||
|
|
Loading…
Reference in New Issue