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.

XML data
#1
Hello, I`m looking to import weaterdata from a website.
The site exports data as xml.

Is there a guide to doing so in LM?

Example xml:
https://www.yr.no/sted/Norge/Vestfold_og...varsel.xml


Sent fra min SM-G950F via Tapatalk
Reply
#2
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
Reply
#3
Daniel, is this still working. ?

I think yr.no changed to another api.

https://api.met.no/
Reply
#4
Their new beta API provides JSON which is much easier to parse:
https://api.met.no/weatherapi/locationfo...0&lon=9.58
Reply
#5
My script still works.
------------------------------
Ctrl+F5
Reply
#6
require('ssl.https')
require('json')

data = ssl.https.request('http://weerlive.nl/api/json-10min.php?locatie=' ..plaatsnaam)
datatable = json.pdecode(data)
datatable = datatable['liveweer'][1]

log(datatable)

If you use something like this, how do you deploy it and how do you extract this to groupo adresses?
Reply
#7
(18.03.2020, 09:29)admin Wrote: Their new beta API provides JSON which is much easier to parse:
https://api.met.no/weatherapi/locationfo...0&lon=9.58
Where do we privide location on this one?

never mind, I see the long lat now Smile

decoding json gives me "nesting to deep" in most of the tables..
What could be the reason for this?
Reply
#8
I tried like that you have to put coordinate of location as longitude and latitude.
Code:
require('ssl.https')
require('json')

data = ssl.https.request('https://api.met.no/weatherapi/locationforecast/2.0/.json?lat=51.28&lon=-0.75')
datatable = json.pdecode(data)
log(datatable.properties.timeseries[1].data.instant.details.air_temperature)
------------------------------
Ctrl+F5
Reply
#9
(18.03.2020, 10:22)Daniel. Wrote: I tried like that you have to put coordinate of location as longitude and latitude.
Code:
require('ssl.https')
require('json')

data = ssl.https.request('https://api.met.no/weatherapi/locationforecast/2.0/.json?lat=51.28&lon=-0.75')
datatable = json.pdecode(data)
log(datatable.properties.timeseries[1].data.instant.details.air_temperature)
yeah, I got some data, but I also got "nesting to deep" in the log. could try to extract spesiffic data, maybe that would do the trick.

seems to work, but the amount of data in the table is quite big, and difficult to navigate.
Any good tips to view the table in a structured way?
Reply
#10
Firefox can display JSON data in a structured way. Or just copy JSON from the URL and use any online JSON viewer.
Reply


Forum Jump: