18.03.2020, 09:01
Code:
require('socket.http')
forecast = {}
function parsetag(parser, tag, attributes)
if tag == 'time' then
table.insert(forecast, {})
elseif tag == 'symbol' then
forecast[ #forecast ].symbol = attributes.name
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
url = 'http://www.yr.no/place/United_Kingdom/England/Farnborough/varsel.xml'
data, err = socket.http.request(url)
if data then
require('lxp').new({
StartElement = parsetag
}):parse(data)
grp.update('Weather precipitation', tonumber(forecast[1].precipitation))
grp.update('Weather symbol', forecast[1].symbol)
grp.update('Weather windDirection', forecast[1].wind_direction)
grp.update('Weather windSpeed', tonumber(forecast[1].wind_speed))
grp.update('Weather temperature', tonumber(forecast[1].temperature))
grp.update('Weather pressure', tonumber(forecast[1].pressure))
--log(forecast)
else
alert('Fetch failed: ' .. tostring(err))
end
------------------------------
Ctrl+F5
Ctrl+F5