Bitcoin Price Widget

From awesome
Jump to: navigation, search

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]]


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)