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

Auto Locate by IP weather skin

Get help with creating, editing & fixing problems with skins
User avatar
JamX
Posts: 207
Joined: October 4th, 2019, 2:46 pm

Auto Locate by IP weather skin

Post by JamX »

I'm trying to make my weatherskin again auto locate by IP address.
I use this code:

Code: Select all

[MeasureLocationIP]
Measure=WebParser
URL=https://ipapi.co/json
RegExp=(?siU)"city": "(.*)".*"region": "(.*)".*"country_name": "(.*)"
FinishAction=[!SetVariable city [MeasureCity]][!EnableMeasure MeasureWeatherCode]

[MeasureCountry]
Measure=WebParser
Url=[MeasureLocationIP]
StringIndex=3

[MeasureRegionName]
Measure=WebParser
URL=[MeasureLocationIP]
StringIndex=2

[MeasureCity]
Measure=WebParser
URL=[MeasureLocationIP]
StringIndex=1

[MeasureWeatherCode]
Measure=WebParser
FinishAction=[!SetVariable URLcurrent https://weather.com/#Language#/weather/today/l/[MeasureWeatherCode]]   
URL=https://weather.codes/search/?q=#city#
RegExp=(?siU)<dt>(.*)</dt>
StringIndex=1
DynamicVariables=1
Disabled=1
The MeasureWeatherCode returns NLXX0272 which is correct.
The !Setvariable URLCurrent string is returned correct: https://weather.com/en-GB/weather/today/l/NLXX0272
Capture1.JPG
But when things go wrong:
I get an error: (Fetch error) The URL does not use a recognized protocol (ErrorCode=12006) (Weather_TWC - Auto Locate\Weather_TWC - Auto Locate.ini - [@CurrentAll])
Capture2.JPG
The #URLCurrent# in below code is returning the error.

Code: Select all

[@CurrentAll]
Measure=WebParser
Group=Weather | WeatherCurrent
Url=#URLcurrent#
RegExp=(?siU)^(.*)$
UpdateRate=#UpdateRate#
Why???

When I copy the URLCurrent string from the log and paste it in EDGE address bar I get the correct page:
Capture3.JPG
Here is the package for who is interested:
Weather AutoLoc_.rmskin
You do not have the required permissions to view the files attached to this post.
User avatar
Yincognito
Rainmeter Sage
Posts: 7125
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Auto Locate by IP weather skin

Post by Yincognito »

First of all, I didn't look at the whole skin yet. I will take a look at it, but I don't guarantee anything.

Now that being said, you should be aware that:

1. There are much easier ways of achieveing what you (probably) want to achieve. The first step (the ipapi.co one) is useful, but you don't need to query another site (i.e. weather.codes in the posted example) to get your Weather.com location code - you can simply use your latitude and longitude (that you got from ipapi.co) directly into weather.com, instead of a "location code", like here. In other words, this will save you a step in the process: instead of querying 3 sites for something, you'll query just 2, where the 2nd will be weather.com itself.

2. Just from looking at the parts of the code you posted, I can see a couple of "problems":
- you don't seem to do anything with the #URLcurrent# variable there. Yes, you set it correctly, but you didn't command the target WebParser measure to update or anything like that, so the variable isn't used from a practical point of view (until the next update set by #UpdateRate#, of course, but I have no idea when that is set to happen)
- you didn't add DynamicVariables=1 to the [@CurrentAll] measure, so the measure won't get the actual value of either #URLcurrent# or #UpdateRate#, in the (highly probably) case they both dynamically change while the skin is active.
- I could be wrong, but from my experience, you'll have little chances of success with:

Code: Select all

[!SetVariable URLcurrent https://weather.com/#Language#/weather/today/l/[MeasureWeatherCode]]
from the [MeasureWeatherCode] measure, because the WebParser parent measure is not its capture. The way this should be done is to create a WebParser child measure that grabs the capture from [MeasureWeatherCode]'s RegExp (i.e. the (.*) from (?siU)<dt>(.*)</dt>) using StringIndex=1, then try setting the variable from there (using, say, that measure's OnUpdateAction option to do it).
- you seem to try to make your [MeasureWeatherCode] measure both a parent and a child WebParser measure. I doubt that's the right way to do it. Instead, you should be aware that an actual URL is used in a parent WebParser measure, while in a child WebParser measure, the URL option usually takes the form of its parent measure and the StringIndex option is normally used in the child, and not the parent. I suggest checking a bit the "WebParser and Dynamic Variables" section here (and the skin samples) to get some hints on how things should be approached.

Hopefully, some of the things above will help you correct or improve the code and get what you want in your skin. If not, I'll think about posting a basic sample of getting the needed result, without trying too much to modify the packed skin you linked to. Then, it will be up to you to port what is useful into the original skin and try to make it work as well as that sample.

EDIT
And here is the sample (I just realized that it took me less to build this sample than to write the explanation, LOL ... but then, maybe both will help):

Code: Select all

[Variables]
Language=en-GB
URLCurrent=""

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

---Measures---

[MeasureLocationGeocode]
Measure=WebParser
URL=https://ipapi.co/json
RegExp="(?siU).*"latitude": (.*),.*"longitude": (.*),.*"
UpdateRate=600
FinishAction=[!UpdateMeasureGroup GeocodeGroup][!SetVariable URLCurrent "https://weather.com/#Language#/weather/today/l/[MeasureLatitude],[MeasureLongitude]"][!EnableMeasure "@CurrentAll"][!UpdateMeasure "@CurrentAll"][!CommandMeasure @CurrentAll "Update"]
DynamicVariables=1

[MeasureLatitude]
Group=GeocodeGroup
Measure=WebParser
URL=[MeasureLocationGeocode]
StringIndex=1
UpdateDivider=-1

[MeasureLongitude]
Group=GeocodeGroup
Measure=WebParser
URL=[MeasureLocationGeocode]
StringIndex=2
UpdateDivider=-1

[@CurrentAll]
Disabled=1
Measure=WebParser
Group=Weather | WeatherCurrent
URL="#URLCurrent#"
RegExp="(?siU).*"vt1observation":\{.*,"temperature":(.*),.*\}.*"
FinishAction=[!UpdateMeasureGroup WeatherGroup]
UpdateRate=-1
DynamicVariables=1

[MeasureTemperature]
Group=WeatherGroup
Measure=WebParser
URL=[@CurrentAll]
StringIndex=1
UpdateDivider=-1

---Meters---

[MeterAutoLocateDemo]
Meter=STRING
X=5
Y=5
FontFace=Consolas
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
FontSize=16
AntiAlias=1
MeasureName=MeasureLatitude
MeasureName2=MeasureLongitude
MeasureName3=MeasureTemperature
Text="Latitude    = %1#CRLF#Longitude   = %2#CRLF#Temperature = %3 [\x00B0]C"
DynamicVariables=1
Most of these measures have UpdateDivider=-1 set on them, just to be on the safe side (me, I like to do things on demand). Everything is triggered step by step by the [MeasureLocationGeocode]'s update, and the relevant data is displayed in [MeterAutoLocateDemo]. Good luck in adapting this in your skin. ;-)
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
balala
Rainmeter Sage
Posts: 16144
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Auto Locate by IP weather skin

Post by balala »

JamX wrote: March 4th, 2020, 9:42 am The #URLCurrent# in below code is returning the error.

Code: Select all

[@CurrentAll]
Measure=WebParser
Group=Weather | WeatherCurrent
Url=#URLcurrent#
RegExp=(?siU)^(.*)$
UpdateRate=#UpdateRate#
Why???
As Yincognito very well says, take care that if you set the value of a variable (in this case URLCurrent) with a !SetVariable bang, you have to add a DynamicVariables=1 option to the measure or meter where you wanna use that variable. In this case, the [@CurrentAll] measure should get such an option, so add it.
Secondly, when the above variable is set (so when the [@CurrentAll] measure gets correctly its URL option), you should have to update the measure, in order to let it parse the inaformation from internet. I'd do this with a !CommandMeasure bang, but this should be preceded by a !UpdateMeasure, because otherwise before the next update cycle, the measure doesn't know the newly sett URL. So, add the following two bangs to the FinishAction option of the [MeasureWeatherCode] measure: FinishAction=[!SetVariable URLcurrent "https://weather.com/#Language#/weather/today/l/[MeasureWeatherCode]"][!UpdateMeasure "@CurrentAll"][!CommandMeasure "@CurrentAll" "Update"]. Note that I also added quotations around the address you have to set, which always is a good idea.
User avatar
Yincognito
Rainmeter Sage
Posts: 7125
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Auto Locate by IP weather skin

Post by Yincognito »

balala wrote: March 4th, 2020, 5:14 pmI'd do this with a !CommandMeasure bang, but this should be preceded by a !UpdateMeasure, because otherwise before the next update cycle, the measure doesn't know the newly set URL.
That's an excellent point, balala. I've actually included the !UpdateMeasure there mainly because from my experience it's needed after enabling a previously disabled measure, but you described the real reason for it. In simple terms (correct me if I'm wrong), !UpdateMeasure updates the measure's options/variables, while !CommandMeasure "Update" updates its web content (in the case of a WebParser parent measure).
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
balala
Rainmeter Sage
Posts: 16144
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Auto Locate by IP weather skin

