-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcurrency-cbr-search.lua
More file actions
70 lines (62 loc) · 1.62 KB
/
currency-cbr-search.lua
File metadata and controls
70 lines (62 loc) · 1.62 KB
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
-- name = "Курс валют ЦБ"
-- description = "Cкрипт отображает курс валюты ЦБ России на заданную дату (usd 30 3 22)"
-- data_source = "https://www.cbr.ru/"
-- type = "search"
-- lang = "ru"
-- author = "Andrey Gavrilov"
-- version = "1.0"
-- modules
local xml = require "xml"
local md_color = require "md_colors"
-- constants
local red = md_colors.red_500
-- variables
local cur = ""
local dat = ""
local val = 0
function on_search(inp)
val = 0
local c,d,m,y = inp:match("^(%a%a%a)%s?(%d?%d?)%s?(%d?%d?)%s?(%d?%d?%d?%d?)$")
if c == nil then return end
cur = c:upper()
local t = os.date("*t")
if d == "" then
d = t.day
end
if m == "" then
m = t.month
end
if y == "" then
y = t.year
elseif y%100 > 95 then
y = 1900 + y%100
else
y = 2000 + y%100
end
dat = os.date("%d.%m.%Y",os.time{day=d,month=m,year=y})
search:show_buttons({"Курс "..cur.." "..dat},{red})
end
function on_click()
if val == 0 then
http:get("https://www.cbr.ru/scripts/XML_daily.asp?date_req="..dat:replace("%.","/"))
return false
else
system:to_clipboard(val)
return true
end
end
function on_network_result(res)
local t = xml:parse(res)
for i,v in ipairs(t.ValCurs.Valute) do
if v.CharCode:value() == cur then
search:show_buttons({t.ValCurs["@Date"],v.Nominal:value().." "..v.CharCode:value().." = "..v.Value:value():replace(",",".").." RUB"},{red,red})
val = v.Value:value()
return
end
end
search:show_buttons({"Нет данных по валюте "..cur},{red})
end
function on_long_click()
system:to_clipboard(val)
return true
end