summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPavel Makhov <pmakhov@theoctavegroup.com>2019-07-11 13:31:46 -0400
committerPavel Makhov <pmakhov@theoctavegroup.com>2019-07-11 13:31:46 -0400
commit434cdae5e27c9c2508bf11e5bfe0ae08801f85b1 (patch)
tree624a1994cba0c9338bd7995bed38e238090e557b
parent3d84ce64bd27920a751206115ee0a6bca8eb95c9 (diff)
pr #84
-rw-r--r--secrets.lua8
-rw-r--r--weather-widget/weather.lua35
2 files changed, 34 insertions, 9 deletions
diff --git a/secrets.lua b/secrets.lua
index 9e3dace..e110fe5 100644
--- a/secrets.lua
+++ b/secrets.lua
@@ -7,12 +7,12 @@
local secrets = {
-- Yandex.Translate API key - https://tech.yandex.com/translate/
- translate_widget_api_key = 'API_KEY',
+ translate_widget_api_key = os.getenv('AWW_TRANSLATE_API_KEY') or 'API_KEY',
-- OpenWeatherMap API key - https://openweathermap.org/appid
- weather_widget_api_key = 'API_KEY',
- weather_widget_city = 'Montreal,ca',
- weather_widget_units = 'metric' -- for celsius, or 'imperial' for fahrenheit
+ weather_widget_api_key = os.getenv('AWW_WEATHER_API_KEY') or 'c3d7320b359da4e48c2d682a04076576',
+ weather_widget_city = os.getenv('AWW_WEATHER_CITY') or 'Montreal,ca',
+ weather_widget_units = os.getenv('AWW_WEATHER_UNITS') or 'metric' -- for celsius, or 'imperial' for fahrenheit
}
return secrets
diff --git a/weather-widget/weather.lua b/weather-widget/weather.lua
index d060fd2..deeff61 100644
--- a/weather-widget/weather.lua
+++ b/weather-widget/weather.lua
@@ -7,6 +7,8 @@
-------------------------------------------------
local http = require("socket.http")
+local socket = require("socket")
+local ltn12 = require("ltn12")
local json = require("json")
local naughty = require("naughty")
local wibox = require("wibox")
@@ -27,6 +29,13 @@ local function worker(args)
local api_key = args.api_key or naughty.notify{preset = naughty.config.presets.critical, text = 'OpenweatherMap API key is not set'}
local units = args.units or 'metric'
+ local weather_api_url = (
+ 'https://api.openweathermap.org/data/2.5/weather'
+ .. '?q=' .. city
+ .. '&appid=' .. api_key
+ .. '&units=' .. units
+ )
+
local icon_widget = wibox.widget {
{
id = "icon",
@@ -104,10 +113,26 @@ local function worker(args)
local resp
weather_timer:connect_signal("timeout", function()
- local resp_json, status = http.request('https://api.openweathermap.org/data/2.5/weather?q='
- .. city
- .. '&appid=' .. api_key
- .. '&units=' .. units)
+ local resp_json = {}
+ local res, status = http.request{
+ url=weather_api_url,
+ sink=ltn12.sink.table(resp_json),
+ -- 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) then
local err_resp = json.decode(resp_json)
naughty.notify {
@@ -115,7 +140,7 @@ local function worker(args)
text = err_resp.message,
preset = naughty.config.presets.critical,
}
- elseif (resp_json ~= nil) then
+ elseif (resp_json ~= nil and resp_json ~= '') then
resp = json.decode(resp_json)
icon_widget.image = path_to_icons .. icon_map[resp.weather[1].icon]
temp_widget:set_text(string.gsub(resp.main.temp, "%.%d+", "")