Post by balala »

Yincognito wrote: March 4th, 2020, 5:58 pm That's an excellent point, balala. I've actually included the !UpdateMeasure there mainly because from my experience it's needed after enabling a previously disabled measure, but you described the real reason for it.
If that's a "normal" measure (for instance a Calc) and if the Update of the skin is set to the default Update=1000 (as in most cases), when you enable the previously disabled measure, it is updated on the next update cycle (at the latest after one second). Such in a case not always and not absolutely needed to update the measure, however many times it's a good idea.
But if the measure we're talking about is a WebParser, it is controlled, beside the UpdateDivider (which in most cases on this kind of measures is recommended to be set to the default UpdateDivider=1 - see the What about UpdateRate? section of this), by the UpdateRate option as well. Usually this is set to a larger value, in most cases at least 5 - 10 minutes. Now if you set the URL option through a !SetVariable or a !SetOption bang, you have to update the measure right when you set the URL, to get it acquiring immediately the information from the web. If you don't do this, you have to wait up to UpdateRate x UpdateDivider second and the measure will parse the information only then. The immediate update is done by the !CommandMeasure bang.
Yincognito wrote: March 4th, 2020, 5:58 pm In simple terms (correct me if I'm wrong), !UpdateMeasure updates the measure's options/variables, while !CommandMeasure "Update" updates its web content (in the case of a WebParser parent measure).
Well, in fact correctly that bang is !CommandMeasure "ParentWebParserMeasureName" "Update" (the name of the parent WebParser measure must be included), but yes, in principle this is right.

Does it work now?
User avatar
Yincognito
Rainmeter Sage
Posts: 7125
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Auto Locate by IP weather skin

Post by Yincognito »

balala wrote: March 4th, 2020, 6:34 pm If that's a "normal" measure (for instance a Calc) and if the Update of the skin is set to the default Update=1000 (as in most cases), when you enable the previously disabled measure, it is updated on the next update cycle (at the latest after one second). Such in a case not always and not absolutely needed to update the measure, however many times it's a good idea.
Yes, I'm talking about my experiences with Calc measures (counters) that need to "restart" imediately after being disabled and enabled back again. I am using this method to "reset" a Calc "counter" to 0 before restarting it. Sure I could have used a !SetOption on its Formula option, but when you have long and complicated formulas to restore, it's easier to just disable the measure and enable it back again. Oh, and they're updated every 24 milliseconds... :sly:
balala wrote: March 4th, 2020, 6:34 pmWell, in fact correctly that bang is !CommandMeasure "ParentWebParserMeasureName" "Update" (the name of the parent WebParser measure must be included), but yes, in principle this is right.
Ha! I knew you'd mention this, but I posted like that anyway. I was lazy to write some hypothetical measure name there, I figured it would be obvious that some measure name was to be included in the bang. You don't miss anything, do you? :D
balala wrote: March 4th, 2020, 6:34 pmDoes it work now?
That's for the OP to see if it does. He has the last word on this. ;-)
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
balala
Rainmeter Sage
Posts: 16144
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Auto Locate by IP weather skin

Post by balala »

Yincognito wrote: March 4th, 2020, 6:52 pm Yes, I'm talking about my experiences with Calc measures (counters) that need to "restart" imediately after being disabled and enabled back again. I am using this method to "reset" a Calc "counter" to 0 before restarting it. Sure I could have used a !SetOption on its Formula option, but when you have long and complicated formulas to restore, it's easier to just disable the measure and enable it back again. Oh, and they're updated every 24 milliseconds... :sly:
To restart such a measure there is not needed to set the Update to 24, as I think you've done, based on the
Yincognito wrote: March 4th, 2020, 6:52 pm Oh, and they're updated every 24 milliseconds...
sentence.

For such an operation you have to disable the measure, then immediately update it (very important), then reenable it. This can be done with the following three bangs: [!DisableMeasure "MeasureToBeRestarted"][!UpdateMeasure "MeasureToBeRestarted"][!EnableMeasure "MeasureToBeRestarted"]. Note the most important bang, [!UpdateMeasure "MeasureToBeRestarted"], which has to be between !DisableMeasure and !EnableMeasure.
Some details here: https://forum.rainmeter.net/viewtopic.php?p=94022#p94022
Yincognito wrote: March 4th, 2020, 6:52 pm Ha! I knew you'd mention this, but I posted like that anyway. I was lazy to write some hypothetical measure name there, I figured it would be obvious that some measure name was to be included in the bang. You don't miss anything, do you? :D
:thumbup:
Yincognito wrote: March 4th, 2020, 6:52 pm That's for the OP to see if it does. He has the last word on this. ;-)
Right. Sorry for my negligence. I should have to post this as a reply to one of his posts.
User avatar
Yincognito
Rainmeter Sage
Posts: 7125
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Auto Locate by IP weather skin

Post by Yincognito »

balala wrote: March 4th, 2020, 7:11 pmTo restart such a measure there is not needed to set the Update to 24, as I think you've done, based on your sentence.
No, I meant the update of the skin itself is set to 24 ms, as it's animated. The 24 ms update of the counter Calc measure is a consequence of that, and it has nothing to do with the process of resetting the counter. I mentioned this because obviously I need the reset to happen "right now", which means that all the actions/bangs in the counter happen every 24 ms, including occasional resets.
balala wrote: March 4th, 2020, 7:11 pmFor such an operation you have to disable the measure, then immediately update it (very important), then reenable it. This can be done with the following three bangs: [!DisableMeasure "MeasureToBeRestarted"][!UpdateMeasure "MeasureToBeRestarted"][!EnableMeasure "MeasureToBeRestarted"]. Note the most important bang, [!UpdateMeasure "MeasureToBeRestarted"], which has to be between !DisableMeasure and !EnableMeasure.
Some details here: https://forum.rainmeter.net/viewtopic.php?p=94022#p94022
Yes, that's exactly how I've done this (plus, an [!UpdateMeasure "MeasureToBeRestarted"] after enabling it, to "set things in motion" again). To be clear, I was referring to my experiences from the past with this, not to things that I'm actively looking to do right now. The matter is already solved since a couple of years back (possibly with a little help from your side as well at that time) - I was mentioning it only because it was related to the OP also trying to enable/update back a previously disabled measure, and missing some bits here and there in the process.
balala wrote: March 4th, 2020, 7:11 pmRight. Sorry for my negligence. I should have to post this as a reply to one of his posts.
That's ok, don't worry. Usually when an OP doesn't say anything anymore after starting a thread, it means that he either found a way of doing what he aimed to do, or that he has given up on his attempt for various reasons (e.g. due to perceived complexity, finding another skin that does that already, etc).
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
balala
Rainmeter Sage
Posts: 16144
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Auto Locate by IP weather skin

Post by balala »

Yincognito wrote: March 5th, 2020, 10:38 am (plus, an [!UpdateMeasure "MeasureToBeRestarted"] after enabling it, to "set things in motion" again).
Adding such a bang as well, updates twice the measure. This means that if you use only the !DisableMeasure, the !UpdateMeasure and finally the !EnableMeasure bangs, when you click, the measure restarts from 1, but if you do add twice the !UpdateMeasure bang, on click it starts from 2. I don't say any of these solutions is better then the other, this depends on your needs.
Yincognito wrote: March 5th, 2020, 10:38 am That's ok, don't worry. Usually when an OP doesn't say anything anymore after starting a thread, it means that he either found a way of doing what he aimed to do, or that he has given up on his attempt for various reasons
Yeah, but even in such a case, he could come back and say he is not interested anymore in finding a solution, he found a good solution or whatever. Just let us know what's going on.
User avatar
Yincognito
Rainmeter Sage
Posts: 7125
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Auto Locate by IP weather skin

Post by Yincognito »

balala wrote: March 5th, 2020, 4:52 pmAdding such a bang as well, updates twice the measure. This means that if you use only the !DisableMeasure, the !UpdateMeasure and finally the !EnableMeasure bangs, when you click, the measure restarts from 1, but if you do add twice the !UpdateMeasure bang, on click it starts from 2. I don't say any of these solutions is better then the other, this depends on your needs.
You're absolutely correct - only now I noticed that. Of course, it's hard to notice since one unit of increment is so small compared to the whole interval of "counting" and everything works fine, but I'll have to correct this eventually. Many thanks for making me realize this, it's already on my to do list - I guess that when I wrote the code I added the update after enabling just because it looked "symmetric" and "similar" to the update after disabling, rather than because it was needed...

Nice job, balala! :thumbup:
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth