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

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