It is currently April 19th, 2024, 1:10 am

Parsing a date for countdown.lua

Get help with creating, editing & fixing problems with skins
User avatar
StArL0rd84
Posts: 424
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Parsing a date for countdown.lua

Post by StArL0rd84 »

lualua.png
Successful in parsing the date i need for the coundown, but the coundown script simply wont accept the dynamic value.
If i manually type 22 under day= and refesh the script works (ofcause).
What got me really stumped is the fact that [mYear] works, but not [mDateShow1]

Tried to put a setoption on the parser FinishAction=[!SetOption CountDownShow1 Day [mDateShow1]
No dice...

Tried making a variable Date="" and then:
FinishAction=[!SetVariable "Date" "[mDateShow1]"][!WriteKeyValue "Variables" "Date" "[mDateShow1]"]
With Day=#Date# in the script measure. No cigar...

Also tried writing directly to the script measure: FinishAction=[!WriteKeyValue "CountDownShow1" "Day" "[mDateShow1]"]
It will write 22 under day, but this wont take effect untill next refresh.

Clueless as to what could be going wrong here.
Doubtful it could be the script i found that's faulty,
But just in case i included the it if you wanna take a gander.
CountdownLua.rar
You do not have the required permissions to view the files attached to this post.
([mWorkTime] = 1 ? #Work# : ([mEnergyLoss:%] >= 70% ? #Chillmode# : </>))
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Parsing a date for countdown.lua

Post by jsmorley »

I can't help without the whole skin folder. I need the .ini and the .lua and any .inc and image files and anything else I need to test this skin.
User avatar
StArL0rd84
Posts: 424
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: Parsing a date for countdown.lua

Post by StArL0rd84 »

jsmorley wrote:I can't help without the whole skin folder. I need the .ini and the .lua and any .inc and image files and anything else I need to test this skin.
Okay but it's intergrated into a bigger suite.
Look for 'ShowRSS' inside.
SynergyBETA_0.rmskin
You do not have the required permissions to view the files attached to this post.
([mWorkTime] = 1 ? #Work# : ([mEnergyLoss:%] >= 70% ? #Chillmode# : </>))
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Parsing a date for countdown.lua

Post by jsmorley »

Well, I can't quite wrap my head around all that.

Consider an approach like this:

Skin:

Code: Select all

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

[Variables]
;<item><title>Dark Matter 3x08</title><link>http://www.tvmaze.com/episodes/1111387/episode</link><guid isPermaLink="false">1111387</guid><pubDate>Sat, 22 Jul 2017 02:00:00 +0000</pubDate><description>Dark Matter 3x08 Hot Chocolate airs on Friday 21st, July 2017</description>

[Measure296]
Measure=Plugin
Plugin=WebParser
URL=https://showrss.info/show/schedule/296.rss
RegExp=(?siU)<item><title>(.*)</title><link>(.*)</link>.*<description>.*airs on (.*) ([\d]{1,2}?).*, (.*) (.*)</description>
FinishAction=[!CommandMeasure MeasureLua "GetDays([MeasureMonth], [MeasureDayOfMonth], [MeasureYear])"]

[MeasureTitle]
Measure=Plugin
Plugin=WebParser
URL=[Measure296]
StringIndex=1

[MeasureLink]
Measure=Plugin
Plugin=WebParser
URL=[Measure296]
StringIndex=2

[MeasureDayOfWeek]
Measure=Plugin
Plugin=WebParser
URL=[Measure296]
StringIndex=3

[MeasureDayOfMonth]
Measure=Plugin
Plugin=WebParser
URL=[Measure296]
StringIndex=4

[MeasureMonth]
Measure=Plugin
Plugin=WebParser
URL=[Measure296]
StringIndex=5
Substitute="January":"1","February":"2","March":"3","April":"4","May":"5","June":"6","July":"7","August":"8","September":"9","October":"10","November":"11",""December":"12"

[MeasureYear]
Measure=Plugin
Plugin=WebParser
URL=[Measure296]
StringIndex=6

[MeasureLua]
Measure=Script
ScriptFile=#CURRENTPATH#Countdown.lua
UpdateDivider=-1

[MeasureDaysDiff]
Measure=Calc

[MeasureDaysToGo]
Measure=Calc
Formula=7 - MeasureDaysDiff
MinValue=0
MaxValue=7

[MeterTitle]
Meter=String
MeasureName=MeasureTitle
FontSize=12
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1

[MeterAirDate]
Meter=String
Y=5R
FontSize=12
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
DynamicVariables=1
Text=Air date: [MeasureDayOfWeek], [MeasureMonth]/[MeasureDayOfMonth]/[MeasureYear]

[MeterDaysDiff]
Meter=String
Y=5R
FontSize=12
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
DynamicVariables=1
Text=Days until air: [MeasureDaysDiff]

[MeterDiffBar]
Meter=Bar
MeasureName=MeasureDaysToGo
Y=5R
W=200
H=5
BarColor=69,209,83,255
SolidColor=47,47,47,255
BarOrientation=Horizontal
Lua:

Code: Select all

function GetDays(month, day, year)
	
	local showTime = os.time({year=year, month=month, day=day, hour=0, min=0, sec=0, isdst=false})
	local nowTime = os.time(os.date('!*t'))
	local daysDiff = math.ceil((showTime - nowTime ) / 86400)
	
	SKIN:Bang('!SetOption', 'MeasureDaysDiff', 'Formula', daysDiff)
	
end
Note that os.time(os.date('!*t')) will create an os.time() table using the current GMT date and time. To get local time instead, use os.time(os.date('*t')), without the "!" in the os.date() format.

So I'm making some assumptions to start with, I hope they are more or less correct:

1) You want to use the day the show "airs on" from the description, not the <pubdate>.
2) You want to know how many days (including the rest of today) it is from now til the day the show airs.
3) For purposes of the Bar meter, we are assuming any shows listed will air within the next week (7 days)

