Here’s some test code that I made up when I was having problems with the weather plugin - it’s test only stuff. The XML parsing is based on a Lua pattern. It’s a bit of a hack but works OK on simple XML jobs. If you have an API key it will run in the Lua test area and it will show results in the log.
[code]-- Test the weather parsing
– a-lurker 19 June 2013
local YOUR_API_KEY = ‘abcdefghi’
local YOUR_LATITUDE = ‘29.00’
local YOUR_LONGITUDE = ‘44.00’
local PLUGIN_NAME = ‘TestWeatherParsing’
local DEBUG_MODE = true
local function debug(textParm)
if DEBUG_MODE then
local text = ‘’
local theType = type(textParm)
if theType == ‘string’ then
text = textParm
else
text = 'type is: '…theType
end
luup.log(PLUGIN_NAME…" debug: "…text, 50)
end
end
local function log(textParm, levelParm)
local text = textParm or ‘nil’
local level = levelParm or 50
luup.log(PLUGIN_NAME…" plugin: "…text, level)
end
– refer to: LuaSocket: HTTP support
local function urlRequest()
– this forces a GET instead of a POST
request_body = ‘’
http = require('socket.http')
http.TIMEOUT = 5
local response_body = {}
-- r is 1, c is return status and h are the returned headers in a table variable
local r, c, h = http.request {
url = 'https://api.wunderground.com/api/'..YOUR_API_KEY..'/conditions/forecast/q/'..YOUR_LATITUDE..','..YOUR_LONGITUDE..'.xml',
method = 'POST',
headers = {
['Content-Type'] = 'application/x-www-form-urlencoded',
['Content-Length'] = string.len(request_body)
},
source = ltn12.source.string(request_body),
sink = ltn12.sink.table(response_body)
}
debug('Web site replied with status = '..c)
local page = ''
if type(response_body) == 'table' then
page = table.concat(response_body)
debug('Returned web page data is : '..page)
return true, page
end
return false, page
end
local WEATHER_PATTERN, tmp = string.gsub([[<response>.*
<current_observation>.*<observation_location>.*<latitude>(.-)</latitude>.*<longitude>(.-)</longitude>.*</observation_location>.*
<observation_epoch>(%d-)</observation_epoch>.*
<weather>(.*)</weather>.*
<temp_f>([%d%.%-]-)</temp_f>.*<temp_c>([%d%.%-]-)</temp_c>.*
<relative_humidity>(%d-)%%</relative_humidity>.*
<wind_string>(.*)</wind_string>.*<wind_dir>(%a-)</wind_dir>.*
<wind_mph>([%d%.%-]-)</wind_mph>.*<wind_kph>([%d%.%-]-)</wind_kph>.*
<icon>(.-)</icon>.*
</current_observation>.*
<forecast>.*<simpleforecast><forecastdays><forecastday>.*<period>1</period>
<high><fahrenheit>(.-)</fahrenheit><celsius>(.-)</celsius></high>
<low><fahrenheit>(.-)</fahrenheit><celsius>(.-)</celsius></low>.*
</forecastday>.*<forecastday>.*<period>2</period>.*
]], "%s*", "")
local function parseData()
local st = os.time()
local success, webPage = urlRequest()
-- strip out linefeeds and indentation spaces
local xml = webPage:gsub(">%s*<", "><")
debug("Successful execution of URL: xml is " .. xml)
local lat, long, epoch, condition,
currentTempF, currentTempC,
currentHumidity,
windCondition, windDirection, windMPH, windKPH, icon,
forecastHighTempF, forecastHighTempC, forecastLowTempF, forecastLowTempC
= xml:match(WEATHER_PATTERN)
debug("Execution time: " .. (os.time() - st))
if (lat == nil) then
debug("Parse ERROR")
else
debug("Parse SUCCESSFUL")
debug(lat)
debug(long)
debug(epoch)
debug(condition)
debug(icon)
debug(currentTempF)
debug(currentTempC)
debug(currentHumidity)
debug(windCondition)
debug(windDirection)
debug(windMPH)
debug(windKPH)
debug(forecastHighTempF)
debug(forecastHighTempC)
debug(forecastLowTempF)
debug(forecastLowTempC)
end
end
parseData()
return true
[/code]