Active TopicsActive Topics  Display List of Forum MembersMemberlist  Search The ForumSearch  HelpHelp
  RegisterRegister  LoginLogin
PowerHome Macros, Scripts, and Formulas Repository
 PowerHome Messageboard : PowerHome Macros, Scripts, and Formulas Repository
Subject Topic: Super Easy & Reliable Get Weather Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
GadgetGuy
Super User
Super User
Avatar

Joined: June 01 2008
Location: United States
Online Status: Offline
Posts: 942
Posted: December 14 2014 at 19:34 | IP Logged Quote GadgetGuy

A short while ago Syonker posted an incredible discovery about a free API hook into the Weather Underground weather server.

Syonker's comments can be found here, about half way down the first page of the Thread...
http://www.power-home.com/forum/forum_posts.asp?TID=3346&PN= 1

The beauty of Syonker's find is that the data is all parsed and organized and thus not only incredibly easy to scape for specific information, but VERY FAST.

Here is a sample of about half of the returned data...
     . . . .
     "weather":"Clear",
     "temperature_string":"69.5 F (20.8 C)",
     "temp_f":69.5,
     "temp_c":20.8,
     "relative_humidity":"57%",
     "wind_string":"From the West at 1.2 MPH Gusting to 3.0 MPH",
     "wind_dir":"West",
     "wind_degrees":270,
     "wind_mph":1.2,
     "wind_gust_mph":"3.0",
     "wind_kph":1.9,
     "wind_gust_kph":"4.8",
     "pressure_mb":"1015",
     "pressure_in":"29.97",
     "pressure_trend":"+",
     "dewpoint_string":"54 F (12 C)",
     "dewpoint_f":54,
     "dewpoint_c":12,
     "heat_index_string":"NA",
     "heat_index_f":"NA",
     "heat_index_c":"NA",
     "windchill_string":"NA",
     "windchill_f":"NA",
     "windchill_c":"NA",
     "feelslike_string":"69.5 F (20.8 C)",
     "feelslike_f":"69.5",
     "feelslike_c":"20.8",
     "visibility_mi":"10.0",
     "visibility_km":"16.1",
     "solarradiation":"--",
     "UV":"1","precip_1 hr_string":" 0.00 in ( 0 mm)",
     . . . .

See Syonker's well written post (referenced above).

The purpose of this post is to add to the above by utilizing the new "urlscraper" Plugin recently added to PH.

This plugin runs in the background in its own thread, so no burden is placed on PH for the data fetching.

Only when the data is obtained from the referenced URL is a PH Trigger fired so that you can use the extracted data, as you wish.

Here is a quick run down of my scrapping of the Weather Underground API returned information.

This is the urlscraper.ini file contents placed in the powerhome/plugins folder...
Code:

[config]
urlcount=1

[URL_1]
url=http://api.wunderground.com/api/7d6e*******ccec1/conditi ons/q/MI/Chelsea.json
freq=15.0
scrapecount=1

[URL_1_1]
regexsearch="temp_f":(.+),[\s\S]*?"relative_humidity":"(.+)%[\s\S]*?"wind_mph":(.+),[\s\S]*?"feelslike_f":"(.+)"[\s\S]*?"UV":"(.+)","prec
regexoccur=1
regexflags=0


where the "7d6e*******ccec1" string is your own private access code (see Synoker's description on how to obtain one).

The above regexsearch finds the temp/humidity/wind speed/feelslike data elements in the Weather data.

NOTE: the regex search that is done uses the VBScript regular expression engine (the same that is used in the new ph_regex2 functions). This is DIFFERENT from the older PH regex syntax. Full details, and good tutorials, on the protocol can be found here ...

        http://www.regular-expressions.info/quickstart.html

NOTE: In the following discussion the curly braces {} will be used to represent square brackets since the Forum changes a right square bracket into a ">" , and in some cases "capital-S" to represent "S",since a "slash-S" turns into a "/s".

Briefly the temp_f":(.+), string looks for temp f": and then grabs all the characters between there and the ,.

It sticks that find in TEMP1 for use by the Trigger event handler. Subsequent snaps will fill TEMP2, TEMP3, etc.

The {\s\S}*?"relative_humidity":"(.+)% search is a bit more complicated but not too bad. The {\s\S} indicates to skip over everything that is any "whitespace" "\s" character (ie, space, tab, line break, form feed) and the "\capital-S" matches any non-whitespace character (ie, all characters).

The (.+), denotes all characters up to the next comma will be "snapped" out of the data string (the temperature in this case) for this regex search. This is a greedy search that will find the last occurrence on a line. We are safe using it here because there are no data repeats before the next Return code.

Then a new snap search begins with "relative humidity:;" and then grabs all the characters thereafter until a % is encountered and puts that data in TEMP2. Each subsequent data find is put in ever increasingly numbered TEMPS. Note the *? in this search command. That indicates that the skip-over effort should not be "Greedy" (that is possibly finding another match after the 1st is found, which we would want to happen). A "lazy" (non-greedy) search stops when it finds the first match.

Note the last search (for the UV data) goes beyond a simple "," to end the ".+" skip-over instruction as there are multiple commas on this line and we want to make sure we don't grab data all the way to near the end of the line. A "?" (non-greedy) character would have done the same thing, and stopped the skip at the first comma.

For detailed information on the URLscraper see its associated Help file which will be included with the new 2.1.5b PH release.

The PH Trigger setup looks like this...


and the very simple macro that results from all this is...


It doesn't get much simpler than this!!

In addition, it has been the most reliable and error free weather data grabbing I have ever achieved.

Enjoy.


Edited by GadgetGuy - December 16 2014 at 11:05


__________________
Ken B - Live every day like it's your last. Eventually, you'll get it right!
Back to Top View GadgetGuy's Profile Search for other posts by GadgetGuy
 

If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Post ReplyPost New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum