mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-27 13:19:24 +01:00
Money is a float not an int. Also, uber simplify Currency.yahoo since they
provide a csv file. Yay, no html parsing!
This commit is contained in:
parent
eb23fa3b14
commit
eb60864c51
@ -85,7 +85,7 @@ class Currency(callbacks.Privmsg):
|
|||||||
(number, curr1, curr2) = privmsgs.getArgs(args, required=2,
|
(number, curr1, curr2) = privmsgs.getArgs(args, required=2,
|
||||||
optional=1)
|
optional=1)
|
||||||
try:
|
try:
|
||||||
number = int(number)
|
number = float(number)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
curr2 = curr1
|
curr2 = curr1
|
||||||
curr1 = number
|
curr1 = number
|
||||||
@ -120,10 +120,6 @@ class Currency(callbacks.Privmsg):
|
|||||||
irc.error('XE must\'ve changed the format of their site.')
|
irc.error('XE must\'ve changed the format of their site.')
|
||||||
return
|
return
|
||||||
|
|
||||||
_yahooConvert = re.compile(r'\w{6}=X</a></td><td class[^>]+><b>([\d.]+)'
|
|
||||||
r'</b></td><td class[^>]+>\w{3} \d\d?</td><td'
|
|
||||||
r' class=[^>]+>[\d.]+</td><td class[^>]+><b>'
|
|
||||||
r'([\d,]+(?:.0{2}))', re.I | re.S)
|
|
||||||
def yahoo(self, irc, msg, args):
|
def yahoo(self, irc, msg, args):
|
||||||
"""[<number>] <currency1> to <currency2>
|
"""[<number>] <currency1> to <currency2>
|
||||||
|
|
||||||
@ -133,39 +129,34 @@ class Currency(callbacks.Privmsg):
|
|||||||
(number, curr1, curr2) = privmsgs.getArgs(args, required=2,
|
(number, curr1, curr2) = privmsgs.getArgs(args, required=2,
|
||||||
optional=1)
|
optional=1)
|
||||||
try:
|
try:
|
||||||
number = int(number)
|
number = float(number)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
curr2 = curr1
|
curr2 = curr1
|
||||||
curr1 = number
|
curr1 = number
|
||||||
number = 1
|
number = 1
|
||||||
curr1 = curr1.lower()
|
curr1 = curr1.upper()
|
||||||
curr2 = curr2.lower()
|
curr2 = curr2.upper()
|
||||||
if curr2.startswith('to '):
|
if curr2.startswith('TO '):
|
||||||
curr2 = curr2[3:]
|
curr2 = curr2[3:]
|
||||||
if len(curr1) != 3 and len(curr2) != 3:
|
if len(curr1) != 3 and len(curr2) != 3:
|
||||||
irc.error(self._symbolError)
|
irc.error(self._symbolError)
|
||||||
return
|
return
|
||||||
url = 'http://finance.yahoo.com/currency/convert?amt=%s&from=%s&'\
|
url = r'http://finance.yahoo.com/d/quotes.csv?'\
|
||||||
'to=%s&submit=Convert'
|
r's=%s%s=X&f=sl1d1t1ba&e=.csv' % (curr1, curr2)
|
||||||
try:
|
try:
|
||||||
text = webutils.getUrl(url % (number, curr1, curr2))
|
text = webutils.getUrl(url)
|
||||||
except webutils.WebError, e:
|
except webutils.WebError, e:
|
||||||
irc.error(str(e))
|
irc.error(str(e))
|
||||||
return
|
return
|
||||||
conv = self._yahooConvert.search(text)
|
if 'N/A' in text:
|
||||||
if conv is not None:
|
irc.error('You used an incorrect currency symbol.')
|
||||||
resp = [conv.group(1), curr1.upper(), '=',
|
|
||||||
conv.group(2).replace(',', ''), curr2.upper()]
|
|
||||||
if '.' not in resp[0]:
|
|
||||||
resp[0] = '%s.00' % resp[0]
|
|
||||||
elif resp[0].endswith('.0'):
|
|
||||||
resp[0] = '%.00' % resp[:-2]
|
|
||||||
irc.reply(' '.join(resp))
|
|
||||||
return
|
|
||||||
else:
|
|
||||||
irc.error('Either you used the wrong currency symbol(s) or Yahoo '
|
|
||||||
'changed the format of their site.')
|
|
||||||
return
|
return
|
||||||
|
conv = text.split(',')[1]
|
||||||
|
conv = number * float(conv)
|
||||||
|
resp = [str(number), curr1.upper(), '=', str(conv), curr2.upper()]
|
||||||
|
if '.' not in resp[0] and 'e' not in resp[0]:
|
||||||
|
resp[0] = '%s.00' % resp[0]
|
||||||
|
irc.reply(' '.join(resp))
|
||||||
|
|
||||||
conf.registerPlugin('Currency')
|
conf.registerPlugin('Currency')
|
||||||
conf.registerChannelValue(conf.supybot.plugins.Currency, 'command',
|
conf.registerChannelValue(conf.supybot.plugins.Currency, 'command',
|
||||||
|
Loading…
Reference in New Issue
Block a user