summaryrefslogtreecommitdiffstats
path: root/weather.lua
diff options
context:
space:
mode:
authorSuren A. Chilingaryan <csa@suren.me>2019-08-25 03:28:43 +0200
committerSuren A. Chilingaryan <csa@suren.me>2019-08-25 03:28:43 +0200
commit8c700beff8c8322012c1e6c96ff22f4251ec70bd (patch)
tree7153c2aed92d80b475a46df1a980929cf65c07f1 /weather.lua
parent4bc60db4b1db1c71ea17141ca7dfbcc5faf32227 (diff)
downloadconky-8c700beff8c8322012c1e6c96ff22f4251ec70bd.tar.gz
conky-8c700beff8c8322012c1e6c96ff22f4251ec70bd.tar.bz2
conky-8c700beff8c8322012c1e6c96ff22f4251ec70bd.tar.xz
conky-8c700beff8c8322012c1e6c96ff22f4251ec70bd.zip
All weather services are broken: here is some research, but nothing working yet...
Diffstat (limited to 'weather.lua')
-rwxr-xr-xweather.lua121
1 files changed, 121 insertions, 0 deletions
diff --git a/weather.lua b/weather.lua
new file mode 100755
index 0000000..9364167
--- /dev/null
+++ b/weather.lua
@@ -0,0 +1,121 @@
+#!/usr/bin/env lua
+-- load the http socket module
+http = require("socket.http")
+-- load the json module
+json = require("json")
+
+api_url = "http://api.openweathermap.org/data/2.5/weather?"
+
+-- http://openweathermap.org/help/city_list.txt , http://openweathermap.org/find
+cityid = "2878676"
+
+-- metric or imperial
+cf = "metric"
+
+-- get an open weather map api key: http://openweathermap.org/appid
+apikey = "3df14dd57e705f30c04157c2f6260939"
+
+-- measure is °C if metric and °F if imperial
+measure = '°' .. (cf == 'metric' and 'C' or 'F')
+wind_units = (cf == 'metric' and 'kph' or 'mph')
+
+-- Unicode weather symbols to use
+icons = {
+ ["01"] = "☀️",
+ ["02"] = "🌤",
+ ["03"] = "🌥",
+ ["04"] = "☁",
+ ["09"] = "🌧",
+ ["10"] = "🌦",
+ ["11"] = "🌩",
+ ["13"] = "🌨",
+ ["50"] = "🌫",
+}
+
+currenttime = os.date("!%Y%m%d%H%M%S")
+
+file_exists = function (name)
+ f=io.open(name,"r")
+ if f~=nil then
+ f:close()
+ return true
+ else
+ return false
+ end
+end
+
+if file_exists("/etc/conky/weather.json") then
+ cache = io.open("/etc/conky/weather.json","r")
+ data = json.decode(cache:read())
+ cache:close()
+ timepassed = os.difftime(currenttime, data.timestamp)
+else
+ timepassed = 6000
+end
+
+makecache = function (s)
+ cache = io.open("/etc/conky/weather.json", "w+")
+ s.timestamp = currenttime
+ save = json.encode(s)
+ cache:write(save)
+ cache:close()
+end
+
+if timepassed < 3600 then
+ response = data
+else
+ print (("%sid=%s&units=%s&APPID=%s"):format(api_url, cityid, cf, apikey))
+ weather = http.request(("%sid=%s&units=%s&APPID=%s"):format(api_url, cityid, cf, apikey))
+ print(weather)
+ if weather then
+ response = json.decode(weather)
+ makecache(response)
+ else
+ response = data
+ end
+end
+
+math.round = function (n)
+ return math.floor(n + 0.5)
+end
+
+degrees_to_direction = function (d)
+ val = math.round(d/22.5)
+ directions={"N","NNE","NE","ENE",
+ "E","ESE", "SE", "SSE",
+ "S","SSW","SW","WSW",
+ "W","WNW","NW","NNW"}
+ return directions[val % 16]
+end
+
+temp = response.main.temp
+conditions = response.weather[1].description
+icon2 = response.weather[1].id
+icon = response.weather[1].icon:sub(1, 2)
+humidity = response.main.humidity
+wind = response.wind.speed
+deg = degrees_to_direction(response.wind.deg)
+sunrise = os.date("%H:%M %p", response.sys.sunrise)
+sunset = os.date("%H:%M %p", response.sys.sunset)
+
+conky_text = [[
+${font Symbola:size=48}%s ${voffset -10}${font :size=20}${color1}%s${font}${voffset -5}%s${color}
+${alignc}${voffset 28} %s
+
+${alignc}Humidity: ${color1}%s%%${color}
+${alignc}Wind: ${color1}%s%s %s${color}
+
+${alignc}${font Symbola:size=20}─⯊─${font}
+${alignc}${color1}%s${color} | ${color1}%s${color}
+]]
+io.write((conky_text):format(icons[icon],
+ math.round(temp),
+ measure,
+ conditions,
+ humidity,
+ math.round(wind),
+ wind_units,
+ deg,
+ sunrise,
+ sunset)
+)