Add USD support

This commit is contained in:
Marcel van der Boom 2014-11-28 22:59:52 +01:00
parent 5cda6ad9e6
commit d2974d295f

View File

@ -1,35 +1,49 @@
function getBTCEUR(eur) function getBTCX(amount,currency)
-- Do request on bitcoinaverage, the final / is critical! -- Do request on bitcoinaverage, the final / is critical!
local res,code = https.request("https://api.bitcoinaverage.com/ticker/global/EUR/") local res,code = https.request("https://api.bitcoinaverage.com/ticker/global/"..currency.."/")
if code~= 200 then return nil end if code~= 200 then return nil end
local data = json:decode(res) local data = json:decode(res)
-- Easy, it's right there -- Easy, it's right there
text = "BTC/EUR"..'\n'..'Buy: '..data.ask..'\n'..'Sell: '..data.bid text = "BTC/"..currency..'\n'..'Buy: '..data.ask..'\n'..'Sell: '..data.bid
-- If we have a number as second parameter, calculate the bitcoin amount -- If we have a number as second parameter, calculate the bitcoin amount
if eur~=nil then if amount~=nil then
btc = tonumber(eur) / tonumber(data.ask) btc = tonumber(amount) / tonumber(data.ask)
text = text.."\n EUR "..eur.." = BTC "..btc text = text.."\n "..currency .." "..amount.." = BTC "..btc
end end
return text return text
end end
function run(msg, matches) function run(msg, matches)
if matches[1] == "!btc" then vardump(matches)
return getBTCEUR(nil) local cur = 'EUR'
local amt = nil
-- Get the global match out of the way
if matches[1] == "!btc" then return getBTCX(amt,cur) end
if matches[2]~=nil then
-- There is a second match
amt = matches[2]
cur = string.upper(matches[1])
else
-- Just a EUR or USD param
cur = string.upper(matches[1])
end end
return getBTCEUR(matches[1]) return getBTCX(amt,cur)
end end
return { return {
description = "BTCEUR market value", description = "Bitcoin global average market value (in EUR or USD)",
usage = "!btc [EUR]", usage = "!btc [EUR|USD] [amount]",
patterns = { patterns = {
"^!btc$", "^!btc$",
"^!btc (%d+[%d%.]*)$", "^!btc ([Ee][Uu][Rr])$",
"^!btc ([Uu][Ss][Dd])$",
"^!btc (EUR) (%d+[%d%.]*)$",
"^!btc (USD) (%d+[%d%.]*)$"
}, },
run = run run = run
} }