What we are doing in the skin is:

1) Get the feed using WebParser, and obtain the title and link.
2) From the <description> field, parse and get the "day of the week", "day of the month", "month", and "year" .
3) Send the "month", "day of the month" and "year" to a Lua function GetDays(month,day,year)

What we are doing in the lua is:

1) Put the received year, month and day into an os.time() table so it returns a unix timestamp for that date.
2) Get the current GMT date and time into an os.time() table so it returns a unix timestamp of the current GMT time.
3) Subtract the received timestamp from the current timestamp to get a difference in seconds.
4) Divide that by 86400 to get the number of days from the seconds, and ceiling that value to round it up. The rest of today should count as a full day for this purpose.
5) Return this number of days from now til the show airs to a Calc measure [MeasureDaysDiff]

Then back in the skin we are:

1) Inverting the value of [MeasureDaysDiff] and setting a MinValue and MaxValue to enable using the value in a Bar meter. I am assuming you want to show the value as more or less "number of days in the current 7-day period that have elapsed".

2) Displaying all the info in String meters, and a Bar meter that shows how long we need to wait till the show airs.
1.png
I may have missed the mark on some or even all of what you are trying to do, but I think the key to this is the Lua, which is pretty straightforward. If you have some date and / or time information that you get from some resource, it's pretty easy to turn that into a unix timestamp of seconds from the epoch, and subtract that from the current GMT date and time to get a difference in seconds. You can then do what you want with that difference, from just using it as is to formatting it into weeks, days, hours, minutes and seconds of "difference".

I use it all the time to do things like tell me how "old" an item from a feed is.
2.png
https://www.lua.org/pil/22.1.html
https://docs.rainmeter.net/snippets/format-time/
You do not have the required permissions to view the files attached to this post.
User avatar
StArL0rd84
Posts: 424
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: Parsing a date for countdown.lua

Post by StArL0rd84 »

Hey, thanks man.
Your assumptions was totally right!
Got it working now, and i definitely learned some things about what lua can do here.
Passing information back and forth, retrieving it from the system, converting and dividing.
Ill make use of it in the future.

And thank you for the links. Very useful.

