Advertisement
Guest User

weather.lua

a guest
Jul 19th, 2019
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.90 KB | None | 0 0
  1. #!/usr/bin/env lua
  2. -- load the http socket module
  3. http = require("socket.http")
  4. -- load the json module
  5. json = require("json")
  6.  
  7. api_url = "http://api.openweathermap.org/data/2.5/weather?"
  8.  
  9. -- http://openweathermap.org/help/city_list.txt , http://openweathermap.org/find
  10. cityid = "MY_CITY_ID"
  11.  
  12. -- metric or imperial
  13. cf = "metric"
  14.  
  15. -- get an open weather map api key: http://openweathermap.org/appid
  16. apikey = "MY_API_KEY"
  17.  
  18. -- measure is °C if metric and °F if imperial
  19. measure = '°' .. (cf == 'metric' and 'C' or 'F')
  20. wind_units = (cf == 'metric' and 'kph' or 'mph')
  21.  
  22. -- Unicode weather symbols to use
  23. icons = {
  24.   ["01"] = "☀️",
  25.   ["02"] = "🌤",
  26.   ["03"] = "🌥",
  27.   ["04"] = "☁",
  28.   ["09"] = "🌧",
  29.   ["10"] = "🌦",
  30.   ["11"] = "🌩",
  31.   ["13"] = "🌨",
  32.   ["50"] = "🌫",
  33. }
  34. currenttime = os.date("!%Y%m%d%H%M%S")
  35.  
  36. file_exists = function (name)
  37.     f=io.open(name,"r")
  38.     if f~=nil then
  39.         f:close()
  40.         return true
  41.     else
  42.         return false
  43.     end
  44. end
  45.  
  46. if file_exists("weather.json") then
  47.     cache = io.open("weather.json","r")
  48.     data = json.decode(cache:read())
  49.     cache:close()
  50.     timepassed = os.difftime(currenttime, data.timestamp)
  51. else
  52.     timepassed = 6000
  53. end
  54.  
  55. makecache = function (s)
  56.     cache = io.open("weather.json", "w+")
  57.     s.timestamp = currenttime
  58.     save = json.encode(s)
  59.     cache:write(save)
  60.     cache:close()
  61. end
  62.  
  63. if timepassed < 3600 then
  64.     response = data
  65. else
  66.     weather = http.request(("%sid=%s&units=%s&APPID=%s"):format(api_url, cityid, cf, apikey))
  67.     if weather then
  68.         response = json.decode(weather)
  69.         makecache(response)
  70.     else
  71.         response = data
  72.     end
  73. end
  74.  
  75. math.round = function (n)
  76.     return math.floor(n + 0.5)
  77. end
  78.  
  79. degrees_to_direction = function (d)
  80.     val = math.round(d/22.5)
  81.     directions={"N","NNE","NE","ENE",
  82.                 "E","ESE", "SE", "SSE",
  83.                 "S","SSW","SW","WSW",
  84.                 "W","WNW","NW","NNW"}
  85.     return directions[val % 16]
  86. end
  87.  
  88. temp = response.main.temp
  89. temp_min = response.main.temp_min
  90. temp_max = response.main.temp_max
  91. conditions = response.weather[1].description
  92. icon2 = response.weather[1].id
  93. icon = response.weather[1].icon:sub(1, 2)
  94. humidity = response.main.humidity
  95. sunrise = os.date("%H:%M %p", response.sys.sunrise)
  96. sunset = os.date("%H:%M %p", response.sys.sunset)
  97.  
  98. conky_text = [[
  99. ${alignc}${font "Noto Color Emoji":size=48}%s
  100. ${alignc}${font :size=20}${color1}%s ${font}${voffset -5}%s${color}
  101. ${alignc}${voffset 10}${font :size=20}${color1}%s ${font}${voffset -5}%s${color} ${font :size=20}${color1}%s ${font}${voffset -5}%s${color}
  102. ${alignc}${voffset 28} %s
  103. ${alignc}Humidity: ${color1}%s%%
  104. ${alignc}%s | %s
  105. ]]
  106. io.write((conky_text):format(
  107.     icons[icon],
  108.     temp,
  109.     measure,
  110.     temp_min,
  111.     measure,
  112.     temp_max,
  113.     measure,
  114.     conditions,
  115.     humidity,
  116.     sunrise,
  117.     sunset
  118. ))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement