| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
m> |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
local pcall = pcall |
| 23 |
local helpers = require("vicious.helpers") |
| 24 |
local spawn = require("vicious.spawn") |
| 25 |
|
| 26 |
local success, json = pcall(require, "cjson") |
| 27 |
if not success then |
| 28 |
json = require("json") |
| 29 |
end |
| 30 |
|
| 31 |
local string = { |
| 32 |
sub = string.sub, |
| 33 |
upper = string.upper, |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
local btc_all = {} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
function btc_all.async(format, warg, callback) |
| 45 |
|
| 46 |
if not warg then warg = "usd" end |
| 47 |
|
| 48 |
local btc = { ["{price}"] = "N/A" } |
| 49 |
local currency_code = string.upper(warg) |
| 50 |
local url = "https://api.coindesk.com/v1/bpi/currentprice/" .. currency_code .. ".json" |
| 51 |
local cmd = "curl "..helpers.shellquote(url) |
| 52 |
|
| 53 |
|
| 54 |
local function parse(response) |
| 55 |
|
| 56 |
local status, data = pcall(function() return json.decode(response) end) |
| 57 |
if not status or not data then |
| 58 |
return btc |
| 59 |
end |
| 60 |
|
| 61 |
btc["{price}"] = string.sub(data["bpi"][currency_code]["rate"], 0, -3) |
| 62 |
return btc |
| 63 |
end |
| 64 |
|
| 65 |
|
| 66 |
spawn.easy_async(cmd, function(stdout) callback(parse(stdout)) end) |
| 67 |
end |
| 68 |
|
| 69 |
|
| 70 |
return helpers.setasyncall(btc_all) |