From 2231b02add7443c2cc2692bc0d63d92dfd09c043 Mon Sep 17 00:00:00 2001 From: pyrrh0n1c Date: Tue, 7 Nov 2017 13:29:17 +0000 Subject: [PATCH 1/3] Fixed the currency_convert engine. --- searx/engines/currency_convert.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/searx/engines/currency_convert.py b/searx/engines/currency_convert.py index 1218d4849..984636cd7 100644 --- a/searx/engines/currency_convert.py +++ b/searx/engines/currency_convert.py @@ -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) @@ -51,7 +51,7 @@ def request(query, params): q = (from_currency + to_currency).upper() - params['url'] = url.format(query=q) + params['url'] = url.format(from_currency, to_currency) params['ammount'] = ammount params['from'] = from_currency params['to'] = to_currency @@ -63,8 +63,11 @@ def request(query, params): def response(resp): results = [] + pat = '(.+) {0}'.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 @@ -79,14 +82,8 @@ def response(resp): 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}) From 4340c0b16cd6e3375b1867cc9ff02c63d5c4e4fe Mon Sep 17 00:00:00 2001 From: pyrrh0n1c Date: Tue, 7 Nov 2017 14:14:20 +0000 Subject: [PATCH 2/3] Fixed typos. --- searx/engines/currency_convert.py | 10 +++++----- tests/unit/engines/test_currency_convert.py | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/searx/engines/currency_convert.py b/searx/engines/currency_convert.py index 984636cd7..1bb4e60c9 100644 --- a/searx/engines/currency_convert.py +++ b/searx/engines/currency_convert.py @@ -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(from_currency, to_currency) - params['ammount'] = ammount + params['amount'] = amount params['from'] = from_currency params['to'] = to_currency params['from_name'] = iso4217_to_name(from_currency, 'en') @@ -73,9 +73,9 @@ def response(resp): 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'], diff --git a/tests/unit/engines/test_currency_convert.py b/tests/unit/engines/test_currency_convert.py index 2814d791d..e8353a93a 100644 --- a/tests/unit/engines/test_currency_convert.py +++ b/tests/unit/engines/test_currency_convert.py @@ -23,7 +23,7 @@ class TestCurrencyConvertEngine(SearxTestCase): 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" From a1d1a50149b76545086fd2639ff58797007e435e Mon Sep 17 00:00:00 2001 From: pyrrh0n1c Date: Tue, 7 Nov 2017 14:49:10 +0000 Subject: [PATCH 3/3] Fixed unittests. --- tests/unit/engines/test_currency_convert.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/unit/engines/test_currency_convert.py b/tests/unit/engines/test_currency_convert.py index e8353a93a..0758e2fc8 100644 --- a/tests/unit/engines/test_currency_convert.py +++ b/tests/unit/engines/test_currency_convert.py @@ -17,7 +17,7 @@ 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']) @@ -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 = "0.5 {}".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)