It is currently June 26th, 2019, 4:48 pm

Lua Script Count xml Entries

Discuss the use of Lua in Script measures.
Taurec
Posts: 5
Joined: April 24th, 2016, 6:50 am

Lua Script Count xml Entries

Taurec » May 21st, 2016, 12:35 pm

Hello
sorry but i need some help.
I have never touch lua script and i don't know is it possible to get this function.
I have a xml file (parse from web)
I need to count how many section with a specified name (Data), are include. (only the no. as int)
The sections can be 0 up to .....10
e.g.

<PoolOfData>
<DataCollection>
...some Data, diff content
</DataCollection>
<DataCollection>
...some Data, diff content
</DataCollection>
<DataCollection>
...some Data, diff content
</DataCollection>
<DataCollection>
...some Data, diff content
</DataCollection>
</PoolOfOfData>

Result: 4

I have try to get this funtion with Rainmeter regex, but i got only the content of the first Datacollection.

I hope somebody can help me.

cu Taurec
User avatar
jsmorley
Developer
Posts: 19272
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Lua Script Count xml Entries

jsmorley » May 21st, 2016, 12:50 pm

Skin:

Code: Select all

[Rainmeter]
Update=100
DynamicWindowSize=1
AccurateText=1

[MeasureAllXML]
Measure=Plugin
Plugin=WebParser
URL=file://#CURRENTPATH#Test.html
RegExp=(?siU)^(.*)$
FinishAction=[!UpdateMeasure MeasureScript]

[MeasureScript]
Measure=Script
ScriptFile=Test.lua
UpdateDivider=-1

[MeterDataCount]
Meter=String
FontSize=11
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Test.html:

Code: Select all

<PoolOfData>
<DataCollection>
...some Data, diff content
</DataCollection>
<DataCollection>
...some Data, diff content
</DataCollection>
<DataCollection>
...some Data, diff content
</DataCollection>
<DataCollection>
...some Data, diff content
</DataCollection>
</PoolOfOfData>
Test.lua:

Code: Select all

function Initialize()

	measureXML = SKIN:GetMeasure('MeasureAllXML')

end

function Update()

	allXML = measureXML:GetStringValue()
	if allXML == '' then return end

	dummyString, dataCount = string.gsub(allXML, '<DataCollection>', '')

	SKIN:Bang('!SetOption', 'MeterDataCount', 'Text', 'Count of entries: '..dataCount)

	return dataCount

end
1.jpg
The trick here is that we are using the string.gsub function to replace all occurrences of '<DataCollection>' with '', an empty string. That isn't of any value of course (thus dummyString), but string.gsub can also return a second value that is the count of how many times it was able to replace the string. That we can use...

I suspect that the next question is "how can I use that knowledge to parse the data from the site and use it in my skin?". The short answer to that is, you will probably want to do all the parsing in Lua, and only use WebParser like I am, to get the entire feed into a single string you pass to Lua. There isn't really a useful way to pass that count "back" to WebParser and use it to parse the data, you will just end up in an endless loop. We can get into that if you want / need, but I must say that it would be at least somewhat useful if you had a real example of the XML you are getting.

Also need to decide what the rules of the road are. How many "entries" are you going to display? It can't be unlimited and purely based on the count, as at a minimum you need a meter in the skin for each entry you want to deal with. Can there be "fewer" entries in the feed than that number of entries you plan to deal with? If so, that's fine, but you need to take that into account when doing the cosmetic design, so you can have "empty" meters without it being ugly. What will the skin "look like"?

In a sense, that is the only purpose in getting the "count" up front. Since you have to have a meter for each entry you want to display, the only thing the count really buys you (unless you just want to display it) is the ability to perhaps hide or or otherwise format the meters when there is no value for them. Given that, I'm not entirely sure Lua is needed for this. You could also use "lookahead assertions" in your WebParser RegExp, get say five entries, and if the value they return is "" (an empty string) hide the corresponding meter(s).

Up to you, I like Lua for parsing stuff, but if you are really new at it there will be a tiny bit of learning curve. If you want to learn, I'm happy to help.
You do not have the required permissions to view the files attached to this post.
Taurec
Posts: 5
Joined: April 24th, 2016, 6:50 am

Re: Lua Script Count xml Entries

Taurec » May 21st, 2016, 2:44 pm

Hello jsmorley,

thank you very much for the quick response and the perfect working script.

For sure i have some question more about lua scripting, specilly to use this with Rainmeter. :D

But for the moment i can go ahead with my skin. (thx to you)

... and for my questions: at first i will read something about scripting with lua.
I'm sure that i find my answer there and at this time i can learn a little bit. :)

once again thx for helping

cu Taurec