From 5cda6ad9e69c7951a28d9f22a157698224961af1 Mon Sep 17 00:00:00 2001 From: Marcel van der Boom Date: Fri, 28 Nov 2014 22:25:38 +0100 Subject: [PATCH 1/2] Initial implemenation of btc command --- plugins/btc.lua | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 plugins/btc.lua diff --git a/plugins/btc.lua b/plugins/btc.lua new file mode 100644 index 0000000..23d5091 --- /dev/null +++ b/plugins/btc.lua @@ -0,0 +1,36 @@ + +function getBTCEUR(eur) + -- Do request on bitcoinaverage, the final / is critical! + local res,code = https.request("https://api.bitcoinaverage.com/ticker/global/EUR/") + + if code~= 200 then return nil end + local data = json:decode(res) + + -- Easy, it's right there + text = "BTC/EUR"..'\n'..'Buy: '..data.ask..'\n'..'Sell: '..data.bid + + -- If we have a number as second parameter, calculate the bitcoin amount + if eur~=nil then + btc = tonumber(eur) / tonumber(data.ask) + text = text.."\n EUR "..eur.." = BTC "..btc + end + return text +end + +function run(msg, matches) + if matches[1] == "!btc" then + return getBTCEUR(nil) + end + return getBTCEUR(matches[1]) +end + +return { + description = "BTCEUR market value", + usage = "!btc [EUR]", + patterns = { + "^!btc$", + "^!btc (%d+[%d%.]*)$", + }, + run = run +} + From d2974d295f847031e6ad853b32f40704ae5cc324 Mon Sep 17 00:00:00 2001 From: Marcel van der Boom Date: Fri, 28 Nov 2014 22:59:52 +0100 Subject: [PATCH 2/2] Add USD support --- plugins/btc.lua | 48 +++++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/plugins/btc.lua b/plugins/btc.lua index 23d5091..2236355 100644 --- a/plugins/btc.lua +++ b/plugins/btc.lua @@ -1,36 +1,50 @@ -function getBTCEUR(eur) +function getBTCX(amount,currency) -- 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 local data = json:decode(res) - + -- 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 eur~=nil then - btc = tonumber(eur) / tonumber(data.ask) - text = text.."\n EUR "..eur.." = BTC "..btc + if amount~=nil then + btc = tonumber(amount) / tonumber(data.ask) + text = text.."\n "..currency .." "..amount.." = BTC "..btc end return text end function run(msg, matches) - if matches[1] == "!btc" then - return getBTCEUR(nil) - end - return getBTCEUR(matches[1]) + vardump(matches) + 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 + return getBTCX(amt,cur) end return { - description = "BTCEUR market value", - usage = "!btc [EUR]", - patterns = { + description = "Bitcoin global average market value (in EUR or USD)", + usage = "!btc [EUR|USD] [amount]", + patterns = { "^!btc$", - "^!btc (%d+[%d%.]*)$", - }, - run = run + "^!btc ([Ee][Uu][Rr])$", + "^!btc ([Uu][Ss][Dd])$", + "^!btc (EUR) (%d+[%d%.]*)$", + "^!btc (USD) (%d+[%d%.]*)$" + }, + run = run }