28.09.2022, 05:32
Try this:
Code:
date = os.date('%Y%m%d')
periodstart = date .. '0000'
periodend = date .. '2300'
url = 'https://web-api.tp.entsoe.eu/api?securityToken=XXXXXXXXXX&' ..
'documentType=A44&in_Domain=10Y1001A1001A46L&out_Domain=10Y1001A1001A46L&' ..
'periodStart=' .. periodstart .. '&periodEnd=' .. periodend
data, code = require('socket.http').request(url)
if not data or code ~= 200 then
log('request failed', data, code)
return
end
prices = {}
key = 0
callbacks = {
StartElement = function(p, tag)
if tag == 'TimeSeries' then
key = key + 1
prices[ key ] = {}
elseif tag == 'position' then
state = 'position'
elseif tag == 'price.amount' then
state = 'price'
end
end,
EndElement = function(p, tag)
state = nil
end,
CharacterData = function(p, value)
value = value:trim()
if #value > 0 then
if state == 'position' then
position = tonumber(value)
elseif state == 'price' then
prices[ key ][ position ] = tonumber(value)
end
end
end
}
parser = require('lxp').new(callbacks)
parser:parse(data)
parser:close()
if key > 0 then
rate = grp.getvalue('5/5/6')
for i, price in pairs(prices[ 1 ]) do
grp.checkupdate('1/1/' .. i, price * rate / 10)
end
end