I'm working on a skin that uses the WebParser to grab a certain value off a page every five minutes and display it in a String meter.
I now need to dump the values into a text files every five minutes too. I understand it probably involves Lua, but I can't figure out how to accomplish that.
As it stands now, the code has the following structure:
function appendToLog()
-- this gets a handle to your WebParse measure
local parserMeasure = SKIN:GetMeasure('MeasureValue')
-- this gets the value of your WebParser measure
local text = parserMeasure:GetStringValue()
-- converts '.\log.txt' into an absolute path
local logPath = SKIN:MakePathAbsolute('log.txt')
-- open the log file in append-update mode
local logFile = io.open(logPath, 'a+')
-- make sure the file was opened successfully
if not logFile then
print ('could not open ' .. logPath)
else
-- write the line
logFile:write(text)
-- close the file
logFile:close()
end
end
Then whenever you'd like you can call that function from your skin by having a script measure and using !CommandMeasure (for example, in the FinishAction on your WebParser measure)
function Initialize()
filePath = SKIN:MakePathAbsolute(SELF:GetOption('OutputFile'))
end
function Update()
end
function UpdateLog(stringArg)
local fileHandle = io.open(filePath, 'a+')
fileHandle:write(os.date()..' | '..stringArg..'\n')
fileHandle:close()
end
I threw the date / time on the entry, you can certainly remove that from the output if you don't want or need it.
P.S. My real question about this is how you know that an item returned by WebParser is really "new" and should be written. Maybe not important to what you are trying to do, but I'd hate to have a log that is adding a line 200+ times a day if things haven't really changed.
The only real difference is that I passed the text to write on the !CommandMeasure to the script, rather than having the script "come back" and get the WebParser measure value.
It might not be bad for completeness sake to do some kind of "assert" on the file open, so if in fact it fails you are told so. However, 'a+' will create the file if it doesn't already exist, so the only real possible error is if the file can't be created due to an invalid folder structure or a write permission issue in the folder. If this is something the author will distribute, then I would make that part of it a bit more robust. For personal use, it's probably not as important.
function Initialize()
filePath = SKIN:MakePathAbsolute(SELF:GetOption('OutputFile'))
end
function Update()
end
function UpdateLog(stringArg)
local fileHandle = io.open(filePath, 'a+')
fileHandle:write(os.date()..' | '..stringArg..'\n')
fileHandle:close()
end
I threw the date / time on the entry, you can certainly remove that from the output if you don't want or need it.
P.S. My real question about this is how you know that an item returned by WebParser is really "new" and should be written. Maybe not important to what you are trying to do, but I'd hate to have a log that is adding a line 200+ times a day if things haven't really changed.
Edit: FlyingHyrax beat me to it...
And how to delete a date / time from lua?
That's it, I figured it out