]> git.madduck.net Git - etc/awesome.git/blob - widgets/contrib/ccurr.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:

moved acw/init to ccurr + code revision
[etc/awesome.git] / widgets / contrib / ccurr.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2014, Aaron Lebo                      
6                                                   
7 --]]
8
9 local newtimer = require("lain.helpers").newtimer
10 local wibox    = require("wibox")
11 local json     = require("dkjson")
12
13 -- Crypto currencies widget
14 -- lain.widgets.contrib.ccurr
15 local ccurr = {}
16
17 -- Currently gets
18 --   * BTC/USD
19 --   * DOGE/USD
20 -- using Coinbase and Cryptsy APIs.
21
22 -- requires   http://dkolf.de/src/dkjson-lua.fsl/home
23 -- based upon http://awesome.naquadah.org/wiki/Bitcoin_Price_Widget
24
25 local function get(url)
26     local f = io.popen('curl -m 5 -s "' .. url .. '"')
27     if not f then
28         return 0
29     else
30         local s = f:read("*all")
31         f:close()
32         return s
33     end
34 end
35
36 local function parse(j)
37     local obj, pos, err = json.decode(j, 1, nil)
38     if err then
39         return nil
40     else
41         return obj
42     end
43 end
44
45 local function worker(args)
46     local args     = args or {}
47     local timeout  = args.timeout  or 600
48     local btc_url  = args.btc_url  or "https://coinbase.com/api/v1/prices/buy"
49     local doge_url = args.doge_url or "http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=132"
50     local settings = args.settings or function() end
51
52     ccurr.widget = wibox.widget.textbox('')
53
54     local function update()
55         price_now = {
56             btc  = "N/A",
57             doge = "N/A"
58         }
59
60         btc  = parse(get(btc_url))
61         doge = parse(get(doge_url))
62
63         if btc and doge then
64             price_now.btc  = tonumber(btc["subtotal"]["amount"])
65             price_now.doge = tonumber(doge["return"]["markets"]["DOGE"]["lasttradeprice"])
66             price_now.doge = string.format("%.4f", price_now.btc * price_now.doge)
67         end
68
69         widget = ccurr.widget
70         settings()
71     end
72
73     newtimer("ccurr", timeout, update)
74
75     return ccurr.widget
76 end
77
78 return setmetatable(ccurr, { __call = function(_, ...) return worker(...) end })