Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- API_KEY = "XXXXXXXXXXXX" --free API key for Weather Underground
- string.dig = function(str,name)
- assert(type(name)=="string")
- assert(#name > 0)
- if #str == "" then
- return nil, "not found at all"
- end
- if str:match("<"..name.."/>") then
- return 0
- end
- local result = {}
- local match = "<"..name..">(.-)</"..name..">"
- for t in str:gmatch(match) do
- table.insert(result,t)
- end
- if #result == 0 then
- return nil
- end
- if #result == 1 then
- return (result[1])
- end
- return result
- end
- local unit_t = "celsius"
- local unit_t1 = unit_t:sub(1,1)
- require("serialize")
- local function get_tables(place_id)
- local key = "place_"..place_id
- local data = SERIALIZE.load()
- local message
- local all_t = data[key]
- if not all_t then
- require("wget")
- local all_txt = WGET.get("http://api.wunderground.com/api/"..API_KEY.."/conditions/forecast/astronomy/q/"..place_id..".xml")
- --print (current_txt)
- local current = all_txt:dig("current_observation")
- if not current then
- return("No 'current_observation' in data")
- end
- all_t = {}
- all_t.temp = current:dig("temp_"..unit_t1).."°"
- all_t.humidity = assert(current:dig("relative_humidity"))
- all_t.pressure = tonumber(current:dig("pressure_mb"))
- all_t.expire_at = os.time() + 60*5
- data[key] = all_t
- message = "UPDATE"
- --local fd = io.open("/tmp/pressure.txt","a+")
- --fd:write(os.time()..":"..current_t.pressure..":"..current_t.temp.."\n")
- --fd:close()
- local forecast = all_txt:dig("simpleforecast")
- if not forecast then
- return "No 'simpleforecast' in data"
- end
- local moon = all_txt:dig("moon_phase")
- all_t.moon_pct = tonumber(moon:dig("percentIlluminated"))
- all_t.moon_age = tonumber(moon:dig("ageOfMoon"))
- local sunset = moon:dig("sunset")
- all_t.sunset = sunset:dig("hour")..":"..sunset:dig("minute")
- local sunrise = moon:dig("sunrise")
- all_t.sunrise = sunrise:dig("hour")..":"..sunrise:dig("minute")
- local days_txt = assert(forecast:dig("forecastday"))
- while #days_txt > 4 do
- table.remove(days_txt)
- end
- for day_n, day_txt in ipairs(days_txt) do
- local ft = {}
- all_t[day_n] = ft
- ft.name = day_txt:dig("date"):dig("weekday")
- ft.conditions = day_txt:dig("conditions")
- ft.high = day_txt:dig("high"):dig(unit_t).."°"
- ft.low = day_txt:dig("low"):dig(unit_t).."°"
- end
- end
- if message then
- SERIALIZE.save(data)
- end
- return all_t, message
- end
- local all_t, message = get_tables("49.995106,14.509420")
- --local current_t, forecast_t, message = get_tables("KAKANCHO52")
- if type(all_t) == "string" then
- local result = [[<html><head><title>Weather @ Fuxoft.cz</title>
- <meta http-equiv="refresh" content="2">
- </head><body><h1>Cannot get data from weather server<br><br>]]..os.date().."<br><br>"..all_t.."<br><br>"..os.time()..[[</h1></body></html>]]
- return result
- end
- local days = {}
- for i,ft in ipairs(all_t) do
- --ft.conditions = ft.conditions .. " freezing rain Thunderstorms"
- ft.conditions = ft.conditions:gsub("Thunderstorm","T/storm")
- 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>"
- end
- all_t.forecast_result = table.concat(days)
- local phase
- if all_t.moon_pct == 100 then
- phase = "⬤"
- elseif all_t.moon_pct == 0 then
- phase = "◯"
- else
- phase = "☽"
- if all_t.moon_age > 14 then
- phase = "☾"
- end
- end
- local moon = all_t.moon_pct .."% "..phase.." "..all_t.moon_age.." d"
- local time = os.date("*t")
- local date = time.day.."."..time.month..". '"..(time.year%100)
- time = string.format("%d:%02d",time.hour,time.min)
- --message = "FORECAST"
- if message then
- date = "<font color = #00ff00>"..message.."</font>"
- end
- --<link href='http://fonts.googleapis.com/css?family=Ubuntu:400,700' rel='stylesheet' type='text/css'>
- --Praha: 49.995106,14.509420
- local result = [[
- <html><head><title>Weather @ Fuxoft.cz</title>
- <style>
- body {background-color:#000000; color:#ffffff; font-family: 'Arial'; font-weight:700; sans-serif; font-size: 250%; cursor: none;}
- thin {font-weight:400;}
- thingrey {font-weight:400; color: #999999;}
- big {font-size: 180%;}
- giant {font-size: 330%}
- mega {font-size: 350%}
- div.forecast {background-color:#333333; float:left; width:22%; height:49%; margin-left:25px; text-align:center;}
- div.header {background-color:#555555; padding-bottom:5px;}
- </style>
- <meta http-equiv="refresh" content="5">
- </head><body>
- <div style='height: 49%; width: 100%;'>
- <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>
- <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>
- </div>]]..all_t.forecast_result..[[</body>
- ]]
- return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement