It is currently April 20th, 2024, 2:02 pm

Help with OpenWeatherMap API

Get help with creating, editing & fixing problems with skins
etl
Posts: 8
Joined: July 28th, 2019, 5:40 pm

Help with OpenWeatherMap API

Post by etl »

I know this has probably been asked to death, but none of the suggestions I've found on this forum or Reddit have worked, so I'm hoping someone can look at my code and point to what's wrong. Basically, I can't get output from OpenWeatherMap.org. I have an API key, I can view the results just fine in-browser, but when I put it into a RainMeter measure then check the Skins debug page, the value for [MeasureCurrent] remains blank, indicating that it's not successfully fetching the data from OpenWeatherMap. Here's the relevant measures:

Code: Select all

[MeasureIP]
Measure=Plugin
Plugin=WebParser
URL=http://ip-api.com/json
RegExp=(?siU)zip":"(.*)"
UpdateRate=1800
FinishAction=!SetVariable "NewLocation" [MeasureLocationIP]
DynamicVariables=1

[MeasureLocationIP]
Measure=WebParser
URL=[MeasureIP]
StringIndex=1
DynamicVariables=1

[MeasureCurrent]
Measure=Plugin
Plugin=WebParser
UpdateRate=900
Url=api.openweathermap.org/data/2.5/weather?zip=[&MeasureLocationIP],US&units=imperial&appid=[&MyAPI]&mode=xml
RegExp=(?siU),"description":"(.*)".*"temp":(.*),.*icon":"(.*)".*"name":"(.*)"
DynamicVariables=1
Some notes: [MyAPI] is my OpenWeather API key. The RegExp should be correct based off the in-browser OpenWeather output, but I've even tried setting RegExp=(?siU)(.*) just to capture anything, all with no result. I can see that the value of [MeasureLocationIP] is set to a 5-digit zip code as expected. The variable "NewLocation" is also set to my zip code, although I'm not sure how to use the variable in the WebParser URL. I've tried omitting &mode=xml from the URL with no result. If I had to guess, I'd say that the [&MeasureLocationIP] section variable isn't getting processed correctly in the WebParser URL, but I have no idea why that might be.

Can anyone help?
User avatar
balala
Rainmeter Sage
Posts: 16148
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Help with OpenWeatherMap API

Post by balala »

etl wrote: May 11th, 2021, 1:57 am Can anyone help?
It's a little bit difficult, because I don't know the content of the [MyAPI] measure (even if I suppose how does it looks like), but I probably would say that when you're loading / refreshing the skin, both parent WebParser measures are trying to parse the appropriate site. [MeasureIP] succeedes, but [MeasureCurrent] doesn't, because it has to use the value returned by the [MeasureLocationIP] measure in its URL option. While [MeasureLocationIP] doesn't get its value, [MeasureCurrent] can't be executed, but when it does, [MeasureCurrent] isn't executed anymore, because it is immediately when you load / refresh the skin and not once again, a few seconds later (however it is 5 minutes later, due to its UpdateRate=900 option).
If I'm correct, there is an extremely simple approach: you have to disable the second WebParser measure (in this case [MeasureCurrent]), then, when the first measure ([MeasureIP] and appropriately [MeasureLocationIP]) gets the correct value, you have to enable AND UPDATE the second measure ([MeasureCurrent]).
Here is how to do this:
  • Add a Disabled=1 option to the [MeasureCurrent] measure.
  • Add the following option to the [MeasureIP] measure: FinishAction=[!EnableMeasure "MeasureCurrent"][!CommandMeasure "MeasureCurrent" "Update"]
etl
Posts: 8
Joined: July 28th, 2019, 5:40 pm

Re: Help with OpenWeatherMap API

Post by etl »

Thanks for your reply. Your suggestion seems like it should work, logically, but when I tried it, the only change I saw was that the MeasureCurrent measure got grayed out in the debug view, because it was disabled. It seemed like the FinishAction enabled it momentarily, but it just disabled itself again. Really not sure why or how that's happening. I tried commenting out MeasureCurrent's Disabled=1 so that MeasureLocationIP just sends it the Update command, but that still didn't result in MeasureCurrent getting a value. (Also in my post I mentioned that [MyAPI] is just a string value holding my OpenWeather API key, because I didn't feel like posting it in a public forum. I get the exact same (lack of) results from MeasureCurrent if I replace [&MyAPI] with the actual string, so that isn't the issue.)

Here's something interesting, though. If I replace [&MeasureLocationIP] with a regular 5-digit zip code (such that I'm literally just entering the complete URL, which would return the results in a web browser), MeasureCurrent still doesn't return a value. Same if I replace the RegExp with (?siU)(.*) so I have to conclude that the issue is with RainMeter reading the information returned by OpenWeather.
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5402
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Help with OpenWeatherMap API

Post by eclectic-tech »

I do not use OpenWeather, but this may help since you say it does work in your browser.

Try adding a UserAgent value to your Webparser measure; some websites will check for that value before providing the requested info.

Code: Select all

[WebParserParentMeasure]
...
UserAgent=Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0
...
User avatar
balala
Rainmeter Sage
Posts: 16148
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Help with OpenWeatherMap API

Post by balala »

etl wrote: May 11th, 2021, 7:54 pm Thanks for your reply. Your suggestion seems like it should work, logically, but when I tried it, the only change I saw was that the MeasureCurrent measure got grayed out in the debug view, because it was disabled. It seemed like the FinishAction enabled it momentarily, but it just disabled itself again. Really not sure why or how that's happening. I tried commenting out MeasureCurrent's Disabled=1 so that MeasureLocationIP just sends it the Update command, but that still didn't result in MeasureCurrent getting a value. (Also in my post I mentioned that [MyAPI] is just a string value holding my OpenWeather API key, because I didn't feel like posting it in a public forum. I get the exact same (lack of) results from MeasureCurrent if I replace [&MyAPI] with the actual string, so that isn't the issue.)

Here's something interesting, though. If I replace [&MeasureLocationIP] with a regular 5-digit zip code (such that I'm literally just entering the complete URL, which would return the results in a web browser), MeasureCurrent still doesn't return a value. Same if I replace the RegExp with (?siU)(.*) so I have to conclude that the issue is with RainMeter reading the information returned by OpenWeather.
Maybe eclectic-tech's idea might help. If it does it's alright, if doesn't try restarting Rainmeter (I mean a complete restart: right click the Rainmeter icon in the System Tray and click Exit, then restart Rainmeter from the Start menu - you should try this because of this).
If none of the above solutions doesn't help, sorry, but me personally can't help anymore, because if I can't test the skin, have no more idea.