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

0c3ae3ff8924eca05f2dc8ee9514a9db06d094b8
[etc/awesome.git] / widgets / contrib / acw.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
41     local function update()
42         btc = parse(get("https://coinbase.com/api/v1/prices/buy"))
43         if btc then
44             btc = tonumber(btc["subtotal"]["amount"])
45             btc_display = "$" .. btc
46         else
47             btc_display = "N/A"
48         end
49         doge = parse(get("http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=132"))
50         if doge and btc then
51             doge = tonumber(doge["return"]["markets"]["DOGE"]["lasttradeprice"])
52             doge_display = string.format("$%.4f", btc * doge)
53         else
54             doge_display = "N/A"
55         end
56         prices = btc_display .. " " ..  doge_display
57         prices_now = {}
58         prices_now.prices = prices
59
60         widget = acw.widget
61         settings()
62    end
63
64     newtimer("acw", timeout, update)
65
66     return acw.widget
67 end
68
69 return setmetatable(acw, { __call = function(_, ...) return worker(...) end })