Advertisement
fuxoft

Web Weather

May 2nd, 2013
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.99 KB | None | 0 0
  1. API_KEY = "XXXXXXXXXXXX" --free API key for Weather Underground
  2.  
  3.  
  4. string.dig = function(str,name)
  5.     assert(type(name)=="string")
  6.     assert(#name > 0)
  7.     if #str == "" then
  8.         return nil, "not found at all"
  9.     end
  10.     if str:match("<"..name.."/>") then
  11.         return 0
  12.     end
  13.     local result = {}
  14.     local match = "<"..name..">(.-)</"..name..">"
  15.     for t in str:gmatch(match) do
  16.         table.insert(result,t)
  17.     end
  18.     if #result == 0 then
  19.         return nil
  20.     end
  21.     if #result == 1 then
  22.         return (result[1])
  23.     end
  24.     return result
  25. end
  26.  
  27. local unit_t = "celsius"
  28. local unit_t1 = unit_t:sub(1,1)
  29. require("serialize")
  30.  
  31. local function get_tables(place_id)
  32.     local key = "place_"..place_id
  33.     local data = SERIALIZE.load()
  34.     local message
  35.    
  36.     local all_t = data[key]
  37.     if not all_t then
  38.         require("wget")
  39.         local all_txt = WGET.get("http://api.wunderground.com/api/"..API_KEY.."/conditions/forecast/astronomy/q/"..place_id..".xml")
  40.         --print (current_txt)
  41.         local current = all_txt:dig("current_observation")
  42.         if not current then
  43.             return("No 'current_observation' in data")
  44.         end
  45.         all_t = {}
  46.         all_t.temp = current:dig("temp_"..unit_t1).."°"
  47.         all_t.humidity = assert(current:dig("relative_humidity"))
  48.         all_t.pressure = tonumber(current:dig("pressure_mb"))
  49.         all_t.expire_at = os.time() + 60*5
  50.         data[key] = all_t
  51.         message = "UPDATE"
  52.         --local fd = io.open("/tmp/pressure.txt","a+")
  53.         --fd:write(os.time()..":"..current_t.pressure..":"..current_t.temp.."\n")
  54.         --fd:close()
  55.         local forecast = all_txt:dig("simpleforecast")
  56.         if not forecast then
  57.             return "No 'simpleforecast' in data"
  58.         end
  59.         local moon = all_txt:dig("moon_phase")
  60.         all_t.moon_pct = tonumber(moon:dig("percentIlluminated"))
  61.         all_t.moon_age = tonumber(moon:dig("ageOfMoon"))
  62.         local sunset = moon:dig("sunset")
  63.         all_t.sunset = sunset:dig("hour")..":"..sunset:dig("minute")
  64.         local sunrise = moon:dig("sunrise")
  65.         all_t.sunrise = sunrise:dig("hour")..":"..sunrise:dig("minute")
  66.  
  67.         local days_txt = assert(forecast:dig("forecastday"))
  68.         while #days_txt > 4 do
  69.             table.remove(days_txt)
  70.         end
  71.         for day_n, day_txt in ipairs(days_txt) do
  72.             local ft = {}
  73.             all_t[day_n] = ft
  74.             ft.name = day_txt:dig("date"):dig("weekday")
  75.             ft.conditions = day_txt:dig("conditions")
  76.             ft.high = day_txt:dig("high"):dig(unit_t).."°"
  77.             ft.low = day_txt:dig("low"):dig(unit_t).."°"
  78.         end
  79.     end
  80.  
  81.     if message then
  82.         SERIALIZE.save(data)
  83.     end
  84.     return all_t, message
  85. end
  86.  
  87. local all_t, message = get_tables("49.995106,14.509420")
  88. --local current_t, forecast_t, message = get_tables("KAKANCHO52")
  89.  
  90. if type(all_t) == "string" then
  91.     local result = [[<html><head><title>Weather @ Fuxoft.cz</title>
  92. <meta http-equiv="refresh" content="2">
  93. </head><body><h1>Cannot get data from weather server<br><br>]]..os.date().."<br><br>"..all_t.."<br><br>"..os.time()..[[</h1></body></html>]]
  94.     return result
  95. end
  96.  
  97. local days = {}
  98. for i,ft in ipairs(all_t) do
  99.     --ft.conditions = ft.conditions .. " freezing rain Thunderstorms"
  100.     ft.conditions = ft.conditions:gsub("Thunderstorm","T/storm")
  101.     days[i] = "<div class='forecast'><div class='header'>"..(ft.name:sub(1,3):upper()).."</div><big>"..ft.high.."<br>"..ft.low.."</big><br>"..ft.conditions.."</div>"
  102. end
  103. all_t.forecast_result = table.concat(days)
  104.  
  105. local phase
  106. if all_t.moon_pct == 100 then
  107.     phase = "⬤"
  108. elseif all_t.moon_pct == 0 then
  109.     phase = "◯"
  110. else
  111.     phase = "☽"
  112.     if all_t.moon_age > 14 then
  113.         phase = "☾"
  114.     end
  115. end
  116.  
  117. local moon = all_t.moon_pct .."% "..phase.." "..all_t.moon_age.." d"
  118.  
  119. local time = os.date("*t")
  120. local date = time.day.."."..time.month..". '"..(time.year%100)
  121. time = string.format("%d:%02d",time.hour,time.min)
  122. --message = "FORECAST"
  123. if message then
  124.     date = "<font color = #00ff00>"..message.."</font>"
  125. end
  126. --<link href='http://fonts.googleapis.com/css?family=Ubuntu:400,700' rel='stylesheet' type='text/css'>
  127. --Praha: 49.995106,14.509420
  128.  
  129. local result = [[
  130. <html><head><title>Weather @ Fuxoft.cz</title>
  131. <style>
  132. body {background-color:#000000; color:#ffffff; font-family: 'Arial'; font-weight:700; sans-serif; font-size: 250%; cursor: none;}
  133. thin {font-weight:400;}
  134. thingrey {font-weight:400; color: #999999;}
  135. big {font-size: 180%;}
  136. giant {font-size: 330%}
  137. mega {font-size: 350%}
  138. div.forecast {background-color:#333333; float:left; width:22%; height:49%; margin-left:25px; text-align:center;}
  139. div.header {background-color:#555555; padding-bottom:5px;}
  140. </style>
  141. <meta http-equiv="refresh" content="5">
  142. </head><body>
  143. <div style='height: 49%; width: 100%;'>
  144. <div style='height: 100%; width: 49%; float:left; padding-left:0.5em;'><giant>]]..all_t.temp..[[</giant><br><thingrey>Humidity:</thingrey> ]]..all_t.humidity..[[<br><thingrey>Pressure:</thingrey> ]]..all_t.pressure..[[ mb<br><thingrey>Moon:</thingrey> ]]..moon..[[</div>
  145. <div style='height: 100%; float:right; text-align:right; padding-right:0.5em;'><mega>]]..time..[[</mega><br><big>]]..date..[[</big><br><thingrey>Light:</thingrey> ]]..all_t.sunrise..[[ - ]]..all_t.sunset..[[</div>
  146. </div>]]..all_t.forecast_result..[[</body>
  147.     ]]
  148. return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement