forked from Ponysearch/Ponysearch
Merge pull request #1079 from pyrrh0n1c/master
Fixed the currency_convert engine.
This commit is contained in:
commit
d00aaeedfa
2 changed files with 21 additions and 23 deletions
|
@ -10,7 +10,7 @@ if sys.version_info[0] == 3:
|
||||||
unicode = str
|
unicode = str
|
||||||
|
|
||||||
categories = []
|
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
|
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,15 +44,15 @@ def request(query, params):
|
||||||
# wrong query
|
# wrong query
|
||||||
return params
|
return params
|
||||||
|
|
||||||
ammount, from_currency, to_currency = m.groups()
|
amount, from_currency, to_currency = m.groups()
|
||||||
ammount = float(ammount)
|
amount = float(amount)
|
||||||
from_currency = name_to_iso4217(from_currency.strip())
|
from_currency = name_to_iso4217(from_currency.strip())
|
||||||
to_currency = name_to_iso4217(to_currency.strip())
|
to_currency = name_to_iso4217(to_currency.strip())
|
||||||
|
|
||||||
q = (from_currency + to_currency).upper()
|
q = (from_currency + to_currency).upper()
|
||||||
|
|
||||||
params['url'] = url.format(query=q)
|
params['url'] = url.format(from_currency, to_currency)
|
||||||
params['ammount'] = ammount
|
params['amount'] = amount
|
||||||
params['from'] = from_currency
|
params['from'] = from_currency
|
||||||
params['to'] = to_currency
|
params['to'] = to_currency
|
||||||
params['from_name'] = iso4217_to_name(from_currency, 'en')
|
params['from_name'] = iso4217_to_name(from_currency, 'en')
|
||||||
|
@ -63,30 +63,27 @@ def request(query, params):
|
||||||
|
|
||||||
def response(resp):
|
def response(resp):
|
||||||
results = []
|
results = []
|
||||||
|
pat = '<span class=bld>(.+) {0}</span>'.format(
|
||||||
|
resp.search_params['to'].upper())
|
||||||
|
|
||||||
try:
|
try:
|
||||||
_, conversion_rate, _ = resp.text.split(',', 2)
|
conversion_rate = re.findall(pat, resp.text)[0]
|
||||||
conversion_rate = float(conversion_rate)
|
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['ammount'],
|
resp.search_params['amount'],
|
||||||
resp.search_params['from'],
|
resp.search_params['from'],
|
||||||
resp.search_params['ammount'] * conversion_rate,
|
resp.search_params['amount'] * conversion_rate,
|
||||||
resp.search_params['to'],
|
resp.search_params['to'],
|
||||||
conversion_rate,
|
conversion_rate,
|
||||||
resp.search_params['from_name'],
|
resp.search_params['from_name'],
|
||||||
resp.search_params['to_name'],
|
resp.search_params['to_name'],
|
||||||
)
|
)
|
||||||
|
|
||||||
now_date = datetime.now().strftime('%Y%m%d')
|
url = 'https://finance.google.com/finance?q={0}{1}'.format(
|
||||||
url = 'https://finance.yahoo.com/currency/converter-results/{0}/{1}-{2}-to-{3}.html' # noqa
|
resp.search_params['from'].upper(), resp.search_params['to'])
|
||||||
url = url.format(
|
|
||||||
now_date,
|
|
||||||
resp.search_params['ammount'],
|
|
||||||
resp.search_params['from'].lower(),
|
|
||||||
resp.search_params['to'].lower()
|
|
||||||
)
|
|
||||||
|
|
||||||
results.append({'answer': answer, 'url': url})
|
results.append({'answer': answer, 'url': url})
|
||||||
|
|
||||||
|
|
|
@ -17,13 +17,13 @@ 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.yahoo.com', params['url'])
|
self.assertIn('finance.google.com', params['url'])
|
||||||
self.assertIn('GBP', params['url'])
|
self.assertIn('GBP', params['url'])
|
||||||
self.assertIn('USD', params['url'])
|
self.assertIn('USD', params['url'])
|
||||||
|
|
||||||
def test_response(self):
|
def test_response(self):
|
||||||
dicto = defaultdict(dict)
|
dicto = defaultdict(dict)
|
||||||
dicto['ammount'] = float(10)
|
dicto['amount'] = float(10)
|
||||||
dicto['from'] = "GBP"
|
dicto['from'] = "GBP"
|
||||||
dicto['to'] = "USD"
|
dicto['to'] = "USD"
|
||||||
dicto['from_name'] = "pound sterling"
|
dicto['from_name'] = "pound sterling"
|
||||||
|
@ -31,13 +31,14 @@ class TestCurrencyConvertEngine(SearxTestCase):
|
||||||
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), [])
|
||||||
|
|
||||||
csv = "2,0.5,1"
|
body = "<span class=bld>0.5 {}</span>".format(dicto['to'])
|
||||||
response = mock.Mock(text=csv, 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)
|
||||||
self.assertEqual(len(results), 1)
|
self.assertEqual(len(results), 1)
|
||||||
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)')
|
||||||
now_date = datetime.now().strftime('%Y%m%d')
|
|
||||||
self.assertEqual(results[0]['url'], 'https://finance.yahoo.com/currency/converter-results/' +
|
target_url = 'https://finance.google.com/finance?q={}{}'.format(
|
||||||
now_date + '/10.0-gbp-to-usd.html')
|
dicto['from'], dicto['to'])
|
||||||
|
self.assertEqual(results[0]['url'], target_url)
|
||||||
|
|
Loading…
Reference in a new issue