Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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 = "MY_CITY_ID"
- -- metric or imperial
- cf = "metric"
- -- get an open weather map api key: http://openweathermap.org/appid
- apikey = "MY_API_KEY"
- -- 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("weather.json") then
- cache = io.open("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("weather.json", "w+")
- s.timestamp = currenttime
- save = json.encode(s)
- cache:write(save)
- cache:close()
- end
- if timepassed < 3600 then
- response = data
- else
- weather = http.request(("%sid=%s&units=%s&APPID=%s"):format(api_url, cityid, cf, apikey))
- 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
- temp_min = response.main.temp_min
- temp_max = response.main.temp_max
- conditions = response.weather[1].description
- icon2 = response.weather[1].id
- icon = response.weather[1].icon:sub(1, 2)
- humidity = response.main.humidity
- sunrise = os.date("%H:%M %p", response.sys.sunrise)
- sunset = os.date("%H:%M %p", response.sys.sunset)
- conky_text = [[
- ${alignc}${font "Noto Color Emoji":size=48}%s
- ${alignc}${font :size=20}${color1}%s ${font}${voffset -5}%s${color}
- ${alignc}${voffset 10}${font :size=20}${color1}%s ${font}${voffset -5}%s${color} ${font :size=20}${color1}%s ${font}${voffset -5}%s${color}
- ${alignc}${voffset 28} %s
- ${alignc}Humidity: ${color1}%s%%
- ${alignc}%s | %s
- ]]
- io.write((conky_text):format(
- icons[icon],
- temp,
- measure,
- temp_min,
- measure,
- temp_max,
- measure,
- conditions,
- humidity,
- sunrise,
- sunset
- ))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement