Skip to content

Instantly share code, notes, and snippets.

@meskarune
Last active May 6, 2022 05:07
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save meskarune/5729e8d6c8428e9c70a72bed475db4e1 to your computer and use it in GitHub Desktop.
Save meskarune/5729e8d6c8428e9c70a72bed475db4e1 to your computer and use it in GitHub Desktop.
lua weather script
#!/usr/bin/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 = "5128581"
-- metric or imperial
cf = "imperial"
-- get an open weather map api key: http://openweathermap.org/appid
apikey = "<API KEY>"
-- measure is °C if metric and °F if imperial
measure = '°' .. (cf == 'metric' and 'C' or 'F')
-- 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
io.close(f)
return true
else
return false
end
end
if file_exists("weather.json") then
cache = io.open("weather.json","r+")
data = json.decode(cache:read())
timepassed = os.difftime(currenttime, data.timestamp)
else
cache = io.open("weather.json", "w")
timepassed = 6000
end
makecache = function (s)
s.timestamp = currenttime
save = json.encode(s)
cache:write(save)
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
temp = response.main.temp
conditions = response.weather[1].main
icon = response.weather[1].icon:sub(1, 2)
io.write(("%s %s%s %s\n"):format(icons[icon], math.round(temp), measure, conditions))
cache:close()
@SamPrzyswa
Copy link

I am using this script and I would like to be able to display icons in .png instead of UNICODE icons, what should I do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment