diff --git a/contrib/README.md b/contrib/README.md index a9f9284..71d6ef8 100644 --- a/contrib/README.md +++ b/contrib/README.md @@ -60,6 +60,21 @@ Supported platforms: Linux (required tools: `sysfs`) **vicious.contrib.batproc** +**vicious.contrib.btc** + +Provides current Bitcoin price in any currency by code ( +https://en.wikipedia.org/wiki/ISO_4217). +Requires `curl` and one of the following json libraries: + - [lua-cjson](https://github.com/mpx/lua-cjson/)) + - [luajson](https://github.com/harningt/luajson/) +Supported platforms: Linux, FreeBSD + +- Arguments: + * takes currency code, i.e. `"usd"`, `"rub"` and other. `"usd"` by + default +- Returns + * a table with string keys: {price} + **vicious.contrib.countfiles** **vicious.widgets.cmus** diff --git a/contrib/btc_all.lua b/contrib/btc_all.lua new file mode 100644 index 0000000..5942711 --- /dev/null +++ b/contrib/btc_all.lua @@ -0,0 +1,56 @@ +--------------------------------------------------- +-- Licensed under the GNU General Public License v2 +-- * (c) 2017, 0x5b +--------------------------------------------------- + +-- {{{ Grab environment +local setmetatable = setmetatable +local pcall = pcall +local helpers = require("vicious.helpers") +local spawn = require("awful.spawn") + +local success, json = pcall(require, "cjson") +if not success then + json = require("json") +end + +local string = { + sub = string.sub, + upper = string.upper, +} +-- }}} + + +-- Btc: provides current bitcoin price +-- vicious.widgets.btc +local btc_all = {} + + +-- {{ Bitcoin widget type +function btc_all.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 +-- }}} + +return btc_all