Bitcoin Price Widget

From awesome
Jump to: navigation, search

MtGox Textbox[edit]

This widget places the current MtGox bitcoin price in USD as a widget in your taskbar. It refreshes every 5 minutes. Hope you enjoy.

You'll need to put [[1]] somewhere where bitcoin_widget can find it.

 -- Builds an mtgox backed ticker for the bitcoin price
 json = require ("dkjson")
 vicious = require("vicious")
 
 local price = "n/a"
 
 -- call the Bitcoin API, expecting JSON in return
 local function get_json()
    bitcoin_url = "https://mtgox.com/api/1/BTCUSD/ticker"
    -- get the visitors and actions for today
    command = 'curl -m 5 -s "'..bitcoin_url..'"'
    f = io.popen(command)
    if (not f) then return 0 end
    -- store the returned json as a string
    return f:read("*all")
 end
 
 -- parse the JSON from Bitcoin
 local function parse_json(j)
    local obj, pos, err = json.decode (j, 1, nil)
    if err then
       return "Err"
    else
       return obj["return"] -- obj["return"]["sell"]["display_short"]
    end
 end
 
 -- the widget updater worker
 function worker(format, warg)
    dat = parse_json(get_json())
    if type(dat) == type("") then
       return string.format(format, dat, dat)
    else
       return string.format(format, dat["buy"]["display_short"],
                                    dat["sell"]["display_short"])
    end
 end
 
 -- init the widget
 bitcoinwidget = wibox.widget.textbox()
 vicious.register(bitcoinwidget, worker, 'BTC %s ↓↑ %s ', 10)

Hope someone finds this useful. Code adapted from [[2]]

bitcoinaverage.com Textbox[edit]

Here's a version which

  • uses latest price from bitcoinaverage.com instead of emptygox
  • uses EUR instead of USD
  • uses mBTC instead of BTC
  • works with awesome 3.4 on Ubuntu 13.04
 -- Builds a BitcoinAverage backed ticker for the bitcoin price
 json = require ("dkjson")
 vicious = require("vicious")
 
 local price = "n/a"
 
 -- call the BitcoinAverage API, expecting JSON in return
 local function get_json()
    bitcoin_url = "https://api.bitcoinaverage.com/ticker/global/EUR/"
    -- get the visitors and actions for today
    command = 'curl -m 5 -s "'..bitcoin_url..'"'
    f = io.popen(command)
    if (not f) then return 0 end
    -- store the returned json as a string
    return f:read("*all")
 end
 
 -- parse the JSON from Bitcoin
 local function parse_json(j)
    local obj, pos, err = json.decode (j, 1, nil)
    if err then
       return "Err"
    else
       return obj
    end
 end
 
 -- the widget updater worker
 function worker(format, warg)
    dat = parse_json(get_json())
    if type(dat) == type("") then
       return string.format(format, dat, dat)
    else
       return string.format(format, 
                            math.floor(dat["bid"]) / 1000.0,
                            math.floor(dat["ask"]) / 1000.0)
    end
 end
 
 -- init the widget
 bitcoinwidget = widget({type="textbox", name="bitcoinwidget", align="right"})
 vicious.register(bitcoinwidget, worker, '€/m฿ %s ↓↑ %s ', 10)


blockchain.info Linegraph and Textbox[edit]

The following widget uses the awesome 3.5 and the Blingbling widget library to plot a line graph of the BTC price in USD.

BtcLinegraphAndTextbox.png

Create the widgets

local bb = require("blingbling") -- for fancy graphs
-- the widget updater worker
function getPrice()
   bitcoin_url = "https://blockchain.info/q/24hrprice"
   command = 'curl -m 2 -s "'..bitcoin_url..'"'
   f = io.popen(command)
   if (f) then
     price = f:read()
     f:close()
     return tonumber(price)
   else
     return nil
   end
end
-- convert prices to plot values between 0 and 1
function getPlotVal(min,max,price)
  b = -min/(max-min)
  m = 1/(max-min)
  return m*price+b
end
-- init the widget
btcPriceText = wibox.widget.textbox()
btcPriceGraph = bb.line_graph({width = 300,
                               show_text = true,
                               label = "BTC(USD)",
                               rounded_size = 0.3 
                             })
maxBtcPrice = 0 
minBtcPrice = math.maxinteger
avgBtcPrice = 0 
btcTimer = timer({ timeout = 5 })
btcTimer:connect_signal("timeout",
function()
  price = getPrice()
  if ( price ~= nil ) then
    if ( price > maxBtcPrice ) then
      maxBtcPrice = price
    end 
    if ( price < minBtcPrice ) then
      minBtcPrice = price
    end 
    val = getPlotVal(minBtcPrice,maxBtcPrice,price)
    s = string.format("Now %.2f Max %.2f Min %.2f", price, maxBtcPrice,
minBtcPrice)
    btcPriceText:set_text(s)
    -- line_graph wants the values to be between 0 and 1
    btcPriceGraph:add_value(val)
  end 
end)
btcTimer:start()

Then add the widgets to the layout.

    local left_layout = wibox.layout.fixed.horizontal()
    left_layout:add(btcPriceGraph)
    left_layout:add(btcPriceText)