From 642181089149caad8ec7f3cefc9080654ef6402a Mon Sep 17 00:00:00 2001 From: sriramsv Date: Mon, 20 Feb 2023 16:38:04 -0800 Subject: [PATCH] Add Sunrise sunset search --- community/sunrise-sunset-search.lua | 32 +++++++++++++++++++++++++++++ community/sunrise-sunset-widget.lua | 10 ++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 community/sunrise-sunset-search.lua diff --git a/community/sunrise-sunset-search.lua b/community/sunrise-sunset-search.lua new file mode 100644 index 0000000..9c2687f --- /dev/null +++ b/community/sunrise-sunset-search.lua @@ -0,0 +1,32 @@ +-- name = "Sun Info" +-- description = "Shows Sunrise Sunset at your location" +-- data_source = "https://api.sunrise-sunset.org/" +-- type = "search" +-- author = "Sriram S V" +-- version = "1.0" +-- prefix = "sun" +-- foldable = "false" + +local json = require "json" +local date = require "date" +md_colors = require("md_colors") + +function on_search(input) + local location=system:location() + get_sun_info(location) +end + +function on_network_result(result) + local t = json.decode(result) + local sunrise = date(t.results.sunrise):tolocal():fmt("%r") + local sunset = date(t.results.sunset):tolocal():fmt("%r") + + local lines = {"Sunrise: "..sunrise, "Sunset: "..sunset} + local colors = { md_colors.orange_400, md_colors.orange_800 } + search:show_lines(lines,colors) +end + +function get_sun_info(location) + url="https://api.sunrise-sunset.org/json?lat="..location[1].."&lng="..location[2].."&date=today&formatted=1" + http:get(url) +end \ No newline at end of file diff --git a/community/sunrise-sunset-widget.lua b/community/sunrise-sunset-widget.lua index 5f3129a..027191a 100644 --- a/community/sunrise-sunset-widget.lua +++ b/community/sunrise-sunset-widget.lua @@ -8,13 +8,12 @@ local json = require "json" local date = require "date" + function on_alarm() local location=system:location() - url="https://api.sunrise-sunset.org/json?lat="..location[1].."&lng="..location[2].."&date=today&formatted=1" - http:get(url) + get_sun_info(location) end - function on_network_result(result) local t = json.decode(result) local table = { @@ -22,4 +21,9 @@ function on_network_result(result) { "sunset:", date(t.results.sunset):tolocal():fmt("%r") }, } ui:show_table(table, 2) +end + +function get_sun_info(location) + url="https://api.sunrise-sunset.org/json?lat="..location[1].."&lng="..location[2].."&date=today&formatted=1" + http:get(url) end \ No newline at end of file -- 2.43.0