Skip to content

Commit 5eec03a

Browse files
committed
Bitcoin price widget.
Async receiving bitcoin price. Simple text widget. Provides a price in any currency by code: (https://en.wikipedia.org/wiki/ISO_4217)
1 parent 2877baa commit 5eec03a

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

contrib/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ Supported platforms: Linux (required tools: `sysfs`)
6060

6161
**vicious.contrib.batproc**
6262

63+
**vicious.contrib.btc**
64+
65+
Provides current Bitcoin price in any currency by code (
66+
https://en.wikipedia.org/wiki/ISO_4217).
67+
Supported platforms: Linux, FreeBSD (required tools: `curl`)
68+
69+
- Arguments:
70+
* takes currency code, i.e. `"usd"`, `"rub"` and other. `"usd"` by
71+
default
72+
- Returns
73+
* a table with string keys: {price}
74+
6375
**vicious.contrib.countfiles**
6476

6577
**vicious.widgets.cmus**

contrib/btc_all.lua

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---------------------------------------------------
2+
-- Licensed under the GNU General Public License v2
3+
-- * (c) 2017, 0x5b <[email protected]>
4+
---------------------------------------------------
5+
6+
-- {{{ Grab environment
7+
local setmetatable = setmetatable
8+
local pcall = pcall
9+
local helpers = require("vicious.helpers")
10+
local spawn = require("awful.spawn")
11+
local json = require("json")
12+
local string = {
13+
sub = string.sub,
14+
upper = string.upper,
15+
}
16+
-- }}}
17+
18+
19+
-- Btc: provides current bitcoin price
20+
-- vicious.widgets.btc
21+
local btc_linux = {}
22+
23+
24+
-- {{ Bitcoin widget type
25+
function btc_linux.async(format, warg, callback)
26+
-- Default values
27+
if not warg then warg = "usd" end
28+
29+
local btc = { ["{price}"] = "N/A" }
30+
local currency_code = string.upper(warg)
31+
local url = "https://api.coindesk.com/v1/bpi/currentprice/" .. currency_code .. ".json"
32+
local cmd = "curl "..helpers.shellquote(url)
33+
34+
-- {{ Checking response
35+
local function parse(response)
36+
-- If 'response' is not json, 'json.decode' will return Error
37+
local status, data = pcall(function() return json.decode(response) end)
38+
if not status or not data then
39+
return btc
40+
end
41+
42+
btc["{price}"] = string.sub(data["bpi"][currency_code]["rate"], 0, -3)
43+
return btc
44+
end
45+
-- }}
46+
47+
spawn.easy_async(cmd, function(stdout) callback(parse(stdout)) end)
48+
end
49+
-- }}}
50+
51+
-- {{ Bitcoin widget type
52+
local function worker(format, warg)
53+
btc_linux.async(format, warg, function(data) return data end)
54+
end
55+
-- }}}
56+
57+
return setmetatable(btc_linux, { __call = function(_, ...) return worker(...) end })

contrib/btc_linux.lua

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---------------------------------------------------
2+
-- Licensed under the GNU General Public License v2
3+
-- * (c) 2017, 0x5b <[email protected]>
4+
---------------------------------------------------
5+
6+
-- {{{ Grab environment
7+
local setmetatable = setmetatable
8+
local pcall = pcall
9+
local helpers = require("vicious.helpers")
10+
local spawn = require("awful.spawn")
11+
local json = require("json")
12+
local string = {
13+
sub = string.sub,
14+
upper = string.upper,
15+
}
16+
-- }}}
17+
18+
19+
-- Btc: provides current bitcoin price
20+
-- vicious.widgets.btc
21+
local btc_linux = {}
22+
23+
24+
-- {{ Bitcoin widget type
25+
function btc_linux.async(format, warg, callback)
26+
-- Default values
27+
if not warg then warg = "usd" end
28+
29+
local btc = { ["{price}"] = "N/A" }
30+
local currency_code = string.upper(warg)
31+
local url = "https://api.coindesk.com/v1/bpi/currentprice/" .. currency_code .. ".json"
32+
local cmd = "curl "..helpers.shellquote(url)
33+
34+
-- {{ Checking response
35+
local function parse(response)
36+
-- If 'response' is not json, 'json.decode' will return Error
37+
local status, data = pcall(function() return json.decode(response) end)
38+
if not status or not data then
39+
return btc
40+
end
41+
42+
btc["{price}"] = string.sub(data["bpi"][currency_code]["rate"], 0, -3)
43+
return btc
44+
end
45+
-- }}
46+
47+
spawn.easy_async(cmd, function(stdout) callback(parse(stdout)) end)
48+
end
49+
-- }}}
50+
51+
-- {{ Bitcoin widget type
52+
local function worker(format, warg)
53+
btc_linux.async(format, warg, function(data) return data end)
54+
end
55+
-- }}}
56+
57+
return setmetatable(btc_linux, { __call = function(_, ...) return worker(...) end })

0 commit comments

Comments
 (0)