weather : auto set coordinates from ip address

This commit is contained in:
BusyVar 2022-01-07 14:43:20 +01:00
parent 28f93c4943
commit 9d73a50ff8
2 changed files with 24 additions and 2 deletions

View File

@ -25,7 +25,7 @@ It is possible to customize widget by providing a table with all or some of the
| Name | Default | Description | | Name | Default | Description |
|---|---|---| |---|---|---|
| coordinates | Required | Table with two elements: latitude and longitude, e.g. `{46.204400, 6.143200}` | | coordinates | Required | Table with two elements: latitude and longitude, e.g. `{46.204400, 6.143200}` or "ipBased" if you want to set it automatically|
| api_key | Required | Get it [here](https://openweathermap.org/appid) | | api_key | Required | Get it [here](https://openweathermap.org/appid) |
| font_name | `beautiful.font:gsub("%s%d+$", "")` | **Name** of the font to use e.g. 'Play' | | font_name | `beautiful.font:gsub("%s%d+$", "")` | **Name** of the font to use e.g. 'Play' |
| both_units_widget | false | Show temperature in both units - '28°C (83°F) | | both_units_widget | false | Show temperature in both units - '28°C (83°F) |

View File

@ -123,6 +123,28 @@ local function uvi_index_color(uvi)
return '<span weight="bold" foreground="' .. color .. '">' .. uvi .. '</span>' return '<span weight="bold" foreground="' .. color .. '">' .. uvi .. '</span>'
end end
local function get_coordinates(val)
local coordinates = val
if val == "ipBased" then
local geofile = "/tmp/geo.json"
if not gears.filesystem.file_readable(geofile) then
local handle = os.execute("curl -H 'Accept:application/json' ipinfo.io/json -s -o " .. geofile)
if handle then
show_warning("fail to get coordinates from ip")
end
end
local file = io.open( geofile,"r")
local result = file:read("*all")
result = json.decode(result)
file:close()
coordinates = {}
for match in result.loc:gmatch("[^,%s]+") do
coordinates[#coordinates + 1] = tonumber(match)
end
end
return coordinates
end
local function worker(user_args) local function worker(user_args)
local args = user_args or {} local args = user_args or {}
@ -135,7 +157,7 @@ local function worker(user_args)
return return
end end
local coordinates = args.coordinates local coordinates = get_coordinates(args.coordinates)
local api_key = args.api_key local api_key = args.api_key
local font_name = args.font_name or beautiful.font:gsub("%s%d+$", "") local font_name = args.font_name or beautiful.font:gsub("%s%d+$", "")
local units = args.units or 'metric' local units = args.units or 'metric'