[enh] smarter currency query parse

This commit is contained in:
asciimoo 2013-12-01 16:41:24 +01:00
parent cfb06048ac
commit a35128f5e0
1 changed files with 8 additions and 4 deletions

View File

@ -1,21 +1,25 @@
from datetime import datetime from datetime import datetime
import re
categories = [] categories = []
url = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s={query}=X' url = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s={query}=X'
weight = 100 weight = 100
parser_re = re.compile(r'^\W*(\d+(?:\.\d+)?)\W*([a-z]{3})\W*(?:in)?\W*([a-z]{3})\W*$')
def request(query, params): def request(query, params):
m = parser_re.match(query)
if not m:
# wrong query
return params
try: try:
# eg.: "X EUR in USD" ammount, from_currency, to_currency = m.groups()
ammount, from_currency, _, to_currency = query.split()
ammount = float(ammount) ammount = float(ammount)
except: except:
# wrong params # wrong params
return params return params
q = (from_currency+to_currency).upper() q = (from_currency+to_currency).upper()
if not q.isalpha():
return params
params['url'] = url.format(query=q) params['url'] = url.format(query=q)
params['ammount'] = ammount params['ammount'] = ammount