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

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