It is currently April 19th, 2024, 4:46 am

Webparser Help

Get help with creating, editing & fixing problems with skins
mkboynton
Posts: 23
Joined: January 31st, 2016, 10:52 pm

Webparser Help

Post by mkboynton »

I am trying to parse the website https://weather.codes/united-states-of-america/alabama/ to find the weather code for a specific city. ie: if my city is Dothan, it should return USAL0162. What I have figured out so far is returning the first code (USAL0001). This is the regex I currently have, but I am missing something and can't figure it out.

Code: Select all

(?siU)<dt>(.*)<\/dt>(?(?<=Dothan)<dt>(.*)<\/dt>)
I am asking for help from all of you regex gurus.
User avatar
balala
Rainmeter Sage
Posts: 16144
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Webparser Help

Post by balala »

mkboynton wrote: February 17th, 2020, 8:53 pm I am asking for help from all of you regex gurus.
Something like this:

Code: Select all

[Variables]
Location=Dothan

[MeasureRainmeter]
Measure=WebParser
UpdateRate=3600
Url=https://weather.codes/united-states-of-america/alabama/
RegExp=(?siU)</dd><dt>(.{8})</dt><dd>#Location#\r

[MeasureWeatherCode]
Measure=WebParser
Url=[MeasureRainmeter]
StringIndex=1
mkboynton
Posts: 23
Joined: January 31st, 2016, 10:52 pm

Re: Webparser Help

Post by mkboynton »

That does the trick balala, thanks! Could you explain what this means:<dt>(.{8})</dt> ?
User avatar
balala
Rainmeter Sage
Posts: 16144
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Webparser Help

Post by balala »

mkboynton wrote: February 17th, 2020, 9:26 pm That does the trick balala, thanks! Could you explain what this means:<dt>(.{8})</dt> ?
(.{8}) means there are exactly 8 characters in the weather code. Not more, nor less.
But there is still a problem. If you enter a not existing city name, you get errors. Working on fixing this...
mkboynton
Posts: 23
Joined: January 31st, 2016, 10:52 pm

Re: Webparser Help

Post by mkboynton »

10-4, I really appreciate your help
User avatar
balala
Rainmeter Sage
Posts: 16144
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Webparser Help

Post by balala »

balala wrote: February 17th, 2020, 9:42 pm But there is still a problem. If you enter a not existing city name, you get errors. Working on fixing this...
Fixed.
The problem is that the city name is preceded by its weather code. so there is no way to check if the given city name exists and if it does exist, to get its code. To do this, you have to create a new WebParser measure, which checks it and act accordingly:

Code: Select all

[Variables]
Location=Dothan

[MeasureExists]
Measure=WebParser
UpdateRate=3600
Url=https://weather.codes/united-states-of-america/alabama/
RegExp=(?siU)(?(?=.*#Location#)(.*)\r)
IfMatch=^$
IfMatchAction=[!DisableMeasure "MeasureRainmeter"]
IfNotMatchAction=[!EnableMeasure "MeasureRainmeter"][!CommandMeasure "MeasureRainmeter" "Update"]
StringIndex=1

[MeasureRainmeter]
Measure=WebParser
UpdateRate=3600
Url=https://weather.codes/united-states-of-america/alabama/
RegExp=(?siU)</dd><dt>(.{8})</dt><dd>#Location#\r
Disabled=1

[MeasureWeatherCode]
Measure=WebParser
Url=[MeasureRainmeter]
StringIndex=1
See that [MeasureExists] checks if the city exists. If it does, it enables and updates [MeasureRainmeter]. In this case [MeasureWeatherCode] returns the needed weather code. If on the other hand, the city doesn't exist, [MeasureRainmeter] is kept disabled, so [MeasureWeatherCode] doesn't return a code. You can check for other cities as well.
mkboynton
Posts: 23
Joined: January 31st, 2016, 10:52 pm

Re: Webparser Help

Post by mkboynton »

balala I appreciate all of your help and your code works perfectly. I have run into another issue I can't figure out. Here is the code I have come up with so far.

Code: Select all

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

[Variables]
@Include=#@#var.inc


[MeasureLocation]
Measure=Plugin
Plugin=WebParser
Url=http://api.ipstack.com/check?access_key=###API_KEY_REMOVED###&format=1
RegExp=(?siU)"country_name":"(.*)".*"region_name":"(.*)".*"city":"(.*)".*
UpdateRate=-1
FinishAction=[!Showmeter "MeterChange"]

[MeasureRegion]
Measure=Plugin
Plugin=WebParser
URL=[MeasureLocation]
StringIndex=2
Substitute="A":"a","B":"b","C":"c","D":"d","E":"e","F":"f","G":"g","H":"h","I":"i","J":"j","K":"k","L":"l","M":"m","N":"n","O":"o","P":"p","Q":"q","R":"r","S":"s","T":"t","U":"u","V":"v","W":"w","X":"x","Y":"y","Z":"z"

[MeasureCity]
Measure=Plugin
Plugin=WebParser
URL=[MeasureLocation]
StringIndex=3

[MeasureCountry]
Measure=Plugin
Plugin=WebParser
URL=[MeasureLocation]
StringIndex=1
IfMatch=United States
IfMatchAction=[!SetVariable mUrl "https://weather.codes/united-states-of-america/#Region#/"][!SetVariable mRegExp "(?siU)<dt>(.{8})</dt><dd>#City#\r"]
IfNotMatchAction=[!SetVariable mUrl "https://weather.codes/search/?q=#City#"][!SetVariable mRegExp ""]

[BGblur]
Meter=Image
W=300
H=150
SolidColor=0,0,0,100

[MeterCity]
Meter=String
MeasureName=MeasureCity
AntiAlias=1
FontColor=255,255,255
FontSize=14
X=150
Y=50
StringAlign=CenterCenter

[MeterChange]
Meter=String
AntiAlias=1
FontColor=255,255,255
FontSize=10
Text="Click here if it's your city"
X=r
Y=R
StringAlign=Center
DynamicVariables=1
Hidden=1
LeftMouseDownAction=[!WriteKeyValue Variables Country [MeasureCountry] "#@#var.inc"][!WriteKeyValue Variables Region [MeasureRegion] "#@#var.inc"][!WriteKeyValue Variables City [MeasureCity] "#@#var.inc"][!UpdateMeasure "MeasureWeatherCode"][!Refresh]

[MeasureWeatherCode]
Measure=Plugin
Plugin=WebParser
URL=#mUrl#
RegExp=#mRegExp#
;URL=https://weather.codes/united-states-of-america/#Region#/
;RegExp=(?siU)<dt>(.{8})</dt><dd>#City#\r
UpdateRate=-1
FinishAction=[!HideMeter MeterChange][!Showmeter "MeterCode"]

[MeasureCode]
Measure=Plugin
Plugin=WebParser
URL=[MeasureWeatherCode]
StringIndex=1

[MeterCode]
Meter=String
MeasureName=MeasureCode
FontColor=255,255,255
AntiAlias=1
FontSize=12
Text=%1
X=r
Hidden=1
Y=R
StringAlign=Center
The issue is with MeasureWeatherCode, when the URL and RegExp are hard coded it works fine. When I try to use the variables I created (with the same values) in MeasureCountry I get a webparser error #12006...I don't understand why.
Last edited by Brian on February 20th, 2020, 8:14 am, edited 1 time in total.
Reason: Removed API key
User avatar
balala
Rainmeter Sage
Posts: 16144
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Webparser Help

Post by balala »

mkboynton wrote: February 18th, 2020, 1:36 am The issue is with MeasureWeatherCode, when the URL and RegExp are hard coded it works fine. When I try to use the variables I created (with the same values) in MeasureCountry I get a webparser error #12006...I don't understand why.
It is hard to tell exactly, because I don't have the var.inc file, but most probably you have to add a DynamicVariables=1 option to the [MeasureWeatherCode] measure. This is needed because the measure uses variables created / modified dynamically, which require to set the dynamic variables. I would also remove the UpdateRate=-1 option from the same [MeasureWeatherCode] measure. Doesn't seem a good idea to use such an option on WebParser measures.
If this doesn't help, please post the content of the var.inc file as well.
mkboynton
Posts: 23
Joined: January 31st, 2016, 10:52 pm

Re: Webparser Help

Post by mkboynton »

I didn't think of the dynamicvariables=1. Only other way I can think to do it is to have two measures, one for the USA and the other for the rest of the world.
User avatar
balala
Rainmeter Sage
Posts: 16144
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Webparser Help

Post by balala »

mkboynton wrote: February 18th, 2020, 1:46 pm I didn't think of the dynamicvariables=1. Only other way I can think to do it is to have two measures, one for the USA and the other for the rest of the world.
Doesn't work? I suppose it doesn't and this because when you set values for the mUrl and mRegExp variables (through the IfMatchAction and IfNotMatchAction options of the [MeasureCountry] measure), you also should update the [MeasureWeatherCode] measure. Add the following two bangs to the end of both options, IfMatchActionand and IfNotMatchAction: [!UpdateMeasure "MeasureWeatherCode"][!CommandMeasure "MeasureWeatherCode" "Update"].