Tried to add some SKIN:Bang's in the lua to accommodate for more shows in the skin, but i guess only one is possible?
Still learning ^^
([mWorkTime] = 1 ? #Work# : ([mEnergyLoss:%] >= 70% ? #Chillmode# : </>))
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Parsing a date for countdown.lua

Post by jsmorley »

StArL0rd84 wrote:Hey, thanks man.
Your assumptions was totally right!
Got it working now, and i definitely learned some things about what lua can do here.
Passing information back and forth, retrieving it from the system, converting and dividing.
Ill make use of it in the future.

Tried to add some SKIN:Bang's in the lua to accommodate for more shows in the skin, but i guess only one is possible?
Still learning ^^
You would probably want to pass the name of a measure to in the call to the function, then you can have the bang do !SetOption on that measure. That would be a way to make it more generic.
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Parsing a date for countdown.lua

Post by jsmorley »

So something like this:

Skin:

Code: Select all

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

[Variables]
Show1=296
Show2=581

MonthSub="January":"1","February":"2","March":"3","April":"4","May":"5","June":"6","July":"7","August":"8","September":"9","October":"10","November":"11","December":"12"

[MeasureLua]
Measure=Script
ScriptFile=#CURRENTPATH#Countdown.lua
UpdateDivider=-1

; *************
; First Show
; *************

[MeasureShow1]
Measure=Plugin
Plugin=WebParser
URL=https://showrss.info/show/schedule/#Show1#.rss
RegExp=(?siU)<item><title>(.*)</title><link>(.*)</link>.*<description>.*airs on (.*) ([\d]{1,2}?).*, (.*) (.*)</description>
FinishAction=[!CommandMeasure MeasureLua "GetDays([MeasureMonth1], [MeasureDayOfMonth1], [MeasureYear1], 'MeasureDaysDiff1')"]

[MeasureTitle1]
Measure=Plugin
Plugin=WebParser
URL=[MeasureShow1]
StringIndex=1

[MeasureLink1]
Measure=Plugin
Plugin=WebParser
URL=[MeasureShow1]
StringIndex=2

[MeasureDayOfWeek1]
Measure=Plugin
Plugin=WebParser
URL=[MeasureShow1]
StringIndex=3

[MeasureDayOfMonth1]
Measure=Plugin
Plugin=WebParser
URL=[MeasureShow1]
StringIndex=4

[MeasureMonth1]
Measure=Plugin
Plugin=WebParser
URL=[MeasureShow1]
StringIndex=5
Substitute=#MonthSub#

[MeasureYear1]
Measure=Plugin
Plugin=WebParser
URL=[MeasureShow1]
StringIndex=6

[MeasureDaysDiff1]
Measure=Calc

[MeasureDaysReversed1]
Measure=Calc
Formula=7 - MeasureDaysDiff1
MinValue=0
MaxValue=7

; *************
; Second Show
; *************

[MeasureShow2]
Measure=Plugin
Plugin=WebParser
URL=https://showrss.info/show/schedule/#Show2#.rss
RegExp=(?siU)<item><title>(.*)</title><link>(.*)</link>.*<description>.*airs on (.*) ([\d]{1,2}?).*, (.*) (.*)</description>
FinishAction=[!CommandMeasure MeasureLua "GetDays([MeasureMonth2], [MeasureDayOfMonth2], [MeasureYear2], 'MeasureDaysDiff2')"]

[MeasureTitle2]
Measure=Plugin
Plugin=WebParser
URL=[MeasureShow2]
StringIndex=1

[MeasureLink2]
Measure=Plugin
Plugin=WebParser
URL=[MeasureShow2]
StringIndex=2

[MeasureDayOfWeek2]
Measure=Plugin
Plugin=WebParser
URL=[MeasureShow2]
StringIndex=3

[MeasureDayOfMonth2]
Measure=Plugin
Plugin=WebParser
URL=[MeasureShow2]
StringIndex=4

[MeasureMonth2]
Measure=Plugin
Plugin=WebParser
URL=[MeasureShow2]
StringIndex=5
Substitute=#MonthSub#

[MeasureYear2]
Measure=Plugin
Plugin=WebParser
URL=[MeasureShow2]
StringIndex=6

[MeasureDaysDiff2]
Measure=Calc

[MeasureDaysReversed2]
Measure=Calc
Formula=7 - MeasureDaysDiff2
MinValue=0
MaxValue=7

; *************
; First Meters
; *************

[MeterTitle1]
Meter=String
MeasureName=MeasureTitle1
X=0
Y=0
FontSize=12
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1

[MeterAirDate1]
Meter=String
X=0
Y=5R
FontSize=12
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
DynamicVariables=1
Text=Air date: [MeasureDayOfWeek1], [MeasureMonth1]/[MeasureDayOfMonth1]/[MeasureYear1]

[MeterDaysDiff1]
Meter=String
X=0
Y=5R
FontSize=12
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
DynamicVariables=1
Text=Days until air: [MeasureDaysDiff1]

[MeterDiffBar1]
Meter=Bar
MeasureName=MeasureDaysReversed1
X=0
Y=5R
W=200
H=5
BarColor=69,209,83,255
SolidColor=47,47,47,255
BarOrientation=Horizontal

; *************
; Second Meters
; *************

[MeterTitle2]
Meter=String
MeasureName=MeasureTitle2
X=220
Y=0
FontSize=12
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1

[MeterAirDate2]
Meter=String
X=0r
Y=5R
FontSize=12
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
DynamicVariables=1
Text=Air date: [MeasureDayOfWeek2], [MeasureMonth2]/[MeasureDayOfMonth2]/[MeasureYear2]

[MeterDaysDiff2]
Meter=String
X=0r
Y=5R
FontSize=12
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
DynamicVariables=1
Text=Days until air: [MeasureDaysDiff2]

[MeterDiffBar2]
Meter=Bar
MeasureName=MeasureDaysReversed2
X=0r
Y=5R
W=200
H=5
BarColor=69,209,83,255
SolidColor=47,47,47,255
BarOrientation=Horizontal
Lua:

Code: Select all

function GetDays(month, day, year, measureName)

	local showTime = os.time({year=year, month=month, day=day, hour=0, min=0, sec=0, isdst=false})
	local nowTime = os.time(os.date('!*t'))
	local daysDiff = math.ceil((showTime - nowTime ) / 86400)

	SKIN:Bang('!SetOption', measureName, 'Formula', daysDiff)
	
end
1.png
BTW, some notes on the RegExp I'm using.

RegExp=(?siU)<item><title>(.*)</title><link>(.*)</link>.*<description>.*airs on (.*) ([\d]{1,2}?).*, (.*) (.*)</description>

The bit that gets the day of the month, which can be 1 or 2 digits in length is here:

([\d]{1,2}?)

The way you get variable length character sets is to define the character set [\d] (numeric digits) and the possible number of digits it can be {1,2}. (at least one and up to two in this case). The ? trailing it is because you have the overall regular expression set to "ungreedy" (?si[b]U[/b]), but you need this bit to be "greedy" so it will get 2 if there are 2 and 1 if there is 1. Otherwise it will always only get the first digit. The ? forces just that part of the expression to be "greedy".

https://www.debuggex.com/cheatsheet/regex/pcre
You do not have the required permissions to view the files attached to this post.
User avatar
StArL0rd84
Posts: 424
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: Parsing a date for countdown.lua

Post by StArL0rd84 »

Wow you really went above and beyond for me.
Don't know how to thank you for all of this!

The last bit you posted streamlined things even further.
Before i had 10 scripts, now it's just the one.

Copy/pasting taught me some things but i'm sure ill learn more as i tinker with it.
showraiso.gif
You do not have the required permissions to view the files attached to this post.
([mWorkTime] = 1 ? #Work# : ([mEnergyLoss:%] >= 70% ? #Chillmode# : </>))
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Parsing a date for countdown.lua

Post by jsmorley »

Just one small thing I noticed. When the day of the air is the current day, what I have will return "-0", since the "time" of the feed is not defined, and we just assume midnight of the day the show airs. When the current day is the same, as soon as it is past midnight, the timestamp of the show is effectively in the past, or a negative number. While in other cases we might want to react to that properly, in this case we just want to ignore it. However, when we ceiling the negative fractional number, it rounds -0.3233 or whatever to just -0, which is both useless and annoying.

This will fix that:

Code: Select all

function GetDays(month, day, year, measureName)

	local showTime = os.time({year=year, month=month, day=day, hour=0, min=0, sec=0, isdst=false})
	local nowTime = os.time(os.date('!*t'))
	local daysDiff = math.ceil((showTime - nowTime ) / 86400)

	SKIN:Bang('!SetOption', measureName, 'Formula', math.abs(daysDiff))
	
end
If the feed legitimately returns shows that have aired in previous days, (I suspect not) then a different approach to this will be needed. In that case, I would recommend just letting it return -1 or -2 or whatever (remove the math.abs()), and deal with that as you like in the skin, but since -0 is never going to make sense, have a test in the Lua that just says:

if daysDiff == -0 then daysDiff = 0 end

It's up to you. You can just let anything less that "1" be treated as zero by the Lua:

if daysDiff < 1 then daysDiff = 0

Or put an IfCondition on the [MeasureDaysDiffN] measures that checks for less than zero and sets the Text on the meter to "Already Aired" or whatever you like. The Bar meters won't matter as they will always be at 100% when the value is less than 1.
User avatar
StArL0rd84
Posts: 424
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: Parsing a date for countdown.lua

Post by StArL0rd84 »

Experimenting!
Was noticing that the bars for the shows seemed 1 day ahead of when the shows actually aired. Maybe because of my location in the world in Scandinavia Europe.

The bars would be filled when i went to bed at night and the shows would be out the next day when i woke up.
So i made a small modification to all the calc measures [MeasureDaysToGo].

Before:

Code: Select all

[mDaysToGoShow10]
 Measure=Calc
 Formula=7 - mDaysDiffShow10
 MinValue=0
 MaxValue=7
After:

Code: Select all

[mDaysToGoShow10]
 Measure=Calc
 Formula=7 - (mDaysDiffShow10+1)
 MinValue=0
 MaxValue=7
barss.png
Bars should now be at 0, and truly indicate a full 7 days week i have to wait for the next episode to air.
Further observation will of cause be necessary to see if it'll misbehave because of my crude mod.
You do not have the required permissions to view the files attached to this post.
([mWorkTime] = 1 ? #Work# : ([mEnergyLoss:%] >= 70% ? #Chillmode# : </>))