Skip to content

Commit c9c1d6b

Browse files
authored
Merge pull request #454 from Ryuno-Ki/weather-api-forecast
feat: show daily forecast
2 parents 68ddbd9 + 27727f0 commit c9c1d6b

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

weather-api-widget/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ following config parameters:
1818
| units | `metric` | `metric` for celsius, `imperial` for fahrenheit |
1919
| icon_pack_name | `weather-underground-icons` | Name of the icon pack, could be `weather-underground-icon` or `VitalyGorbachev` or create your own, more details below |
2020
| icons_extension | `.png` | File extension of icons in the pack |
21+
| show_forecast | false | Show forecast for next three days |
2122
| timeout | 120 | How often in seconds the widget refreshes |
2223

2324
### Icons:

weather-api-widget/weather.lua

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,16 @@ local function worker(user_args)
174174
local both_units_widget = args.both_units_widget or false
175175
local icon_pack_name = args.icons or 'weather-underground-icons'
176176
local icons_extension = args.icons_extension or '.png'
177+
local show_forecast = args.show_forecast or false
177178
local timeout = args.timeout or 120
178179

179180
local ICONS_DIR = WIDGET_DIR .. '/icons/' .. icon_pack_name .. '/'
180-
local weather_api =
181-
('https://api.weatherapi.com/v1/current.json' ..
181+
-- Forecast endpoint includes current. I could map show_forecast to days here.
182+
-- Currently overfetching but only showing when opting in.
183+
local weather_api =
184+
('https://api.weatherapi.com/v1/forecast.json' ..
182185
'?q=' .. coordinates[1] .. ',' .. coordinates[2] .. '&key=' .. api_key ..
183-
'&units=' .. units .. '&lang=' .. LANG)
186+
'&units=' .. units .. '&lang=' .. LANG .. '&days=3')
184187

185188
weather_widget = wibox.widget {
186189
{
@@ -301,6 +304,66 @@ local function worker(user_args)
301304
end
302305
}
303306

307+
local forecast_widget = {
308+
forced_width = 300,
309+
layout = wibox.layout.flex.horizontal,
310+
update = function(self, forecast)
311+
local count = #self
312+
for i = 0, count do self[i] = nil end
313+
for i, day in ipairs(forecast) do
314+
-- Free plan allows forecast for up to three days, each with hours
315+
if i > 3 then break end
316+
local day_forecast = wibox.widget {
317+
{
318+
text = os.date('%a', tonumber(day.date_epoch)),
319+
align = 'center',
320+
font = font_name .. ' 9',
321+
widget = wibox.widget.textbox
322+
},
323+
{
324+
{
325+
{
326+
image = ICONS_DIR .. icon_map[day.day.condition.code] .. icons_extension,
327+
resize = true,
328+
forced_width = 48,
329+
forced_height = 48,
330+
widget = wibox.widget.imagebox
331+
},
332+
align = 'center',
333+
layout = wibox.container.place
334+
},
335+
{
336+
text = day.day.condition.text,
337+
font = font_name .. ' 8',
338+
align = 'center',
339+
forced_height = 50,
340+
widget = wibox.widget.textbox
341+
},
342+
layout = wibox.layout.fixed.vertical
343+
},
344+
{
345+
{
346+
text = gen_temperature_str(day.day.mintemp_c, '%.0f', false, units),
347+
align = 'center',
348+
font = font_name .. ' 9',
349+
widget = wibox.widget.textbox
350+
},
351+
{
352+
text = gen_temperature_str(day.day.maxtemp_c, '%.0f', false, units),
353+
align = 'center',
354+
font = font_name .. ' 9',
355+
widget = wibox.widget.textbox
356+
},
357+
layout = wibox.layout.fixed.vertical
358+
},
359+
spacing = 8,
360+
layout = wibox.layout.fixed.vertical
361+
}
362+
table.insert(self, day_forecast)
363+
end
364+
end
365+
}
366+
304367
local function update_widget(widget, stdout, stderr)
305368
if stderr ~= '' then
306369
if not warning_shown then
@@ -347,6 +410,12 @@ local function worker(user_args)
347410
layout = wibox.layout.fixed.vertical
348411
}
349412

413+
414+
if show_forecast then
415+
forecast_widget:update(result.forecast.forecastday)
416+
table.insert(final_widget, forecast_widget)
417+
end
418+
350419
weather_popup:setup({
351420
{
352421
final_widget,

0 commit comments

Comments
 (0)