It is currently April 24th, 2024, 9:31 pm

Generate Automatic WeatherCode from IP address in use

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

Generate Automatic WeatherCode from IP address in use

Post by JamX »

I'm trying to make a weather skin with automatic location based on IP address
=> Retrieving city name from IP address Works!
=> Retrieving weatherCode for this city does NOT work

The [measureCity] result is not correct incorporated in the URL of [MeasureWeatherCode]
I also tried the Setvariable option but this also did not work ( See ; Lines )

Maybe I'm doing it wrong so help is welcome..

Code: Select all

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

[Variables]
City=Heerlen

[MeasureLocation]
Measure=Plugin
Plugin=WebParser
Url=http://ip-api.com/json
RegExp=(?siU)"city":"(.*)","country":"(.*)","countryCode":"(.*)"

[MeasureCity]
Measure=Plugin
Plugin=WebParser
URL=[MeasureLocation]
StringIndex=1
DynamicVariables=1
;[!Setvariable City [MeasureCity]

[MeasureWeatherCode]
Measure=Plugin
Plugin=WebParser
Url=http://wxdata.weather.com/wxdata/search/search?where=[&MeasureCity]
;Url=http://wxdata.weather.com/wxdata/search/search?where=#City#
RegExp=(?siU).*<loc id="(.*)"

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

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

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

[MeterCode]
Meter=String
MeasureName=MeasureCode
FontColor=255,255,255
AntiAlias=1
FontSize=12
Text=%1
X=r
Hidden=1
Y=R
StringAlign=Center
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Generate Automatic WeatherCode from IP address in use

Post by jsmorley »

Something like this worked for me:

Code: Select all

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

[Variables]

[MeasureLocation]
Measure=WebParser
Url=http://ip-api.com/json
RegExp=(?siU)"city":"(.*)".*"country":"(.*)".*"regionName":"(.*)"
FinishAction=[!CommandMeasure MeasureWeatherCode "Update"]

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

[MeasureCountry]
Measure=WebParser
URL=[MeasureLocation]
StringIndex=2

[MeasureRegion]
Measure=WebParser
URL=[MeasureLocation]
StringIndex=3

[MeasureWeatherCode]
Measure=WebParser
Url=http://wxdata.weather.com/wxdata/search/search?where=[&MeasureCity] [&MeasureRegion] [&MeasureCountry]
DynamicVariables=1
RegExp=(?siU).*<loc id="(.*)"

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

[MeterBack]
Meter=Shape
Shape=Rectangle 1,1,302,132 | StrokeWidth 1 | Stroke Color 150,150,150,255 | Fill Color 47,47,47,120

[MeterLocation]
Meter=String
MeasureName=MeasureCity
MeasureName2=MeasureRegion
MeasureName3=MeasureCountry
X=150
Y=20
FontSize=14
FontColor=255,255,255,255
AntiAlias=1
StringAlign=Center
Text=%1#CRLF#%2#CRLF#%3

[MeterCode]
Meter=String
MeasureName=MeasureCode
X=r
Y=10R
FontSize=12
FontColor=255,255,255,255
AntiAlias=1
StringAlign=Center

1.jpg

2.jpg


It's not the perfect location, not the one I use in my skin(s), but since it is based on the local "point of presence" of your ISP, it's likely to be reasonably close.

I really think you need to use the combination of "city/region/country" to zero in. City and country alone will give you just boatloads of hits for most common names.
You do not have the required permissions to view the files attached to this post.
User avatar
balala
Rainmeter Sage
Posts: 16168
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Generate Automatic WeatherCode from IP address in use

Post by balala »

For such a thing I'm using a little bit different approach: I'm using a URL which directly returns the country, city, geographical coordinates, weather code and time zone (and even more, but these are the information I need), based on current IP address:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1

[Variables]

[MeasureIPinfo]
Measure=WebParser
UpdateRate=3600
Url=https://www.iplocationtools.com/
RegExp=(?siU)<span class="flag-icon flag-icon-ro tooltips" data-placement="top" title="\[.*] .*"></span>(.*)</td>.*<div><label><strong>Coordinates of City</strong></label></div>.*<i class="fa fa-map-marker"></i> .*&deg;.*".*&nbsp;&nbsp;&nbsp;.*&deg;.*".*<br>\((.*), (.*)\).*<div><label><strong>Local Time</strong></label></div>.*\(UTC (.*)\).*<div><label><strong>Weather Station</strong></label></div>.*(.*) \((.*)\)

[MeasureIPCountry]
Measure=WebParser
Url=[MeasureIPinfo]
StringIndex=1
RegExpSubstitute=1
Substitute="	":"","\n":"","^\s*(.*)$":"\1","\\1":""
Group=Location

[MeasureIPCity]
Measure=WebParser
Url=[MeasureIPinfo]
StringIndex=5
RegExpSubstitute=1
Substitute="	":"","\n":""
Group=Location

[MeasureIPLatitude]
Measure=WebParser
Url=[MeasureIPinfo]
StringIndex=2
Group=Location

[MeasureIPLongitude]
Measure=WebParser
Url=[MeasureIPinfo]
StringIndex=3
Group=Location

[MeasureIPWeatherCode]
Measure=WebParser
Url=[MeasureIPinfo]
StringIndex=6
Group=Location

[MeasureIPTimeZone]
Measure=WebParser
Url=[MeasureIPinfo]
StringIndex=4
Group=Location
Substitute="+":""
Disabled=1

[Uptime]
Meter=STRING
MeasureName=MeasureIPCountry
MeasureName2=MeasureIPCity
MeasureName3=MeasureIPLatitude
MeasureName4=MeasureIPLongitude
MeasureName5=MeasureIPWeatherCode
MeasureName6=MeasureIPTimeZone
X=0
Y=0
Padding=15,5,15,5
FontColor=220,220,220
FontEffectColor=0,0,0
StringEffect=Shadow
SolidColor=0,0,0,150
FontSize=8
FontFace=Segoe UI
StringStyle=BOLD
StringAlign=LEFT
AntiAlias=1
Text=Country: %1#CRLF#City: %2#CRLF#Latitude: %3#CRLF#Longitude: %4#CRLF#Weather code: %5#CRLF#Time zone: %6
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Generate Automatic WeatherCode from IP address in use

Post by jsmorley »

My only concern with "screen scraping" is that it can vary a bit depending on the detected browser, country and other factors. For instance, I get a parsing error with your code from the USA with Firefox, where I suspect you don't.

I could fix the regular expression, but I couldn't be certain it would then work for others.

Sometimes screen scraping is the best or even only way. I guess I'd be tempted to use a UserAgent string to at least force a distinct browser.
User avatar
balala
Rainmeter Sage
Posts: 16168
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Generate Automatic WeatherCode from IP address in use

Post by balala »

jsmorley wrote: October 4th, 2019, 6:15 pm My only concern with "screen scraping" is that it can vary a bit depending on the detected browser, country and other factors. For instance, I get a parsing error with your code from the USA with Firefox, where I suspect you don't.

Sometimes screen scraping is the best or even only way. I guess I'd be tempted to use a UserAgent string to at least force a distinct browser.
I'm not sure I understand what you mean by "screen scraping", but no, I'm not getting error messages at all when running my previous code.
And beside getting the error message, does the code returns the needed information?
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Generate Automatic WeatherCode from IP address in use

Post by jsmorley »

balala wrote: October 4th, 2019, 6:27 pm I'm not sure I understand what you mean by "screen scraping", but no, I'm not getting error messages at all when running my previous code.
And beside getting the error message, does the code returns the needed information?
I just get a regular expression failure on the RegExp. I haven't looked at it really.

By "screen scraping", I mean just parsing web page source, that isn't certain to be standard, but might adjust itself based on what it thinks you are using to access it. So you are parsing more or less what you get on the "screen", rather than a json or xml "feed", that is going to be consistent.

As an example, these forums will output something entirely different if you are on a mobile device.
User avatar
pul53dr1v3r
Posts: 442
Joined: July 30th, 2014, 10:30 am

Re: Generate Automatic WeatherCode from IP address in use

Post by pul53dr1v3r »

i'm also getting error: RegExp matching error (-1) (test\WeatherCode_by_balala.ini - [MeasureIPinfo])
balala wrote: October 4th, 2019, 6:27 pm
probably what jsmorley said is why i was unable to get any location data when pressed the Detect button of your Mirage suite...? But it worked on any PC you tested. :???:
Last edited by pul53dr1v3r on October 4th, 2019, 7:20 pm, edited 1 time in total.
User avatar
balala
Rainmeter Sage
Posts: 16168
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Generate Automatic WeatherCode from IP address in use

Post by balala »

jsmorley wrote: October 4th, 2019, 6:28 pm I just get a regular expression failure on the RegExp. I haven't looked at it really.
And nothing returned by skin?
jsmorley wrote: October 4th, 2019, 6:28 pm By "screen scraping", I mean just parsing web page source, that isn't certain to be standard, but might adjust itself based on what it thinks you are using to access it. So you are parsing more or less what you get on the "screen", rather than a json or xml "feed", that is going to be consistent.
Ok, understood now.
jsmorley wrote: October 4th, 2019, 6:28 pm As an example, these forums will output something entirely different if you are on a mobile device.
Right, this is true. But does this matter when using WebParser measures to get the results into a skin? How does the skin detects the used web browser? I think I read once something about this here on forum, but I don't know what and I'm not sure now how to look for it.
User avatar
balala
Rainmeter Sage
Posts: 16168
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Generate Automatic WeatherCode from IP address in use

Post by balala »

Pul53dr1v3r wrote: October 4th, 2019, 7:10 pm i'm also getting error: RegExp matching error (-1) (test\WeatherCode_by_balala.ini - [MeasureIPinfo])
But same question as for jsmorley above: does the skin return something, beside the error message?
Pul53dr1v3r wrote: October 4th, 2019, 7:10 pm to balala: probably what jsmorley said is why i was unable to get any location data when pressed the Detect button of your Mirage suite...? But it worked on any PC you tested. :???:
Take care, although I sent you the Mirage for testing, the public don't know yet what Mirage is.
If anyone interested: it'll be my brand new skins suite, which I'll publish soon. Although not long ago I thought it is ready, in meantime I found a bug, which I'm working on now to fix it.
User avatar
pul53dr1v3r
Posts: 442
Joined: July 30th, 2014, 10:30 am

Re: Generate Automatic WeatherCode from IP address in use

Post by pul53dr1v3r »

balala wrote: October 4th, 2019, 7:23 pm But same question as for jsmorley above: does the skin return something, beside the error message?
Not, absolutely nothing besides the error.

Image