![]() |
|
Weather info yr.no new API - Printable Version +- LogicMachine Forum (https://forum.logicmachine.net) +-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1) +--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8) +--- Thread: Weather info yr.no new API (/showthread.php?tid=2753) |
Weather info yr.no new API - AlexLV - 27.07.2020 Hi, service yr.no now use new API and advice use .json format for weather data reading. .xml will not supported anymore from start of the next year. Info here: https://developer.yr.no/doc/ May be some created script based on .json and new API? I just tried to read data for me and receive error: link to info with .json: https://api.met.no/weatherapi/locationforecast/2.0/complete?lat=67.8596&lon=35.0904&altitude=13 Script I tried to read data: Code: require('json')
https = require('ssl.https')
escape = require('socket.url').escape
url = 'https://api.met.no/weatherapi/locationforecast/2.0/complete?lat=67.8596&lon=35.0904&altitude=13'
url = string.format(url)
res = https.request(url)
log(res)
data = json.pdecode(res)
log(data)
if type(data) ~= 'table' then
alert('failed to load weather data')
return
end
if data.error then
log('error', data.error)
return
endI received such error: * string: 403 Forbidden User-Agent header LuaSocket 2.0.2 is not allowed, use a unique identifier What is incorrect in my trying? BR, Alex RE: Weather info yr.no new API - benanderson_475 - 27.07.2020 i tried it got the same error, looking at the documents it say "that you must send a unique identifier in the User-Agent header. Any requests with a prohibited or missing User-Agent will receive a 403 Forbidden error." i don't know how to pass the headers with socket.url but i tried it with http.request, like the below with the User-Agent header, it returns the data Code: function get_request(url)
local http, ltn12, sink, res, err
ltn12 = require('ltn12')
http = require('socket.http')
sink = {}
res, err = http.request({
url = url,
method = 'GET',
headers =
{
["Accept"] = "*/*",
["User-Agent"] = "1234567897", -- i think this can be any number?
},
sink = ltn12.sink.table(sink)
})
if sink then
return table.concat(sink)
else
return nil, err
end
end
x = get_request('https://api.met.no/weatherapi/locationforecast/2.0/complete?lat=67.8596&lon=35.0904&altitude=13')
log(x)RE: Weather info yr.no new API - admin - 28.07.2020 You can override default user agent like this. This example uses LM MAC address to create a unique number for the user agent string. Code: json = require('json')
http = require('socket.http')
url = 'https://api.met.no/weatherapi/locationforecast/2.0/complete?lat=67.8596&lon=35.0904&altitude=13'
mac = 0
io.readfile('/sys/class/net/eth0/address'):gsub('%x%x', function(v)
mac = mac * 256 + tonumber(v, 16)
end)
http.USERAGENT = 'LM ' .. mac
res, err = http.request(url)
log(res, err)RE: Weather info yr.no new API - AlexLV - 28.07.2020 Super, thank you Admin |