feat: safeguard against invalid JSON
It appears that OpenWeatherMap sometimes sent out bad JSON. Learn from the lesson and do better in this PR. Signed-off-by: André Jaenisch <andre.jaenisch@posteo.de>
This commit is contained in:
parent
39d72e4c0d
commit
c734c05614
|
@ -180,7 +180,7 @@ local function worker(user_args)
|
||||||
local timeout = args.timeout or 120
|
local timeout = args.timeout or 120
|
||||||
|
|
||||||
local ICONS_DIR = WIDGET_DIR .. '/icons/' .. icon_pack_name .. '/'
|
local ICONS_DIR = WIDGET_DIR .. '/icons/' .. icon_pack_name .. '/'
|
||||||
local weather_api =
|
local weather_api =
|
||||||
('https://api.weatherapi.com/v1/current.json' ..
|
('https://api.weatherapi.com/v1/current.json' ..
|
||||||
'?q=' .. coordinates[1] .. ',' .. coordinates[2] .. '&key=' .. api_key ..
|
'?q=' .. coordinates[1] .. ',' .. coordinates[2] .. '&key=' .. api_key ..
|
||||||
'&units=' .. units .. '&lang=' .. LANG)
|
'&units=' .. units .. '&lang=' .. LANG)
|
||||||
|
@ -322,13 +322,24 @@ local function worker(user_args)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if string.match(stdout, '<') ~= nil then
|
||||||
|
if not warning_shown then
|
||||||
|
warning_shown = true
|
||||||
|
widget:is_ok(false)
|
||||||
|
tooltip:add_to_object(widget)
|
||||||
|
|
||||||
|
widget:connect_signal('mouse::enter', function() tooltip.text = stdout end)
|
||||||
|
end
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
warning_shown = false
|
warning_shown = false
|
||||||
tooltip:remove_from_object(widget)
|
tooltip:remove_from_object(widget)
|
||||||
widget:is_ok(true)
|
widget:is_ok(true)
|
||||||
|
|
||||||
local result = json.decode(stdout)
|
local result = json.decode(stdout)
|
||||||
widget:set_image(ICONS_DIR .. icon_map[result.current.condition.code] .. icons_extension)
|
widget:set_image(ICONS_DIR .. icon_map[result.current.condition.code] .. icons_extension)
|
||||||
-- TODO: if units isn't "metric", read temp_f instead
|
-- TODO: if units isn't "metric", read temp_f instead
|
||||||
widget:set_text(gen_temperature_str(result.current.temp_c, '%.0f', both_units_widget, units))
|
widget:set_text(gen_temperature_str(result.current.temp_c, '%.0f', both_units_widget, units))
|
||||||
|
|
||||||
current_weather_widget:update(result.current)
|
current_weather_widget:update(result.current)
|
||||||
|
|
Loading…
Reference in New Issue