Skip to content
This repository was archived by the owner on Jun 10, 2025. It is now read-only.

Multiple weather.lua API calls #10

@CCain67

Description

@CCain67

Hi! First, thanks for the amazing dotfiles!

Just wanted to point out, that unless I'm mistaken, the weather widget is making an API call for each piece of weather info every 3600 seconds, every time awful.spawn.easy_async_with_shell(get_weather_data,... is run by awesome. This can be consolidated to a single API call using something like:

local update_weather = function()
    awful.spawn.easy_async_with_shell(get_weather_data, function(out)
        local data = {}
        for word in string.gmatch(out, "%S+") do
            table.insert(data, word)
        end

        local icon = data[1] or "N/A"
        local feelslike = tonumber(data[2]) and math.floor(tonumber(data[2]) + 0.5) or "N/A"
        local temp = tonumber(data[3]) and math.floor(tonumber(data[3]) + 0.5) or "N/A"
        local city = data[4] or "N/A"
        local country = data[5] or "N/A"
        local humidity = data[6] or "N/A"
        local wind = tonumber(data[7]) and math.floor(tonumber(data[7]) + 0.5) or "N/A"
        local weather = data[8] or "N/A"

        city_text.markup = '<span color="' .. beautiful.fg_normal .. '" font="VictorMono Nerd Font Bold 12">' .. city .. '</span>'
        country_text.markup = '<span color="' .. beautiful.fg_normal .. '" font="VictorMono Nerd Font Bold 12">, OH</span>'
        weather_text.markup = '<span color="' .. beautiful.fg_normal .. '" font="VictorMono Nerd Font Bold 12">' .. weather .. '</span>'
        temparature_text.markup = '<span color="' .. beautiful.cyan .. '" font="VictorMono Nerd Font Bold 30">' .. temp .. '°F</span>'
        humidity_text.markup = '<span color="' .. beautiful.fg_normal .. '" font="VictorMono Nerd Font Bold 12">' .. humidity .. '%</span>'
        feels_like_text.markup = '<span color="' .. beautiful.fg_normal .. '" font="VictorMono Nerd Font Bold 12">' .. feelslike .. '°F</span>'
        wind_text.markup = '<span color="' .. beautiful.fg_normal .. '" font="VictorMono Nerd Font Bold 12">' .. wind .. ' mph</span>'

        local weather_image = iconpath .. 'weather-error.svg'
        if icon == '50d' then
            weather_image = iconpath .. 'dmist.svg'
        elseif icon == '01d' then
            weather_image = iconpath .. 'sun_icon.svg'
        elseif icon == '01n' then
            weather_image = iconpath .. 'moon_icon.svg'
        elseif icon == '02d' then
            weather_image = iconpath .. 'dfew_clouds.svg'
        elseif icon == '02n' then
            weather_image = iconpath .. 'nfew_clouds.svg'
        elseif icon == '03d' then
            weather_image = iconpath .. 'dscattered_clouds.svg'
        elseif icon == '03n' then
            weather_image = iconpath .. 'nscattered_clouds.svg'
        elseif icon == '04d' then
            weather_image = iconpath .. 'dbroken_clouds.svg'
        elseif icon == '04n' then
            weather_image = iconpath .. 'nbroken_clouds.svg'
        elseif icon == '09d' then
            weather_image = iconpath .. 'dshower_rain.svg'
        elseif icon == '09n' then
            weather_image = iconpath .. 'nshower_rain.svg'
        elseif icon == '010d' then
            weather_image = iconpath .. 'd_rain.svg'
        elseif icon == '010n' then
            weather_image = iconpath .. 'n_rain.svg'
        elseif icon == '011d' then
            weather_image = iconpath .. 'dthunderstorm.svg'
        elseif icon == '011n' then
            weather_image = iconpath .. 'nthunderstorm.svg'
        elseif icon == '013d' then
            weather_image = iconpath .. 'snow.svg'
        elseif icon == '013n' then
            weather_image = iconpath .. 'snow.svg'
        elseif icon == '50n' then
            weather_image = iconpath .. 'nmist.svg'
        elseif icon == '...' then
            weather_image = iconpath .. 'weather-error.svg'
        end
        weath_image.image = weather_image
    end)
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions