introduce helpers.uri (curl replacement) & migrate widget/weather to it

- new dependency: needs libsoup gi installation (smaller than gvfs)
- [breaking] weather: `(current/|forecast)_call` changed to `(current|forecast)_uri`
- [breaking] weather: use scoped (non-global) `now.current` and `now.forecast` to avoid `weather_now`
  clobbering.  see #550 for more details.
- weather: display something useful by default
- weather: render the hour of the forecast by default since the default
  forecast is 5 days by 3h increments
- weather: add degree symbol to temperatures by default
This commit is contained in:
razamatan 2021-09-14 14:34:48 -07:00
parent 88f5a8abd2
commit cf6323e117
2 changed files with 50 additions and 27 deletions

View File

@ -7,6 +7,7 @@
local spawn = require("awful.spawn") local spawn = require("awful.spawn")
local timer = require("gears.timer") local timer = require("gears.timer")
local gdebug = require("gears.debug")
local debug = require("debug") local debug = require("debug")
local io = { lines = io.lines, local io = { lines = io.lines,
open = io.open } open = io.open }
@ -14,6 +15,7 @@ local pairs = pairs
local rawget = rawget local rawget = rawget
local tsort = table.sort local tsort = table.sort
local unpack = unpack or table.unpack -- lua 5.1 retro-compatibility local unpack = unpack or table.unpack -- lua 5.1 retro-compatibility
local lgi = require("lgi")
-- Lain helper functions for internal use -- Lain helper functions for internal use
-- lain.helpers -- lain.helpers
@ -132,6 +134,25 @@ end
-- }}} -- }}}
-- {{{ Network functions
-- fetch a http uri using lgi.Soup
function helpers.uri(uri, callback, error_handler)
error_handler = error_handler or gdebug.print_error
local ss = lgi.Soup.Session()
local msg = lgi.Soup.Message.new('GET', uri)
ss:send_and_read_async(msg, lgi.GLib.PRIORITY_DEFAULT, nil, function(_, resp, _)
local buf, err = ss:send_and_read_finish(resp)
if err then
error_handler(err)
elseif buf then
callback(buf:get_data())
end
end)
end
-- }}}
-- {{{ A map utility -- {{{ A map utility
helpers.map_table = {} helpers.map_table = {}

View File

@ -17,17 +17,18 @@ local type = type
local tonumber = tonumber local tonumber = tonumber
-- OpenWeatherMap -- OpenWeatherMap
-- current weather and X-days forecast -- current weather and 5d/3h forecast
-- lain.widget.weather -- lain.widget.weather
local function factory(args) local function factory(args)
args = args or {} args = args or {}
local weather = { widget = args.widget or wibox.widget.textbox() } -- weather.now will hold the 'current' and 'forecast' state
local weather = { widget = args.widget or wibox.widget.textbox(), now = {} }
local APPID = args.APPID -- mandatory api key local APPID = args.APPID -- mandatory api key
local timeout = args.timeout or 900 -- 15 min local timeout = args.timeout or 900 -- 15 min
local current_call = args.current_call or "curl -s 'https://api.openweathermap.org/data/2.5/weather?lat=%s&lon=%s&APPID=%s&units=%s&lang=%s'" local current_uri = args.current_uri or "https://api.openweathermap.org/data/2.5/weather?lat=%s&lon=%s&APPID=%s&units=%s&lang=%s"
local forecast_call = args.forecast_call or "curl -s 'https://api.openweathermap.org/data/2.5/forecast?lat=%s&lon=%s&APPID=%s&cnt=%s&units=%s&lang=%s'" local forecast_uri = args.forecast_uri or "https://api.openweathermap.org/data/2.5/forecast?lat=%s&lon=%s&APPID=%s&cnt=%s&units=%s&lang=%s"
local lat = args.lat or 0 -- placeholder local lat = args.lat or 0 -- placeholder
local lon = args.lon or 0 -- placeholder local lon = args.lon or 0 -- placeholder
local units = args.units or "metric" local units = args.units or "metric"
@ -37,15 +38,15 @@ local function factory(args)
local notification_preset = args.notification_preset or {} local notification_preset = args.notification_preset or {}
local notification_text_fun = args.notification_text_fun or local notification_text_fun = args.notification_text_fun or
function (wn) function (wn)
local day = os.date("%a %d", wn["dt"]) local day = os.date("%a %d %H:%M", wn["dt"])
local temp = math.floor(wn["main"]["temp"]) local temp = math.floor(wn["main"]["temp"])
local desc = wn["weather"][1]["description"] local desc = wn["weather"][1]["description"]
return string.format("<b>%s</b>: %s, %d ", day, desc, temp) return string.format("<b>%s</b>: %s, %d°", day, desc, temp)
end end
local weather_na_markup = args.weather_na_markup or " N/A " local weather_na_markup = args.weather_na_markup or " N/A "
local followtag = args.followtag or false local followtag = args.followtag or false
local showpopup = args.showpopup or "on" local showpopup = args.showpopup or "on"
local settings = args.settings or function() end local settings = args.settings or function(_, _) end
weather.widget:set_markup(weather_na_markup) weather.widget:set_markup(weather_na_markup)
weather.icon_path = icons_path .. "na.png" weather.icon_path = icons_path .. "na.png"
@ -88,18 +89,17 @@ local function factory(args)
end end
function weather.forecast_update() function weather.forecast_update()
local cmd = string.format(forecast_call, lat, lon, APPID, cnt, units, lang) local uri = string.format(forecast_uri, lat, lon, APPID, cnt, units, lang)
helpers.uri(uri, function(f)
local forecast, _, err = json.decode(f, 1, nil)
helpers.async(cmd, function(f) if not err and type(weather.forecast) == "table" and tonumber(forecast["cod"]) == 200 then
local err weather.now.forecast = forecast
weather_now, _, err = json.decode(f, 1, nil)
if not err and type(weather_now) == "table" and tonumber(weather_now["cod"]) == 200 then
weather.notification_text = "" weather.notification_text = ""
for i = 1, weather_now["cnt"], math.floor(weather_now["cnt"] / cnt) do for i = 1, forecast["cnt"], math.floor(forecast["cnt"] / cnt) do
weather.notification_text = weather.notification_text .. weather.notification_text = weather.notification_text ..
notification_text_fun(weather_now["list"][i]) notification_text_fun(forecast["list"][i])
if i < weather_now["cnt"] then if i < forecast["cnt"] then
weather.notification_text = weather.notification_text .. "\n" weather.notification_text = weather.notification_text .. "\n"
end end
end end
@ -108,17 +108,18 @@ local function factory(args)
end end
function weather.update() function weather.update()
local cmd = string.format(current_call, lat, lon, APPID, units, lang) local uri = string.format(current_uri, lat, lon, APPID, units, lang)
helpers.uri(uri, function(f)
local current, _, err = json.decode(f, 1, nil)
helpers.async(cmd, function(f) if not err and type(current) == "table" and tonumber(current["cod"]) == 200 then
local err weather.now.current = current
weather_now, _, err = json.decode(f, 1, nil) local sunrise = tonumber(current["sys"]["sunrise"])
local sunset = tonumber(current["sys"]["sunset"])
if not err and type(weather_now) == "table" and tonumber(weather_now["cod"]) == 200 then local icon = current["weather"][1]["icon"]
local sunrise = tonumber(weather_now["sys"]["sunrise"])
local sunset = tonumber(weather_now["sys"]["sunset"])
local icon = weather_now["weather"][1]["icon"]
local loc_now = os.time() local loc_now = os.time()
local city = current["name"]
local temp = current["main"]["temp"]
if sunrise <= loc_now and loc_now <= sunset then if sunrise <= loc_now and loc_now <= sunset then
icon = string.gsub(icon, "n", "d") icon = string.gsub(icon, "n", "d")
@ -127,8 +128,8 @@ local function factory(args)
end end
weather.icon_path = icons_path .. icon .. ".png" weather.icon_path = icons_path .. icon .. ".png"
widget = weather.widget weather.widget:set_markup(string.format(" %s %d° ", city, temp))
settings() settings(weather.widget, weather.now)
else else
weather.icon_path = icons_path .. "na.png" weather.icon_path = icons_path .. "na.png"
weather.widget:set_markup(weather_na_markup) weather.widget:set_markup(weather_na_markup)
@ -138,6 +139,7 @@ local function factory(args)
end) end)
end end
if showpopup == "on" then weather.attach(weather.widget) end if showpopup == "on" then weather.attach(weather.widget) end
weather.timer = helpers.newtimer("weather-" .. lat .. ":" .. lon, timeout, weather.update, false, true) weather.timer = helpers.newtimer("weather-" .. lat .. ":" .. lon, timeout, weather.update, false, true)