This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Weather service not working anymore?
#58
Code:
require('socket.http')

------------------------------------------------------------------------------------------------------------------------------
--
-- Weather forecast from Yr, delivered by the Norwegian Meteorological Institute and the NRK
--
-- This is a combo of the codes contributions from the Admin, Thomas, and me MichelDeLigne
--
-- Usage:
-- * Define your place in the variable url in the function yrno_fetch()
--
-- * Ending the url with "forecast_hour_by_hour.xml" will give access to the predictions for a city for the next 48 hours, per hour
-- * Ending the url with "forecast.xml" will give predictions for 9 days, per 6 hours slices.
-- Search for your location in https://www.yr.no/
-- When found, add forecast.xml or forecast_hour_by_hour.xml to the url
--
-- Don't run the script more that once per hour, or 4 times a day for the 6h forecast. Yr.no web admins will certainly thank you for that.
-- (no they won't, but they won't curse you for polling their servers every minutes either, which is a total waste of time/bandwidth).

-- Previsions for the next hour (or 6h if the url ends with forecast.xml) would be contained in:
-- forecast[1].symbol             "Partly cloudy"
-- forecast[1].icon                "03n" (available at https://hjelp.yr.no/hc/en-us/articles/203786121-Weather-symbols-on-Yr)
-- forecast[1].precipitation        [mm]
-- forecast[1].wind_direction        "SSW", "E", ...
-- forecast[1].wind_speed        [m/s]
-- forecast[1].temperature        [°C]
-- forecast[1].pressure            [hPa]
-- ld_minTemp                [°C] Min temperature for the next day
-- ld_maxTemp                [°C] Max temperature for the next day
-- lt_tomorrow                What the next day is

-- Next hour forecast would be in forecast[2].symbol
-- and so on

-------------------------------------------------------------------------------------------------------------------------------

forecast = {}

---------------------------------------------------------------
-- Functions
---------------------------------------------------------------
function parsetag(parser, tag, attributes)
 if tag == 'time' then
   table.insert(forecast, {})
   forecast[ #forecast ].from = attributes.from
 elseif tag == 'symbol' then
   forecast[ #forecast ].symbol = attributes.name
   forecast[ #forecast ].icon = attributes.var
 elseif tag == 'precipitation' then
   forecast[ #forecast ].precipitation = attributes.value
 elseif tag == 'windDirection' then
   forecast[ #forecast ].wind_direction = attributes.code
 elseif tag == 'windSpeed' then
   forecast[ #forecast ].wind_speed = attributes.mps
 elseif tag == 'temperature' then
   forecast[ #forecast ].temperature = attributes.value
 elseif tag == 'pressure' then
   forecast[ #forecast ].pressure = attributes.value
 end
end

-------------------------------
function yrno_fetch()
 ld_minTemp=0
 ld_maxTemp=0
 next_day={}

-- local url = 'https://www.yr.no/place/Belgium/Wallonia/Marche-lez-%C3%89caussinnes/forecast.xml'
 local url = 'http://www.yr.no/place/Belgium/Wallonia/Marche-lez-%C3%89caussinnes/forecast_hour_by_hour.xml'
 
 local data, err = socket.http.request(url)

 if data then
   require('lxp').new({
     StartElement = parsetag
   }):parse(data)

   -- log(forecast)
 else
   alert('Fetch failed: ' .. tostring(err))
   return 1
 end
   
 ll_tomorrow = os.time()+24*3600 -- tomorrow as long
 lt_tomorrow = os.date("*t",ll_tomorrow) -- tomorrow as table for use if forecast_write

 --gets the date of tomorrow
 local ls_match = os.date("%Y--%m--%d",ll_tomorrow)

 for index, value in ipairs(forecast) do
   if (value.from:match("^"..ls_match)) then
     local ld_temp=tonumber(value.temperature)
     if (ld_minTemp == nil) then
       ld_minTemp=ld_temp
     end
     if (ld_maxTemp == nil) then
       ld_maxTemp=ld_temp
     end
     if (ld_temp<ld_minTemp or index==1) then
       ld_minTemp=ld_temp
     end
     if (ld_temp>ld_maxTemp or index==1) then
       ld_maxTemp=ld_temp
     end
   end
 end
   
 return 0
end

------------------------------------------
-- Main code
------------------------------------------

if yrno_fetch() then
  -- write the data to the bus, or do something else with it
  -- If the fetch fails, we do nothing.
 
  --log(ld_minTemp, ld_maxTemp, lt_tomorrow, forecast[1].symbol, forecast[1].icon, forecast[1].precipitation, forecast[1].wind_direction,
  --forecast[1].wind_speed, forecast[1].temperature, forecast[1].pressure)
 
  --grp.write('1/1/12', ld_minTemp, dt.float16)
  --grp.write('1/1/13', ld_maxTemp, dt.float16)
 --grp.write('1/1/14', lt_tomorrow, dt.date)
 
 --grp.write('1/1/15', forecast[1].symbol)
 --grp.write('1/1/16', forecast[1].icon)
 
 --grp.write('1/1/17', forecast[1].precipitation)
  --grp.write('1/1/18', forecast[1].wind_direction)
  --grp.write('1/1/19', forecast[1].wind_speed)
  --grp.write('1/1/20', forecast[1].temperature)
  --grp.write('1/1/21', forecast[1].pressure)
end

Hi,

Here is a small contribution from me and others (mostly others in fact). 
The script fetches the weather forecast from Yr.no

I've put together some of the previous scripts, and removedsome functions and correct the error handling. 
I am not a big fan of using functions only once. I find it makes the code less readable because you have to jump all over the place when you read it. 

There is also a description at the beginning of the code explaining how to use it.

Cheers.
Michel
Reply


Messages In This Thread
RE: Weather service not working anymore? - by MichelDeLigne - 03.04.2019, 19:58

Forum Jump: