This commit is contained in:
Evgeny
2021-08-05 17:51:53 +03:00
parent 0735b38d94
commit 449b80c266
5 changed files with 190 additions and 45 deletions

View File

@@ -118,7 +118,8 @@ AIO Launcher включает в себя интерпретатор LuaJ 3.0.1
Стандартный API Lua расширен следующими функциями:
* `string:split(delimeter)` - разделяет строку с помощью указанного разделителя и возвращает таблицу;
* `has_value(table, value)` - проверяет есть ли в таблице указанное значение;
* `string:replace(regexp, string)` - заменяет текст, найденный регулярным выражением, на другой текст;
* `get_index(table, value)` - возвращает индекс элемента таблицы;
* `round(x, n)` - округляет число;
В комплект также входят:

2
bad-script2.lua Normal file
View File

@@ -0,0 +1,2 @@
ui:show_toast("Hello")

74
my_covid_widget.lua Normal file
View File

@@ -0,0 +1,74 @@
-- name = "Covid info #2"
-- description = "Статистика по заболеввниям и выздоровлениям COVID-19"
-- data_source = "https://covid19api.com/"
-- type = "widget"
-- author = "Evgeny Zobnin (zobnin@gmail.com)"
-- version = "1.0"
local api_url = "https://api.covid19api.com/summary"
local ccode = "RU"
local json = require "json"
local equals = "<font color=\""..ui:get_secondary_text_color().."\"> = </font>"
local tab = {}
function on_alarm()
http:get(api_url)
end
function on_network_result(result)
local t=json.decode(result)
if ccode == "" then
local dat = string.sub(t.Global.Date,1,10):gsub("-",".")
ui:set_title(ui:get_default_title().." "..dat)
local new = t.Global.NewConfirmed
local total = t.Global.TotalConfirmed
local newDeaths = t.Global.NewDeaths
local totalDeaths = t.Global.TotalDeaths
local newRecovered = t.Global.NewRecovered
local totalRecovered = t.Global.TotalRecovered
local newActives = new - newRecovered - newDeaths
local totalActives = total - totalRecovered - totalDeaths
tab = {
"<u>Заболевшие</u> | всего"..equals..comma_value(total).." | новые"..equals..comma_value(new),
"<u>Выздоровевшие</u> | всего"..equals..comma_value(totalRecovered).." | новые"..equals..comma_value(newRecovered),
"<u>Умершие</u> | всего"..equals..comma_value(totalDeaths).." | новые"..equals..comma_value(newDeaths),
"<u>Активные</u> | всего"..equals..comma_value(totalActives).." | новые"..equals..comma_value(newActives)
}
else
local i = 1
while 1 do
if t.Countries[i].CountryCode == ccode then
break
end
i = i + 1
end
local dat = string.sub(t.Countries[i].Date,1,10):gsub("-",".")
ui:set_title(ui:get_default_title().." "..ccode.." "..dat)
local new = t.Countries[i].NewConfirmed
local total = t.Countries[i].TotalConfirmed
local newDeaths = t.Countries[i].NewDeaths
local totalDeaths = t.Countries[i].TotalDeaths
local newRecovered = t.Countries[i].NewRecovered
local totalRecovered = t.Countries[i].TotalRecovered
local newActives = new - newRecovered - newDeaths
local totalActives = total - totalRecovered - totalDeaths
tab = {
"<u>Заболевшие</u> | всего"..equals..comma_value(total).." | новые"..equals..comma_value(new),
"<u>Выздоровевшие</u> | всего"..equals..comma_value(totalRecovered).." | новые"..equals..comma_value(newRecovered),
"<u>Умершие</u> | всего"..equals..comma_value(totalDeaths).." | новые"..equals..comma_value(newDeaths),
"<u>Активные</u> | всего"..equals..comma_value(totalActives).." | новые"..equals..comma_value(newActives)
}
end
ui:show_lines(tab)
end
function comma_value(n) -- credit http://richard.warburton.it
local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
return (left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right):gsub(","," ")
end
function on_resume()
ui:set_folding_flag(true)
ui:show_lines(tab)
end

112
my_currencies_widget.lua Normal file
View File

@@ -0,0 +1,112 @@
-- name = "Курсы валют #2"
-- description = "Виджет курсов валют. Нажмите на виджет, чтобы изменить дату"
-- data_source = "https://github.com/fawazahmed0/currency-api#readme"
-- type = "widget"
-- author = "Andrey Gavrilov"
-- version = "1.0"
local sx = require "pl.stringx"
local json = require "json"
local curs = {"usd:rub", "eur:rub", "eur:usd", "gbp:rub", "gbp:usd", "chf:usd", "aed:usd", "cny:rub", "inr:rub", "btc:usd"}
-- constants
local red_color = "#f44336"
local green_color = "#48ad47"
local text_color = ui:get_secondary_text_color()
local equals = "<font color=\""..text_color.."\"> = </font>"
-- global vars
local result_curr = ""
local tabl = {}
function on_resume()
ui:set_folding_flag(true)
ui:show_lines(tabl)
end
function on_alarm()
get_rates("latest", "curr")
end
function get_rates(loc_date,id)
http:get("https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/"..loc_date.."/currencies/usd.json",id)
end
function on_network_result_curr(result)
result_curr = result
local t = json.decode(result)
local dat = t.date
local prev_date = prev_date(dat)
get_rates(prev_date, "prev")
end
function on_network_result_prev(result)
tabl = create_tab(result)
ui:show_lines(tabl)
end
function on_click(idx)
ui:show_edit_dialog("Введите дату курсов", "Введите дату курсов в формате 2020.12.31. Пустое значение - текущая дата.")
end
function on_dialog_action(dat)
if dat == "" then dat = "latest" end
get_rates(dat:gsub('%.', "-"), "curr")
end
function prev_date(dat)
local prev_date = dat:split("-")
local prev_time = os.time{year=prev_date[1], month=prev_date[2], day=prev_date[3]} - (60*60*24)
return os.date("%Y-%m-%d", prev_time)
end
function create_tab(result)
local tab = {}
local t_c = json.decode(result_curr)
local t_p = json.decode(result)
-- set title
local dat = t_c.date
ui:set_title(ui:get_default_title().." "..dat:gsub("-", "."))
for idx = 1, #curs, 1 do
local cur = curs[idx]:split(":")
local rate_curr1 = t_c.usd[cur[1]]
local rate_curr2 = t_c.usd[cur[2]]
local rate_prev1 = t_p.usd[cur[1]]
local rate_prev2 = t_p.usd[cur[2]]
local rate_curr = round(rate_curr2/rate_curr1, 4)
local rate_prev = round(rate_prev2/rate_prev1, 4)
local change = round((rate_curr-rate_prev)/rate_prev*100,2)
local line = "1 "..string.upper(cur[1])..equals..rate_curr.." "..string.upper(cur[2])
line = line..get_formatted_change_text(change)
tab[idx] = line
end
return tab
end
-- utils --
function get_formatted_change_text(change)
if change > 0 then
return "<font color=\""..green_color.."\"><small> +"..change.."%</small></font>"
elseif change < 0 then
return "<font color=\""..red_color.."\"><small> "..change.."%</small></font>"
else
return "<font color=\""..text_color.."\"><small> "..change.."%</small></font>"
end
end
function round(x, n)
local n = math.pow(10, n or 0)
local x = x * n
if x >= 0 then x = math.floor(x + 0.5) else x = math.ceil(x - 0.5) end
return x / n
end

View File

@@ -1,44 +0,0 @@
function get_args_kv()
local keys = {}
local values = {}
local args = aio:get_args()
for idx = 1, #args, 1 do
local arg = sx.split(args[idx], ":")
keys[idx] = arg[1]
values[idx] = arg[2]
end
return { keys, values }
end
function string:split(sep)
if sep == nil then
sep = "%s"
end
local t={}
for str in string.gmatch(self, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
function round(x, n)
local n = math.pow(10, n or 0)
local x = x * n
if x >= 0 then x = math.floor(x + 0.5) else x = math.ceil(x - 0.5) end
return x / n
end
function table:get_index(val)
for index, value in ipairs(self) do
if value == val then
return index
end
end
return -1
end