]> git.madduck.net Git - etc/awesome.git/blob - .config/awesome/cryptocoin_widgets.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

factor cryptowidgets into separate module
[etc/awesome.git] / .config / awesome / cryptocoin_widgets.lua
1 local dkjson = require("lain.util").dkjson
2 local math = require("math")
3 local lain = require("lain")
4
5 local widgets = {}
6
7 local function poloniex_price(output, pair, prec)
8     local xc, pos, err = dkjson.decode(output, 1, nil)
9     if not prec then prec = 4 end
10     val = (xc and xc[pair]["last"]) or 0
11     val = math.floor(val*10^prec+0.5)/10^prec
12     return (not err and val) or "n/a"
13 end
14
15 widgets.eth_widget = lain.widget.watch({
16     cmd = "curl -m5 -s 'https://poloniex.com/public?command=returnTicker'",
17     timeout = 600,
18     settings = function()
19         widget:set_text(poloniex_price(output, 'BTC_ETH') .. " Ƀ/Ξ")
20     end
21 })
22
23 local function coindesk_price(output, base, prec)
24     local xc, pos, err = dkjson.decode(output, 1, nil)
25     if not prec then prec = 4 end
26     val = (xc and xc["bpi"][base]["rate_float"]) or 0
27     val = math.floor(val*10^prec+0.5)/10^prec
28     return (not err and val) or "n/a"
29 end
30
31 widgets.btc_widget = lain.widget.watch({
32     cmd = "curl -m5 -Ls 'https://api.coindesk.com/v1/bpi/currentprice/EUR.json'",
33     timeout = 600,
34     settings = function()
35         widget:set_text(coindesk_price(output, "EUR", 2) .. " €/Ƀ")
36     end
37 })
38
39 return widgets