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)
This commit is contained in:
0x5b 2017-11-22 17:22:52 +03:00
parent 2877baa7f4
commit 5eec03a3c3
3 changed files with 126 additions and 0 deletions

View File

@ -60,6 +60,18 @@ Supported platforms: Linux (required tools: `sysfs`)
**vicious.contrib.batproc** **vicious.contrib.batproc**
**vicious.contrib.btc**
Provides current Bitcoin price in any currency by code (
https://en.wikipedia.org/wiki/ISO_4217).
Supported platforms: Linux, FreeBSD (required tools: `curl`)
- Arguments:
* takes currency code, i.e. `"usd"`, `"rub"` and other. `"usd"` by
default
- Returns
* a table with string keys: {price}
**vicious.contrib.countfiles** **vicious.contrib.countfiles**
**vicious.widgets.cmus** **vicious.widgets.cmus**

57
contrib/btc_all.lua Normal file
View File

@ -0,0 +1,57 @@
---------------------------------------------------
-- Licensed under the GNU General Public License v2
-- * (c) 2017, 0x5b <dragen15051@gmail.com>
---------------------------------------------------
-- {{{ Grab environment
local setmetatable = setmetatable
local pcall = pcall
local helpers = require("vicious.helpers")
local spawn = require("awful.spawn")
local json = require("json")
local string = {
sub = string.sub,
upper = string.upper,
}
-- }}}
-- Btc: provides current bitcoin price
-- vicious.widgets.btc
local btc_linux = {}
-- {{ Bitcoin widget type
function btc_linux.async(format, warg, callback)
-- Default values
if not warg then warg = "usd" end
local btc = { ["{price}"] = "N/A" }
local currency_code = string.upper(warg)
local url = "https://api.coindesk.com/v1/bpi/currentprice/" .. currency_code .. ".json"
local cmd = "curl "..helpers.shellquote(url)
-- {{ Checking response
local function parse(response)
-- If 'response' is not json, 'json.decode' will return Error
local status, data = pcall(function() return json.decode(response) end)
if not status or not data then
return btc
end
btc["{price}"] = string.sub(data["bpi"][currency_code]["rate"], 0, -3)
return btc
end
-- }}
spawn.easy_async(cmd, function(stdout) callback(parse(stdout)) end)
end
-- }}}
-- {{ Bitcoin widget type
local function worker(format, warg)
btc_linux.async(format, warg, function(data) return data end)
end
-- }}}
return setmetatable(btc_linux, { __call = function(_, ...) return worker(...) end })

57
contrib/btc_linux.lua Normal file
View File

@ -0,0 +1,57 @@
---------------------------------------------------
-- Licensed under the GNU General Public License v2
-- * (c) 2017, 0x5b <dragen15051@gmail.com>
---------------------------------------------------
-- {{{ Grab environment
local setmetatable = setmetatable
local pcall = pcall
local helpers = require("vicious.helpers")
local spawn = require("awful.spawn")
local json = require("json")
local string = {
sub = string.sub,
upper = string.upper,
}
-- }}}
-- Btc: provides current bitcoin price
-- vicious.widgets.btc
local btc_linux = {}
-- {{ Bitcoin widget type
function btc_linux.async(format, warg, callback)
-- Default values
if not warg then warg = "usd" end
local btc = { ["{price}"] = "N/A" }
local currency_code = string.upper(warg)
local url = "https://api.coindesk.com/v1/bpi/currentprice/" .. currency_code .. ".json"
local cmd = "curl "..helpers.shellquote(url)
-- {{ Checking response
local function parse(response)
-- If 'response' is not json, 'json.decode' will return Error
local status, data = pcall(function() return json.decode(response) end)
if not status or not data then
return btc
end
btc["{price}"] = string.sub(data["bpi"][currency_code]["rate"], 0, -3)
return btc
end
-- }}
spawn.easy_async(cmd, function(stdout) callback(parse(stdout)) end)
end
-- }}}
-- {{ Bitcoin widget type
local function worker(format, warg)
btc_linux.async(format, warg, function(data) return data end)
end
-- }}}
return setmetatable(btc_linux, { __call = function(_, ...) return worker(...) end })