It is currently July 27th, 2024, 8:09 am

Problem with YouTube Subs Counter

Get help with creating, editing & fixing problems with skins
dasiun
Posts: 30
Joined: January 27th, 2021, 8:05 pm

Problem with YouTube Subs Counter

Post by dasiun »

Hi!, I am building a 1x3 LED screen (64x192 dots), and i need a youtube sub counter, i tried code from 4 years ago.

I have "362" Subs, the skin shows "15" as seen in the image.

Code: Select all

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

[Variables]

[StatsSubs]
Measure=Plugin
Plugin=WebParser
URL=https://www.youtube.com/dasiun
RegExp=(?siU)"header":.*"subscriberCountText":.*"[^"]*text":"([^"]*)"
StringIndex=1
UpdateRate=3600
debug=2
RegExpSubstitute=1
Substitute=" .*$":""

[Subscribers]
Meter=String
MeasureName=StatsSubs
StringAlign=Left
Y=0
X=0
FontSize=37
FontColor=55EE55
AntiAlias=1
You do not have the required permissions to view the files attached to this post.
User avatar
Yincognito
Rainmeter Sage
Posts: 7785
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Problem with YouTube Subs Counter

Post by Yincognito »

dasiun wrote: May 13th, 2024, 8:28 pm Hi!, I am building a 1x3 LED screen (64x192 dots), and i need a youtube sub counter, i tried code from 4 years ago.

I have "362" Subs, the skin shows "15" as seen in the image.
That's because in the page source (which you can view in Chrome by right clicking and choosing View Page Source) the number of subscribers from your channels (15 and 18) seem to be in front / before the total number of subscribers for your account - doing a CTRL+F to find "subscriberCountText" makes that obvious. To further "complicate" matters, the syntax before those 15, 18 and 362 values in the page source is exactly the same.

The solution to this is to choose the last subscriber count. In regular expressions this is done by "anchoring" the pattern to a relevant point for your case - say, the beginning of the string aka ^ - and then ignoring / skipping as many characters as possible - done by temporarily "inverting" the ?U ungreedy flag at the start by adding a ? after .* and make mtatching greedy - before matching the desired text. So, something like this should do (added the UserAgent and some Flags just in case, see the WebParser measure):

Code: Select all

[Rainmeter]
Update=1000
AccurateText=1
DynamicWindowSize=1
BackgroundMode=2
SolidColor=0,0,0,255

[Variables]

[StatsSubs]
Measure=WebParser
URL=https://www.youtube.com/dasiun
UserAgent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Flags=ForceReload | NoCookies
RegExp=(?siU)^.*?"subscriberCountText":.*"[^"]*text":"([^"]*)"
StringIndex=1
UpdateRate=3600
debug=2

[Subscribers]
Meter=String
MeasureName=StatsSubs
StringAlign=Left
Y=0
X=0
FontSize=37
FontColor=55EE55
AntiAlias=1
Text=Subs: %1
I removed the "header" part as it didn't make much sense here, since it has 7 occurrences in the page source (a regex pattern is the more effective as its parts are more rare in the string, ideally the only such parts).
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
dasiun
Posts: 30
Joined: January 27th, 2021, 8:05 pm

Re: Problem with YouTube Subs Counter

Post by dasiun »

Thank you :D

One last thing, I would like to know how to show the green text when the number goes up and red when it goes down.

I think using IfAboveValue and IfBelowValue but that doesn't change the previous or next number.

Sorry for bad english.
You do not have the required permissions to view the files attached to this post.
User avatar
Yincognito
Rainmeter Sage
Posts: 7785
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Problem with YouTube Subs Counter

Post by Yincognito »

dasiun wrote: May 14th, 2024, 11:30 pm Thank you :D

One last thing, I would like to know how to show the green text when the number goes up and red when it goes down.

I think using IfAboveValue and IfBelowValue but that doesn't change the previous or next number.

Sorry for bad english.
You're welcome, don't worry about your English, not everyone here is a native speaker. ;-)

This is not tested extensively, since for me, it seems the address keeps alternating the response randomly, where sometimes RegExp=(?siU)^.*?"subscriberCountText":.*"[^"]*text":"(\d+) .*" works, and sometimes RegExp=(?siU)"contentMetadataViewModel":.*?"metadataParts".*"text":.*"content":.*"(\d+) .*" does (both are now adjusted to capture only the number, without any text afterwards between the quotes), but you could try something like:

Code: Select all

[Variables]
PrevSubs=300

[Rainmeter]
Update=1000
AccurateText=1
DynamicWindowSize=1
BackgroundMode=2
SolidColor=0,0,0,255

[StatsSubs]
Measure=WebParser
URL=https://www.youtube.com/dasiun
;URL=file://#CURRENTPATH#WebParserDump.txt
UserAgent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Flags=ForceReload | NoCookies
RegExp=(?siU)^.*?"subscriberCountText":.*"[^"]*text":"(\d+) .*"
;RegExp=(?siU)"contentMetadataViewModel":.*?"metadataParts".*"text":.*"content":.*"(\d+) .*"
StringIndex=1
UpdateRate=3600
debug=2
FinishAction=[!EnableMeasure CurrSubs][!UpdateMeasure CurrSubs]

[CurrSubs]
Disabled=1
Measure=Calc
Formula=[&StatsSubs]
UpdateDivider=-1
IfCondition=(#CURRENTSECTION#>#PrevSubs#)
IfTrueAction=[!SetVariable PrevSubs [CurrSubs]][!WriteKeyValue Variables PrevSubs [CurrSubs]][!SetOption Subscribers FontColor "0,255,0,255"][!UpdateMeter Subscribers][!Redraw]
IfCondition2=(#CURRENTSECTION#=#PrevSubs#)
IfTrueAction2=[!SetVariable PrevSubs [CurrSubs]][!WriteKeyValue Variables PrevSubs [CurrSubs]][!SetOption Subscribers FontColor "255,255,255,255"][!UpdateMeter Subscribers][!Redraw]
IfCondition3=(#cURRENTSECTION#<#PrevSubs#)
IfTrueAction3=[!SetVariable PrevSubs [CurrSubs]][!WriteKeyValue Variables PrevSubs [CurrSubs]][!SetOption Subscribers FontColor "255,0,0,255"][!UpdateMeter Subscribers][!Redraw]
DynamicVariables=1

[Subscribers]
Meter=String
MeasureName=StatsSubs
StringAlign=Left
Y=0
X=0
FontSize=37
FontColor=255,255,255,255
AntiAlias=1
Text=Subs: %1
So basically, your idea was good (IfCondition is much more versatile than IfAboveValue / IfBelowValue), except that the thing should happen in another measure enabled and updated from the FinishAction of the WebParser so that it doesn't react before the WebParser retrieves the response from the site, the previous value must become the current value if you want to compare with the last retrieved value and not always with the value stored at skin load, and optionally the current value can be physically written to the file as the previous value so that you get a similar reaction even if you load the skin after a period when the number of subs changed and the skin was unloaded. Feel free to adjust things to your preference. The hardcoded 300 value is just for my testing purpose, as an initial reference, it can be any number since as soon as data is retrieved it will be updated anyway (in memory and optionally written to the file for persistency).
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
dasiun
Posts: 30
Joined: January 27th, 2021, 8:05 pm

Re: Problem with YouTube Subs Counter

Post by dasiun »

thanks, i think it works perfectly.
dasiun
Posts: 30
Joined: January 27th, 2021, 8:05 pm

Re: Problem with YouTube Subs Counter

Post by dasiun »

I put a Substitute="18":"CurrSubs" in the "Subscribers" string so it doesnt show 18 again.
User avatar
Yincognito
Rainmeter Sage
Posts: 7785
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Problem with YouTube Subs Counter

Post by Yincognito »

dasiun wrote: May 15th, 2024, 9:25 pm thanks, i think it works perfectly.
You're welcome, I'm glad if it does. ;-)
dasiun wrote: May 15th, 2024, 10:41 pm I put a Substitute="18":"CurrSubs" in the "Subscribers" string so it doesnt show 18 again.
Yeah, I don't think that's a solution to avoid the measure matching the wrong number. CurrSubs is just a string, [CurrSubs] is the value of the that measure, but in any case it doesn't make much sense to do it. If the format of the response received in WebParserDump.txt alternates for you too like mentioned earlier, try this:

Code: Select all

[Variables]
PrevSubs=0

[Rainmeter]
Update=1000
AccurateText=1
DynamicWindowSize=1
BackgroundMode=2
SolidColor=0,0,0,255

[StatsSubs]
Measure=WebParser
URL=https://www.youtube.com/dasiun
;URL=file://#CURRENTPATH#WebParserDump.txt
UserAgent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Flags=ForceReload | NoCookies
RegExp=(?siU)(?|.*"contentMetadataViewModel":.*?"metadataParts".*"text":.*"content":.*"(\d+) .*"|.*?"subscriberCountText":.*"[^"]*text":"(\d+) .*")
StringIndex=1
UpdateRate=3600
Debug=2
FinishAction=[!EnableMeasure CurrSubs][!UpdateMeasure CurrSubs]

[CurrSubs]
Disabled=1
Measure=Calc
Formula=[&StatsSubs]
UpdateDivider=-1
IfCondition=(#CURRENTSECTION#<#PrevSubs#)
IfTrueAction=[!SetVariable PrevSubs [CurrSubs]][!WriteKeyValue Variables PrevSubs [CurrSubs]][!SetOption Subscribers FontColor "255,0,0,255"][!UpdateMeter Subscribers][!Redraw]
IfCondition2=(#CURRENTSECTION#>#PrevSubs#)
IfTrueAction2=[!SetVariable PrevSubs [CurrSubs]][!WriteKeyValue Variables PrevSubs [CurrSubs]][!SetOption Subscribers FontColor "0,255,0,255"][!UpdateMeter Subscribers][!Redraw]
;IfCondition3=(#CURRENTSECTION#=#PrevSubs#)
;IfTrueAction3=[!SetVariable PrevSubs [CurrSubs]][!WriteKeyValue Variables PrevSubs [CurrSubs]][!SetOption Subscribers FontColor "255,255,255,255"][!UpdateMeter Subscribers][!Redraw]
DynamicVariables=1

[Subscribers]
Meter=String
MeasureName=StatsSubs
StringAlign=Left
Y=0
X=0
FontSize=37
FontColor=255,255,255,255
AntiAlias=1
Text=Subs: %1
I used a "branch reset group" here as the RegExp pattern, which basically tries to match the part before the | and do the number capture, and if it fails it tries to match the part after the | and do the number capture. The | symbol means "OR".

I also left the equality test commented out in case you need that behavior. Assuming the last subs before loading the skin was 363 and you have a progression of subs like 363, 364, 364 on subsequent measure updates after loading the skin:
- if uncommented the text will be white (363=363), green (364>363), and then white again (364=364) until an increase or decrease occurs
- if commented the text will be white (default), green (364>363), and then still green (no action) until an increase or decrease occurs
In other words, when uncommented the color is strictly based on the comparison to the previous value so white will occur as soon as they are equal, while when commented the color will stay true to the green or red "trend" last registered even though the current value might be equal to the last value from one update to another.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth