add many new scripts

This commit is contained in:
Evgeny
2022-08-22 10:24:40 +03:00
parent a6e081c988
commit 329c7abc70
24 changed files with 617 additions and 40 deletions

35
addons/battery-widget.lua Normal file
View File

@@ -0,0 +1,35 @@
-- name = "Battery info"
-- description = "Simple battery info widget"
-- author = "Evgeny Zobnin (zobnin@gmail.com)"
ticks = -1
function on_tick()
-- Update one time per 10 seconds
ticks = ticks + 1
if ticks % 10 ~= 0 then
return
end
ticks = 0
local batt_info = system:get_battery_info()
local batt_strings = stringify_table(batt_info)
local folded_str = "Battery: "..batt_info.percent.."% | "..batt_info.temp.."° | "..batt_info.voltage.." mV"
ui:show_lines(batt_strings, nil, folded_str)
end
function stringify_table(tab)
local new_tab = {}
for k,v in pairs(tab) do
table.insert(new_tab, capitalize(k)..": "..tostring(v))
end
return new_tab
end
function capitalize(string)
return string:gsub("^%l", string.upper)
end

View File

@@ -0,0 +1,71 @@
-- name = "Exchange rates"
-- description = "Shows currency rate by code"
-- data_source = "https://api.exchangerate.host"
-- type = "search"
-- author = "Evgeny Zobbin & Andrey Gavrilov"
-- version = "1.0"
-- modules
local json = require "json"
local md_color = require "md_colors"
-- constants
local host = "https://api.exchangerate.host"
local red = md_colors.red_500
local base_currency = system:get_currency()
-- variables
local req_currency = ""
local req_amount = 1
local result = 0
function on_search(inp)
req_currency = ""
req_amount = 1
result = 0
local a,c = inp:match("^(%d*)%s?(%a%a%a)$")
if c == nil then return end
req_currency = c:upper()
if get_index(supported, req_currency) == 0 then
return
end
if a ~= "" then
req_amount = a
end
search:show({"Exchange rate for "..req_amount.." "..req_currency},{red})
end
function on_click()
if result == 0 then
http:get(host.."/convert?from="..req_currency.."&to="..base_currency.."&amount="..req_amount)
return false
else
system:copy_to_clipboard(result)
return true
end
end
function on_long_click()
system:copy_to_clipboard(result)
return true
end
function on_network_result(res)
local tab = json.decode(res)
if tab.success then
search:show({tab.query.amount.." "..tab.query.from.." = "..tab.result.." "..tab.query.to}, {red})
else
search:show({"No data"},{red})
end
end
-- curl -s -XGET 'https://api.exchangerate.host/symbols?format=csv' | cut -d ',' -f 2 | grep -v code | sed 's/$/,/' | tr '\n' ' '
supported = {
"AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BMD", "BND", "BOB", "BRL", "BSD", "BTC", "BTN", "BWP", "BYN", "BZD", "CAD", "CDF", "CHF", "CLF", "CLP", "CNH", "CNY", "COP", "CRC", "CUC", "CUP", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GGP", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "ILS", "IMP", "INR", "IQD", "IRR", "ISK", "JEP", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRO", "MRU", "MUR", "MVR", "MWK", "MXN", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SEK", "SGD", "SHP", "SLL", "SOS", "SRD", "SSP", "STD", "STN", "SVC", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "UYU", "UZS", "VEF", "VES", "VND", "VUV", "WST", "XAF", "XAG", "XAU", "XCD", "XDR", "XOF", "XPD", "XPF", "XPT", "YER", "ZAR", "ZMW", "ZWL",
}

41
addons/dice-widget.lua Normal file
View File

@@ -0,0 +1,41 @@
-- name = "Dice widget"
-- description = "Roll the Dice"
-- author = "Evgeny Zobnin (zobnin@gmail.com)"
-- foldable = "false"
local dices = {
"fa:dice-one",
"fa:dice-two",
"fa:dice-three",
"fa:dice-four",
"fa:dice-five",
"fa:dice-six",
}
function on_resume()
ui:show_buttons{
"Roll the dice",
"fa:dice-six",
"fa:dice-six",
}
end
function on_click(idx)
if idx == 1 then
roll_dice(2)
roll_dice(3)
else
roll_dice(idx)
end
end
function roll_dice(idx)
local tab = {}
for i=1,10 do
table.insert(tab, dices[math.random(1, 6)])
end
morph:change_text_seq(idx, tab, 150)
end

23
addons/facts-widget.lua Normal file
View File

@@ -0,0 +1,23 @@
-- name = "Random facts"
-- description = "Radom useless facts"
-- data_source = "https://uselessfacts.jsph.pl/"
-- type = "widget"
-- author = "Evgeny Zobnin (zobnin@gmail.com)"
-- version = "1.0"
-- foldable = "false"
function on_alarm()
http:get("https://uselessfacts.jsph.pl/random.json?language=en")
end
function on_network_result(result)
text = ajson:get_value(result, "object string:text")
ui:show_lines{ text }
end
function on_click()
if text ~= nil then
system:copy_to_clipboard(text)
end
end

View File

@@ -0,0 +1,63 @@
-- name = "Google Translate"
-- description = "A search script that shows the translation of what you type in the search window"
-- data_source = "translate.google.com"
-- type = "search"
-- author = "Evgeny Zobnin"
-- version = "1.0"
local json = require "json"
local md_color = require "md_colors"
-- constants
local blue = md_colors.blue_500
local red = md_colors.red_500
local uri = "http://translate.googleapis.com/translate_a/single?client=gtx&sl=auto"
-- vars
local text_from = ""
local text_to = ""
function on_search(input)
text_from = input
text_to = ""
search:show_top({"Translate \""..input.."\""}, {blue})
end
function on_click()
if text_to == "" then
search:show_top({"Translating..."}, {blue})
request_trans(text_from)
return false
else
system:copy_to_clipboard(text_to)
return true
end
end
function request_trans(str)
http:get(uri.."&tl="..system:get_lang().."&dt=t&q="..str)
end
function on_network_result(result, code)
if code >= 200 and code < 300 then
decode_and_show(result)
else
search:show_top({"Server error"}, {red})
end
end
function decode_and_show(result)
local t = json.decode(result)
for i, v in ipairs(t[1]) do
text_to = text_to..v[1]
end
--local lang_from = t[3]
if text_to ~= "" then
search:show_top({text_to}, {blue})
end
end

View File

@@ -0,0 +1,97 @@
-- name = "Kodi Remote"
-- description = "Kodi multimedia center remote control widget"
-- type = "widget"
-- author = "Evgeny Zobnin (zobnin@gmail.com)"
-- arguments_help = "Enter the Kodi IP address and port in the following format: 192.168.0.102:8080"
-- version = "1.1"
local colors = require "md_colors"
local json = require "json"
-- constants
local media_type = "application/json"
local get_players_cmd = [[ { "id": 1, "jsonrpc": "2.0", "method": "Player.GetActivePlayers" } ]]
local play_cmd = [[ {"jsonrpc": "2.0", "method": "Player.PlayPause", "params": { "playerid": XXX }, "id": 1} ]]
local stop_cmd = [[ {"jsonrpc": "2.0", "method": "Player.Stop", "params": { "playerid": XXX }, "id": 1} ]]
local prev_cmd = [[ {"jsonrpc": "2.0", "method": "Player.GoTo", "params": {"playerid": XXX,"to":"previous"}, "id":1} ]]
local next_cmd = [[ {"jsonrpc": "2.0", "method": "Player.GoTo", "params": {"playerid": XXX,"to":"next"}, "id":1} ]]
local forward_cmd = [[ {"jsonrpc": "2.0", "method": "Player.Seek", "params": { "playerid": XXX, "value": { "seconds": 300 } }, "id": 1} ]]
local backward_cmd = [[ {"jsonrpc": "2.0", "method": "Player.Seek", "params": { "playerid": XXX, "value": { "seconds": -300 } }, "id": 1} ]]
local buttons = { "fa:fast-backward", "-5m", "fa:play", "fa:stop", "+5m", "fa:fast-forward", "Open Kore" }
local buttons_cmds = { prev_cmd, backward_cmd, play_cmd, stop_cmd, forward_cmd, next_cmd }
-- global vars
local url = nil
local curr_idx = nil
function on_resume()
if next(settings:get()) == nil then
ui:show_text("Tap to enter Kodi address")
return
end
init_url_from_settings()
ui:set_folding_flag(true)
ui:show_buttons(buttons)
end
function on_click(idx)
if next(settings:get()) == nil then
settings:show_dialog()
return
end
if (idx == 7) then
apps:launch("org.xbmc.kore")
return
end
curr_idx = idx
http:post(url, get_players_cmd, media_type, "players")
end
function on_network_result_players(result)
local parsed = json.decode(result)
if parsed.error ~= nil then
show_error(parsed)
return
end
-- not playing anything
if next(parsed.result) == nil then
return
end
local player_id = parsed.result[1].playerid
local cmd = buttons_cmds[curr_idx]:replace("XXX", player_id)
http:post(url, cmd, media_type, "cmd")
curr_idx = nil
end
function on_network_result_cmd(result)
local parsed = json.decode(result)
if parsed.error ~= nil then
show_error(parsed)
end
end
-- utils
function init_url_from_settings()
local ip_port = settings:get()[1]:split(":")
url = "http://"..ip_port[1]..":"..ip_port[2].."/jsonrpc"
end
function show_error(parsed)
ui:show_toast("Error "..parsed.error.code..": "..parsed.error.message)
end

View File

@@ -0,0 +1,30 @@
-- name = "Password generator"
-- description = "30-character password generator"
-- type = "search"
-- author = "Evgeny Zobnin (zobnin@gmail.com)"
local pass = ""
function on_search(str)
if str:lower():find(string.lower("password")) then
pass = gen_pass()
search:show{pass}
end
end
function gen_pass()
local chars = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V','W', 'X', 'Y', 'Z', 'a','b','c','d', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '!', '"','#','$','%','&',"'",'(',')','*','+',',','-','.','/','1','2','3','4','5','6','7','8','9','0',':',';','<','=','>','?','@','[',']','^','_','`','{','}'}
math.randomseed(os.clock()*100000000000)
pass = ""
for i=1, 30, 1 do
pass = pass..chars[math.random(1, #chars)]
end
return pass
end
function on_click()
system:copy_to_clipboard(pass)
end

View File

@@ -0,0 +1,81 @@
-- name = "Uptimerobot"
-- description = "Shows uptime information from uptimerobot.com. Needs API key."
-- data_source = "uptimerobot.com"
-- type = "widget"
-- author = "Evgeny Zobnin (zobnin@gmail.com)
-- version = "1.1"
-- arguments_help = "Enter your API key"
local json = require "json"
local md_colors = require "md_colors"
-- constants
local api_url = "https://api.uptimerobot.com/v2/"
local click_url = "https://uptimerobot.com/dashboard#mainDashboard"
local media_type = "application/x-www-form-urlencoded"
function on_alarm()
if (next(settings:get()) == nil) then
ui:show_text("Tap to enter API key")
return
end
local key = settings:get()[1]
local body = "api_key="..key.."&format=json"
http:post(api_url.."getMonitors", body, media_type)
end
function on_click()
if (next(settings:get()) == nil) then
settings:show_dialog()
else
system:open_browser(click_url)
end
end
function on_network_result(result, code)
if (code >= 400) then
ui:show_text("Error: "..code)
return
end
local parsed = json.decode(result)
if (parsed.stat ~= "ok") then
ui:show_text("Error: "..parsed.error.message)
return
end
local strings_tab = {}
for k,v in ipairs(parsed.monitors) do
strings_tab[k] = v.friendly_name..": "..format_status(v.status)
end
ui:show_table(table_to_tables(strings_tab, 2))
end
-- utils
function format_status(status)
local statuses = { "not checked", "up", "error", "error", "error", "error", "error", "seems down", "down" }
local status_colors = { "yellow_500", "green_500", "red_500", "red_500", "red_500", "red_500", "red_500", "orange_500", "red_500" }
return "<font color=\""..md_colors[status_colors[status]].."\">"..statuses[status].."</font>"
end
function table_to_tables(tab, num)
local out_tab = {}
local row = {}
for k,v in ipairs(tab) do
table.insert(row, v)
if k % num == 0 then
table.insert(out_tab, row)
row = {}
end
end
return out_tab
end

View File

@@ -0,0 +1,21 @@
-- name = "What to do?"
-- description = "Let's find you something to do"
-- data_source = "https://www.boredapi.com/"
-- type = "widget"
-- author = "Evgeny Zobnin (zobnin@gmail.com)"
-- version = "1.0"
-- foldable = "false"
function on_resume()
ui:show_text("Tell me what to do?")
end
function on_click()
system:vibrate(100)
http:get("http://www.boredapi.com/api/activity/")
end
function on_network_result(result)
text = ajson:get_value(result, "object string:activity")
ui:show_text(text)
end