From c6688a87ae38c2a483731ec9f4da072f2132c725 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergiusz=20=27q3k=27=20Baza=C5=84ski?= Date: Mon, 18 Nov 2013 09:56:45 +0100 Subject: [PATCH] Bitcoin plugin. --- plugins/bitcoin.lua | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 plugins/bitcoin.lua diff --git a/plugins/bitcoin.lua b/plugins/bitcoin.lua new file mode 100644 index 0000000..be82ea3 --- /dev/null +++ b/plugins/bitcoin.lua @@ -0,0 +1,44 @@ +local API = 'https://api.bitcoincharts.com/v1/markets.json' +local GetRates = function() + local Body, Code, Headers = https.request(API) + if Code ~= 200 then + error(string.format("Status code returned: %i", Code)) + end + + local Data = json.decode.decode(Body) + if not Data then + error("Could not decode JSON.") + end + + local Rates = {} + for _, Market in pairs(Data) do + local Rate = {} + Rate.high = Market.high + Rate.low = Market.low + Rate.avg = Market.avg + Rate.currency = Market.currency + Rates[Market.symbol] = Rate + end + return Rates +end + +local FormatData = function(Rate, Key) + local Value = Rate[Key] + local Currency = Rate.currency + + return string.format("%.2f %s", Value, Currency) +end + +local DefaultRates = {'mtgoxUSD', 'mtgoxPLN', 'bitcurexPLN'} +plugin.AddCommand('btc', 0, function(Username, Channel) + local Rates = GetRates() + for _, Key in pairs(DefaultRates) do + local Rate = Rates[Key] + local High = FormatData(Rate, 'high') + local Low = FormatData(Rate, 'low') + local Avg = FormatData(Rate, 'avg') + local Data = string.format("%s: high: %s, low: %s, avg: %s", Key, High, Low, Avg) + Channel:Say(Data) + end +end, "Show exchange BTC rate on popular exchanges.") +