This Skin shows how can one use HTML2Table to get upcoming Sun and Moon events from an HTML table using WebParser.
Parsing: Astronomical Applications Department
Skin.ini
#@#Init.lua
(Make sure HTML2Table.lua is in #@#Resources folder as well).
Parsing: Astronomical Applications Department
Ev.gif
Code:
[Rainmeter]Update=1000AccurateText=1DynamicWindowSize=1;This skin gets the upcoming sun and moon events for today.[Variables]Width=280Height=125;Set your Coordinates and Time Zone Offset:;Latitude and Longitude (up to 4 decimal places);Latitude is Negative to the Southlat=0.0000;Longitude is Negative to the Westlon=0.0000;Time Zone offset in hourstz=0.00;Time Zone offset sign (-1 or 1)tz_sign=-1URL=https://aa.usno.navy.mil/calculated/rstt/oneday?date=[&*date*]&lat=#lat#&lon=#lon#&label=&tz=#tz#&tz_sign=#tz_sign#&tz_label=false&dst=false&submit=Get+DataRegExp=(?siU)<table.*Sun</th></tr>(.*)<tr><th colspan="2">Moon</th></tr>(.*)</table>UserAgent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 OPR/109.0.0.0[Date]Measure=TimeFormat=%F;If the date changes, WebParser runs to get the new events.OnChangeAction=[!EnableMeasureGroup Parser][!CommandMeasure WebParser "Update"][WebParser]Group=ParserMeasure=WebParserURL=#URL#UserAgent=#UserAgent#RegExp=#RegExp#;Once the site is parsed, it updates the script with the html for the sun and the moon, the script then converts that html to lua tables and another function returns the next closest events. Finally webparser is disabled.FinishAction=[!EnableMeasure lua][!CommandMeasure "lua" "sunEventsHtml = [[[&sunEvents]]]"][!CommandMeasure "lua" "moonEventsHtml = [[[&moonEvents]]]"][!CommandMeasure "lua" "Run = true"][!UpdateMeasure lua][!DisableMeasureGroup Parser]UpdateRate=-1[sunEvents];Sun Events in HTMLGroup=ParserMeasure=WebParserURL=[WebParser]StringIndex=1[moonEvents];Moon Events in HTMLGroup=ParserMeasure=WebParserURL=[WebParser]StringIndex=2[lua];Lua script measure.;It's enabled once WebParser finishes.Disabled=1Measure=ScriptScriptFile=#@#init2.luaUpdateDivider=-1[SunTime];The value of this measure is set by the scriptMeasure=TimeIfCondition=[SunTime:] = 0;Once the countdown gets to 0, the script updates the value.IfTrueAction=[!CommandMeasure "lua" "RunSunEvent = true"][!UpdateMeasure lua]DynamicVariables=1[MoonTime];The value of this measure is set by the scriptMeasure=TimeIfCondition=[MoonTime:] = 0;Once the countdown gets to 0, the script updates the value.IfTrueAction=[!CommandMeasure "lua" "RunMoonEvent = true"][!UpdateMeasure lua]DynamicVariables=1[BG]Meter=ImageSolidColor=0,0,0,200H=#Height#W=#Width#[Text];Meter StyleX=([BG:w]/2)StringAlign=CenterFontColor=255,255,255,255FontFace=ConsolasFontSize=10AntiAlias=1padding=0,10DynamicVariables=1[SunEvent];The text of this meter is set by the scriptMeter=StringMeterStyle=TextText=Sun Events[MoonEvent];The text of this meter is set by the scriptMeter=StringMeterStyle=Texty=RText=Moon Events
#@#Init.lua
(Make sure HTML2Table.lua is in #@#Resources folder as well).
Code:
function Initialize()--Get #@# pathresources=SKIN:GetVariable('@')--include HTML2Table.luaHTML2Table=dofile(resources..'HTML2Table.lua')--Set initial Run states.Run = falseRunSunEvent = falseRunMoonEvent = falseendfunction Update()if Run then--Convert parsed html to lua tables and get their stringssunEvents=HTML2Table.getStrings(HTML2Table.toTable(sunEventsHtml))moonEvents=HTML2Table.getStrings(HTML2Table.toTable(moonEventsHtml))--Set Run back to false, and Run Events.Run = falseif sunEvents then RunSunEvent = true endif moonEvents then RunMoonEvent = true endendif RunSunEvent then--Get Next Sun EventsunEvent,sunTime=getNextEvent(sunEvents)--Set Sun Optionsif sunTime thenSKIN:Bang('!SetOption','SunTime','timestamp','('..sunTime ..'-[date:timestamp]%86400)')elseSKIN:Bang('!SetOption','SunTime','timestamp',0)endSKIN:Bang('!SetOption','SunEvent','Text','Upcoming Sun Event:\n'..sunEvent..'\nCountdown: [SunTime]')RunSunEvent = falseendif RunMoonEvent then--Get Next Moon EventmoonEvent,moonTime=getNextEvent(moonEvents)--Set Moon Optionsif moonTime thenSKIN:Bang('!SetOption','MoonTime','timestamp','('..moonTime ..'-[date:timestamp]%86400)')elseSKIN:Bang('!SetOption','MoonTime','timestamp',0)endSKIN:Bang('!SetOption','MoonEvent','Text','Upcoming Moon Event:\n'..moonEvent..'\nCountdown: [MoonTime]')RunMoonEvent = falseendend--Get Next Upcoming Event.function getNextEvent(eventsList) local currentTime = os.date('*t') local currentMinutes = currentTime.hour * 60 + currentTime.min local nextEventLabel = nil local nextEventTime = nil local nextEventInSeconds = nil for i = 1, #eventsList, 2 do local eventLabel = eventsList[i] local eventTime = eventsList[i + 1] local hour, minute = eventTime:match('^(%d+):(%d+)$') if hour and minute then local eventMinutes = tonumber(hour) * 60 + tonumber(minute) if eventMinutes > currentMinutes and (not nextEventTime or eventMinutes < nextEventTime) then nextEventLabel = eventLabel nextEventTime = eventMinutes nextEventInSeconds = eventMinutes * 60 end end end if nextEventLabel and nextEventTime then return nextEventLabel .. ' - ' .. string.format('%02d:%02d', math.floor(nextEventTime / 60), nextEventTime % 60), nextEventInSeconds else return 'No upcoming events today.' endend
Statistics: Posted by RicardoTM — Today, 8:58 am