-
Notifications
You must be signed in to change notification settings - Fork 12
/
currencies.lua
81 lines (72 loc) · 2.26 KB
/
currencies.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
local asyncshell = require('asyncshell')
local naughty = require('naughty')
local currencies = { invalid_time = 86400,
interested_in = { "USD", "EUR", "NOK", "UAH" } }
local cached_rates = {}
local function get_cached_rate(from, to)
if cached_rates[from] ~= nil then
return cached_rates[from][to]
end
end
local function cache_rate(from, to, rate)
if cached_rates[from] == nil then
cached_rates[from] = {}
end
if cached_rates[to] == nil then
cached_rates[to] = {}
end
cached_rates[from][to] = { rate = rate, time = os.time() }
cached_rates[to][from] = { rate = 1 / rate, time = os.time() }
end
local function build_req(from, to)
return "http://rate-exchange.appspot.com/currency?from=" .. from:upper() .. "&to=" .. to:upper()
end
local function request_rate_from_api(from, to)
local f = asyncshell.demand("curl '" .. build_req(from, to) .. "'", 2)
local res = f:read()
f:close()
if res ~= nil then
local rate = tonumber(res:match('.*"rate": ([^,]+).*'))
if rate then
cache_rate(from, to, rate)
return rate
end
end
end
function currencies.get_rate(from, to)
from = from:upper()
to = to:upper()
local cached = get_cached_rate(from, to)
if (cached ~= nil) and (os.time() - cached.time < currencies.invalid_time) then
return cached.rate
else
local rate = request_rate_from_api(from, to)
if not rate then
if cached then
naughty.notify({ title = string.format("Couldn't return rates for: %1, %2", from, to),
text = "Using cached values." })
rate = cached.rate
else
return nil
end
end
return rate
end
end
function currencies.recalc(str)
local val, curr = str:match("([%d%.]+)%s*(.+)")
val = tonumber(val)
curr = curr:upper()
local result = ""
for i, c in ipairs(currencies.interested_in) do
if c ~= curr then
result = string.format("%s%4.2f %s", result, val * currencies.get_rate(curr, c), c)
if i ~= #currencies.interested_in then
result = result .. "\n"
end
end
end
naughty.notify({ title = val .. " " .. curr,
text = result, timeout = 0 })
end
return currencies