parent
54924ef064
commit
fe631a4253
|
@ -6,7 +6,9 @@
|
||||||
-- @copyright 2018 Pavel Makhov
|
-- @copyright 2018 Pavel Makhov
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
|
|
||||||
|
local socket = require("socket")
|
||||||
local http = require("socket.http")
|
local http = require("socket.http")
|
||||||
|
local ltn12 = require("ltn12")
|
||||||
local json = require("json")
|
local json = require("json")
|
||||||
local naughty = require("naughty")
|
local naughty = require("naughty")
|
||||||
local wibox = require("wibox")
|
local wibox = require("wibox")
|
||||||
|
@ -14,6 +16,13 @@ local gears = require("gears")
|
||||||
|
|
||||||
local secrets = require("awesome-wm-widgets.secrets")
|
local secrets = require("awesome-wm-widgets.secrets")
|
||||||
|
|
||||||
|
local weather_api_url = (
|
||||||
|
'https://api.openweathermap.org/data/2.5/weather'
|
||||||
|
.. '?q=' .. secrets.weather_widget_city
|
||||||
|
.. '&appid=' .. secrets.weather_widget_api_key
|
||||||
|
.. '&units=' .. secrets.weather_widget_units
|
||||||
|
)
|
||||||
|
|
||||||
local path_to_icons = "/usr/share/icons/Arc/status/symbolic/"
|
local path_to_icons = "/usr/share/icons/Arc/status/symbolic/"
|
||||||
|
|
||||||
local icon_widget = wibox.widget {
|
local icon_widget = wibox.widget {
|
||||||
|
@ -93,18 +102,34 @@ local weather_timer = gears.timer({ timeout = 60 })
|
||||||
local resp
|
local resp
|
||||||
|
|
||||||
weather_timer:connect_signal("timeout", function ()
|
weather_timer:connect_signal("timeout", function ()
|
||||||
local resp_json, status = http.request('https://api.openweathermap.org/data/2.5/weather?q='
|
local resp_json = {}
|
||||||
.. secrets.weather_widget_city
|
local res, status = http.request{
|
||||||
.. '&appid=' .. secrets.weather_widget_api_key
|
url=weather_api_url,
|
||||||
.. '&units=' .. secrets.weather_widget_units)
|
sink=ltn12.sink.table(resp_json),
|
||||||
if (status ~= 200 and resp_json ~= nil) then
|
-- ref:
|
||||||
|
-- http://w3.impa.br/~diego/software/luasocket/old/luasocket-2.0/http.html
|
||||||
|
create=function()
|
||||||
|
-- ref: https://stackoverflow.com/a/6021774/595220
|
||||||
|
local req_sock = socket.tcp()
|
||||||
|
-- 't' — overall timeout
|
||||||
|
req_sock:settimeout(0.2, 't')
|
||||||
|
-- 'b' — block timeout
|
||||||
|
req_sock:settimeout(0.001, 'b')
|
||||||
|
return req_sock
|
||||||
|
end
|
||||||
|
}
|
||||||
|
if (resp_json ~= nil) then
|
||||||
|
resp_json = table.concat(resp_json)
|
||||||
|
end
|
||||||
|
|
||||||
|
if (status ~= 200 and resp_json ~= nil and resp_json ~= '') then
|
||||||
local err_resp = json.decode(resp_json)
|
local err_resp = json.decode(resp_json)
|
||||||
naughty.notify{
|
naughty.notify{
|
||||||
title = 'Weather Widget Error',
|
title = 'Weather Widget Error',
|
||||||
text = err_resp.message,
|
text = err_resp.message,
|
||||||
preset = naughty.config.presets.critical,
|
preset = naughty.config.presets.critical,
|
||||||
}
|
}
|
||||||
elseif (resp_json ~= nil) then
|
elseif (resp_json ~= nil and resp_json ~= '') then
|
||||||
resp = json.decode(resp_json)
|
resp = json.decode(resp_json)
|
||||||
icon_widget.image = path_to_icons .. icon_map[resp.weather[1].icon]
|
icon_widget.image = path_to_icons .. icon_map[resp.weather[1].icon]
|
||||||
temp_widget:set_text(string.gsub(resp.main.temp, "%.%d+", "")
|
temp_widget:set_text(string.gsub(resp.main.temp, "%.%d+", "")
|
||||||
|
|
Loading…
Reference in New Issue