Weather Scraper: Current Conditions

General HouseBot discussion. Any issues that don't fit into any of the other topics belong here.
Post Reply
Osler
HouseBot Guru
Posts: 742
Joined: Fri Feb 03, 2006 11:18 pm

Weather Scraper: Current Conditions

Post by Osler »

Find below the vbscript code that uses the NOAA mobile users website to retrieve current weather conditions for your area (US only).

Code: Select all

'Step 1: Create a Script Device called "CurrentWeather"
'Step 2: Under "Settings", "Property Manager" add the following properties: CWLocation, CWTime, CWWeather, CWTemperature, CWHumidity, CWWindSpeed, CWBarometer, and CWDewPoint
'====Property Name and Description should be the same
'Step 3: Go to NOAA's Mobile website (http://mobile.srh.weather.gov), enter your zip code and hit return, then select "Current Conditions"
'Step 4: Cut and Paste the web address from the top of the "Current Conditions" page for you area into the sURLCurrent line below
'Step 5: Save the script into the HouseBot Script file
'Step 6: Click on the "CurrentWeather" Script Device and point it to the script you just saved
'Step 7: Run the script from the Script Device - a message box will appear initially showing you the results (you can do this prior to adding all of the properties above to ensure it works correctly)
'Step 8: Edit the script and following the instructions for removing the MsgBox in the last three lines


'This is a script to scrape current weather information from the NOAA mobile website

Dim sCurrent, oXMLHTTP, sURLCurrent, Parse1, Parse2
Dim sLocation, sTime, sWeather, sTemperature, sHumidity, sWindSpeed, sBarometer, sDewPoint

'Enter the corresponding web address for your area into the following variables from the NOAA mobile users website http://www.srh.noaa.gov
sURLCurrent = "http://www.srh.noaa.gov/port/port_mp_ns.php?select=4&CityName=Flower%20Mound&site=FWD&State=TX&warnzone=TXZ103"

'Create the xmlhttp object so you can use it to gather data from the webpages
Set oXMLHTTP = CreateObject("microsoft.xmlhttp")

'Get the current data from NOAA
oXMLHTTP.Open "GET", sURLCurrent, True
oXMLHTTP.Send

'Do some error trapping to ensure the server is up and all data was sent
If oXMLHTTP.readyState = 4 Then
     If oXMLHTTP.status = 200 Then
          'Save the html from the site to the appropriate variable
          sCurrent = oXMLHTTP.responseText
     Else
          alert("There was a problem retrieving the current weather data.")
     End If
End If

'Destroy the object you created
Set oXMLHTTP = Nothing

'Now parse the data out of the file

'Search for the location
Parse1 = InStr(sCurrent, "Current Local Conditions at:<br>")
Parse1 = Parse1 + 32
Parse2 = InStr(Parse1, sCurrent, "<br>")
sLocation = Trim(Mid(sCurrent, Parse1, Parse2 - Parse1))
SetPropertyValue "CurrentWeather.CWLocation", sLocation

'Search for the update time
Parse1 = InStr(sCurrent, "Last Update: ")
Parse1 = Parse1 + 13
Parse2 = InStr(Parse1, sCurrent, "<br>")
sTime = Trim(Mid(sCurrent, Parse1, Parse2 - Parse1))
SetPropertyValue "CurrentWeather.CWTime", sTime

'Search for the weather description
Parse1 = InStr(sCurrent, "Weather: ")
Parse1 = Parse1 + 9
Parse2 = InStr(Parse1, sCurrent, "<br>")
sWeather = Trim(Mid(sCurrent, Parse1, (Parse2 - Parse1)))
SetPropertyValue "CurrentWeather.CWWeather", sWeather

'Search for the temperature
Parse1 = InStr(sCurrent, "Temperature: ")
Parse1 = Parse1 + 13
Parse2 = InStr(Parse1, sCurrent, "&deg")
sTemperature = Trim(Mid(sCurrent, Parse1, Parse2 - Parse1))
sTemperature = sTemperature & " F"
SetPropertyValue "CurrentWeather.CWTemperature", sTemperature

'Search for the humidity
Parse1 = InStr(sCurrent, "Humidity: ")
Parse1 = Parse1 + 10
Parse2 = InStr(Parse1, sCurrent, " %")
sHumidity = Trim(Mid(sCurrent, Parse1, Parse2 - Parse1))
sHumidity = sHumidity & " %"
SetPropertyValue "CurrentWeather.CWHumidity", sHumidity

'Search for the wind speed
Parse1 = InStr(sCurrent, "Wind Speed: ")
Parse1 = Parse1 + 12
Parse2 = InStr(Parse1, sCurrent, "<br>")
sWindSpeed = Trim(Mid(sCurrent, Parse1, Parse2 - Parse1))
SetPropertyValue "CurrentWeather.CWWindSpeed", sWindSpeed

'serach for the barometric pressure
Parse1 = InStr(sCurrent, "Barometer: ")
Parse1 = Parse1 + 11
Parse2 = InStr(Parse1, sCurrent, "<br>")
sBarometer = Trim(Mid(sCurrent, Parse1, Parse2-Parse1))
SetPropertyValue "CurrentWeather.CWBarometer", sBarometer

'Search for the dewpoint
Parse1 = InStr(sCurrent, "Dewpoint: ")
Parse1 = Parse1 + 10
Parse2 = InStr(Parse1, sCurrent, "&deg")
sDewPoint = Trim(Mid(sCurrent, Parse1, (Parse2 ) - Parse1))
sDewPoint = sDewPoint & " F"
SetPropertyValue "CurrentWeather.CWDewPoint", sDewPoint

'Now post all the data to a message box to see if the script is working correctly
'(place a single quote in front of the MsgBox line when you are satisfied all is working)
'MsgBox sLocation & vbCR & sTime & vbCR & sWeather & vbCR & sTemperature & vbCR & sHumidity & vbCR & sWindSpeed & vbCR & sBarometer & vbCR & sDewPoint
Osler
Post Reply