Merge pull request #320 from dalf/currency

[enh] currency_convert engine : "1 dollars in euros"
This commit is contained in:
Adam Tauber
2015-05-24 15:09:49 -04:00
4 changed files with 7868 additions and 6 deletions

7655
searx/data/currencies.json Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,11 +1,37 @@
from datetime import datetime
import re
import os
import json
import unicodedata
categories = []
url = 'https://download.finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s={query}=X'
weight = 100
parser_re = re.compile(r'^\W*(\d+(?:\.\d+)?)\W*([a-z]{3})\W*(?:in)?\W*([a-z]{3})\W*$', re.I) # noqa
parser_re = re.compile(r'^\W*(\d+(?:\.\d+)?)\W*([^.0-9].+)\W*in?\W*([^\.]+)\W*$', re.I) # noqa
db = 1
def normalize_name(name):
name = name.lower().replace('-', ' ')
name = re.sub(' +', ' ', name)
return unicodedata.normalize('NFKD', u"" + name).lower()
def name_to_iso4217(name):
global db
name = normalize_name(name)
currencies = db['names'].get(name, [name])
return currencies[0]
def iso4217_to_name(iso4217, language):
global db
return db['iso4217'].get(iso4217, {}).get(language, iso4217)
def request(query, params):
@@ -16,6 +42,8 @@ def request(query, params):
ammount, from_currency, to_currency = m.groups()
ammount = float(ammount)
from_currency = name_to_iso4217(from_currency.strip())
to_currency = name_to_iso4217(to_currency.strip())
q = (from_currency + to_currency).upper()
@@ -23,6 +51,8 @@ def request(query, params):
params['ammount'] = ammount
params['from'] = from_currency
params['to'] = to_currency
params['from_name'] = iso4217_to_name(from_currency, 'en')
params['to_name'] = iso4217_to_name(to_currency, 'en')
return params
@@ -35,12 +65,14 @@ def response(resp):
except:
return results
answer = '{0} {1} = {2} {3} (1 {1} = {4} {3})'.format(
answer = '{0} {1} = {2} {3}, 1 {1} ({5}) = {4} {3} ({6})'.format(
resp.search_params['ammount'],
resp.search_params['from'],
resp.search_params['ammount'] * conversion_rate,
resp.search_params['to'],
conversion_rate
conversion_rate,
resp.search_params['from_name'],
resp.search_params['to_name'],
)
now_date = datetime.now().strftime('%Y%m%d')
@@ -55,3 +87,15 @@ def response(resp):
results.append({'answer': answer, 'url': url})
return results
def load():
global db
current_dir = os.path.dirname(os.path.realpath(__file__))
json_data = open(current_dir + "/../data/currencies.json").read()
db = json.loads(json_data)
load()

View File

@@ -27,9 +27,11 @@ class TestCurrencyConvertEngine(SearxTestCase):
def test_response(self):
dicto = defaultdict(dict)
dicto['ammount'] = 10
dicto['ammount'] = float(10)
dicto['from'] = "EUR"
dicto['to'] = "USD"
dicto['from_name'] = "euro"
dicto['to_name'] = "United States dollar"
response = mock.Mock(text='a,b,c,d', search_params=dicto)
self.assertEqual(currency_convert.response(response), [])
@@ -38,7 +40,7 @@ class TestCurrencyConvertEngine(SearxTestCase):
results = currency_convert.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['answer'], '10 EUR = 5.0 USD (1 EUR = 0.5 USD)')
self.assertEqual(results[0]['answer'], '10.0 EUR = 5.0 USD, 1 EUR (euro) = 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-eur-to-usd.html')
now_date + '/10.0-eur-to-usd.html')