It is currently April 26th, 2024, 9:23 am

Lua and RegExp

Discuss the use of Lua in Script measures.
Tidus
Posts: 5
Joined: January 30th, 2013, 1:37 am

Lua and RegExp

Post by Tidus »

Hey all,
I'm attempting to create a meter that parses a webpage for information for a certain day, and so was toying with using measures in a regexp but was not able to make that work. So i looked up some Lua and am attempting to use a script to insert a variable into my expression and so change the regexp depending on the date.
Thanks in advance

INI

Code: Select all

[MeasureToday]
Measure=Time

[MeasureYesterday]
Measure=Time
TimeStamp=([MeasureToday:]-86400)
Format=%A, %B %d
DynamicVariables=1

[LuaModifyMeasureScoresText]
Measure=Script
ScriptFile=#ScoresTextPath#
mDate1=[MeasureYesterday]
Debug=1

[MeasureScoresText]
Measure=Plugin
Plugin=WebParser
Url=http://www.sportsnetwork.com/merge/tsnform.aspx?c=sportsnetwork&page=nba/stats/monthlyschedSN.aspx
RegExp="(?siU)Sunday, January 27.*FINAL: <strong>(.*)</strong>(.*)    </td>"
DynamicVariables=1
Debug=1

[MeasureScore1]
Measure=Plugin
Plugin=WebParser
URL=[MeasureScoresText]
StringIndex=1

[MeasureScore2]
Measure=Plugin
Plugin=WebParser
URL=[MeasureScoresText]
StringIndex=2

[ScoresText]
Meter=String
MeasureName=MeasureScore1
MeasureName2=MeasureScore2
MeterStyle=TextStyle
Text=%1 %2
x=10
y=10
FontColor=255,255,255,255
LUA

Code: Select all

function Initialize()

	sDate1 = SELF:GetOption('mDate1')
	
end

function Update()
	
	sDate1 = SELF:GetOption('mDate1')
	SKIN:Bang('!SetOption MeasureScoresText RegExp \"(?siU)'..sDate1..'.*FINAL: (.*)<strong>(.*)</strong>    </td>\"')
	SKIN:Bang('!Refresh')
	
	return ('complete')
	
end	
and finally, the text from the webpage:

Code: Select all

...
ign="center">Monday, January 28</td>        </tr>
...
 nowrap>FINAL: <strong>GSW (114)</strong> - TOR (102)    </td>    <td class="TSN5" width="30%" 
...
 nowrap>FINAL: <strong>MEM (103)</strong> - PHI (100)    </td>    <td class="TSN1"
...
I'm focusing on the
"GSW (114)" and " - TOR (102)"
"MEM (103)"..." - PHI (100)"
and any other sets of scores in that format right after any "FINAL:" , but only for specific dates.

I don't think my script has been able to alter the RegExp= option yet.

Some other things:

Removing "SKIN:Bang('!Refresh') displays the default text from the regexp, which has not been altered by the lua script, so I assume the INI is functioning properly. With the !Refresh in place, the log doesnt show debug from the webparser, and also doesnt show any values for the "LuaModifyMeasureScoresText","MeasureScoresText","MeasureScore1" or "MeasureScore2" measures. When the !Refresh is removed, LuaModifyMeasureScoresText returns a "complete" value, from the script.

Thanks again
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Lua and RegExp

Post by smurfier »

SetOption cannot be used with WebParser measures because WebParser does not as of yet support Dynamic Variables.

If the source text does not contain any unicode characters we can parse the text in the Lua function. Could you please provide the full source text file?
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
Tidus
Posts: 5
Joined: January 30th, 2013, 1:37 am

Re: Lua and RegExp

Post by Tidus »

Unfortunate...

EDIT: did not read your full post. I was unable to attach a txt or html file, and was unable to paste the entire line from the text either. Even pasting the line itself exceeded the character limit. What file type is acceptable? And I am assuming you are asking for the text from the webpage I am parsing?

alternatively i could post the link to the website if you can save it as a txt file?
http://www.sportsnetwork.com/merge/tsnform.aspx?c=sportsnetwork&page=nba/stats/monthlyschedSN.aspx

sorry, I'm still getting used to this
User avatar
lysy1993lbn
Posts: 291
Joined: July 25th, 2011, 9:53 am
Location: Lublin, Poland

Re: Lua and RegExp

Post by lysy1993lbn »

Tidus wrote:alternatively i could post the link to the website if you can save it as a txt file?
You can post code to service like http://sync.in/ or http://pastebin.mozilla.org/ or http://pastebin.com/
"Never argue with an idiot, he will drag you down to his level and beat you with experience."
my deviantART | Alternative Rainmeter tray icons
Tidus
Posts: 5
Joined: January 30th, 2013, 1:37 am

Re: Lua and RegExp

Post by Tidus »

Alright, I posted it to pastebin under the title "Tidus rainmeter source text 1/29".
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Lua and RegExp

Post by smurfier »

The format of the page makes matching a bit difficult. This may take some time.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Lua and RegExp

Post by smurfier »

Now that I've successfully parsed the page, what do you want to do with the data?
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
Tidus
Posts: 5
Joined: January 30th, 2013, 1:37 am

Re: Lua and RegExp

Post by Tidus »

The idea is to make a sports ticker. It would arrange the string the sets of scores from each of the previous games into a long string, which repeats, and uses Kaelri's transition script to make the string move across the bottom of the screen.

The words will be arranged in a way such as:

LAC (73) - TOR (98) | MIA (89) - IND (102) | ORL (84) - BOS (97) | ...

I did notice that this source had [bold] [/bold] around the winners, and I intended to devise a way to remove those with "?"s or something that ignores optional information.
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Lua and RegExp

Post by smurfier »

If I interpret your needs correctly...

Skin.ini

Code: Select all

[Rainmeter]
Update=1000

[Web]
Measure=Plugin
Plugin=WebParser
Url=http://www.sportsnetwork.com/merge/tsnform.aspx?c=sportsnetwork&page=nba/stats/monthlyschedSN.aspx
RegExp=(?siU)(.*)$
FinishAction=!UpdateMeasure Lua #CURRENTCONFIG#

[Lua]
Measure=Script
ScriptFile=Lua.lua
UpdateDivider=-1

[Scores]
Meter=String
MeasureName=Lua
Lua.lua

Code: Select all

function Initialize()
	Web = SKIN:GetMeasure('Web')
end -- Initialize

function Update()
	local text, tbl, date = Web:GetStringValue():match('<table[^>]-Class="TsnTableStyle">(.-)</table>') or '', {}

	for block in text:gmatch('<tr>.-</tr>') do
		if block:match('FINAL:') and date then
			local temp = block:match('FINAL:(.-)</td>'):gsub('<[^>]+>', ''):match('^%s*(.-)%s*$')
			if not tbl[date] then
				tbl[date] = {temp}
			else
				table.insert(tbl[date], temp)
			end
		elseif block:match('%w+, %w+ %d+') then
			date = block:match('<td[^>]->([^<]+)</td>')
		end
	end

	local yesterday = os.date('%A, %B #%d', os.time() - 86400):gsub('#0?', '')
	return table.concat(tbl[yesterday] or {}, ' | ')
end -- Update
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
Tidus
Posts: 5
Joined: January 30th, 2013, 1:37 am

Re: Lua and RegExp

Post by Tidus »

Works perfectly! I really appreciate it.