Last active
November 30, 2024 08:40
-
-
Save meskarune/5729e8d6c8428e9c70a72bed475db4e1 to your computer and use it in GitHub Desktop.
lua weather script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
You give your self the answer :) instead of ["01"] = "unicode", use: ["01"] = "path_to_.png",
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?