In this tutorial we will create a Weather software using our favorite cross-platform software development environment REALbasic. Learn how to download a file using HTTPSocket, how to format text and how to find text between two strings. Watch it, learn it, enjoy it!
Add to your Window Class 8 x StaticText Control and 1 x Canvas Control. Try to put this controls like on the screenshot below. In our example we will download and format weather data for Polish city Wroclaw. Of course there are no problems to change it for your city. Add in code editor a New Property s As String. We will hold there the downloaded web page.

Before we start please notice that the data (conditions, forecasts, news, images, logos) contained in weather.yahoo.com are copyrighted by Yahoo! Inc. and the Weather Channel Enterprises, Inc. You are prohibited from using or repurposing this data in any way without express written consent from Yahoo! Inc. and the Weather Channel Enterprises, Inc. If you are looking for a source of weather data, please see the National Weather Service Website at http://www.nws.noaa.gov/. Their data is public information, to be used with appropriate byline/photo/image credits. This tutorial shows only some techniques allowing you download a file and render some data.
Add a New Method and enter for Method Name: getData for Parameters: KeyQuery As String, KeyQuery2 As String, data As String and for Result: String. Insert then the code below. If you don’t understand what is going here please visit Mid and InStr Functions in REALbasic tutorial for better orientation.
1 2 3 4 | Dim KeyStart As String = KeyQuery Dim KeyEnd As String = KeyQuery2 Return Mid( Data, InStr( Data, KeyStart ) + Len(KeyStart), InStr( Data, KeyEnd ) - (InStr( Data, KeyStart) + Len(KeyStart) ) ) |
Create a New Method and name it getImage. In this method we will find the graphics on the web page, download it to our hard drive and load it to our application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Dim f As FolderItem Dim p As Picture Dim http as New HTTPSocket Dim pictureurl As String f = getTemporaryFolderItem() //temporary folder location to save image pictureurl = getData("http://l.yimg.com/a/i/us/nws/weather/gr/", ".png", s) //find the picture name from source code dim result as boolean = http.get("http://l.yimg.com/a/i/us/nws/weather/gr/" + pictureurl + ".png", f, 10) //download and save the picture if result then f.MacCreator = "prvw" f.MacType = "PNG" p = f.openAsPicture() //load the picture from hard drive to memory Canvas1.Backdrop = p //load the picture to Canvas1 Control end if http.Close |
Ok, when we view http://weather.yahoo.com/Wroclaw-Poland/PLXX0029/forecast.html page source we will find a HTML dl Tag with all data we like to use in our REALbasic Weather application.
1 2 3 4 5 6 7 8 9 10 | <dl> <dt>Feels Like:</dt><dd>57°</dd> <dt>Barometer:</dt><dd>30 in and steady</dd> <dt>Humidity:</dt><dd>72%</dd> <dt>Visibility:</dt><dd>6.21 mi</dd> <dt>Dewpoint:</dt><dd>48°</dd> <dt>Wind:</dt><dd>ESE 8 mph</dd> <dt>Sunrise:</dt><dd>6:32 am</dd> <dt>Sunset:</dt><dd>4:40 pm</dd> </dl> |
Now we try to find and format this data and display it in our StaticText Controls. Let’s do it!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | Dim http as New HTTPSocket Dim part As String s = http.Get("http://weather.yahoo.com/Wroclaw-Poland/PLXX0029/forecast.html",30) part = getData("<dl>","</dl>", s) //grab only the data we will need StaticText1.Text = "Feels Like: " + getData("Feels Like:</dt><dd>","</dd>", part) part = Replace(part, "</dd>", "") //this will give us a option to find next end of </dd> StaticText2.Text = "Barometer: " + getData( "Barometer:</dt><dd>" ,"</dd>" , part) part = Replace(part, "</dd>", "") StaticText3.Text = "Humidity: " + getData("Humidity:</dt><dd>","</dd>", part) part = Replace(part, "</dd>", "") StaticText4.Text = "Visibility: " + getData("Visibility:</dt><dd>","</dd>", part) part = Replace(part, "</dd>", "") StaticText5.Text = "Dewpoint: " + getData("Dewpoint:</dt><dd>","</dd>", part) part = Replace(part, "</dd>", "") StaticText6.Text = "Wind: " + getData("Wind:</dt><dd>","</dd>", part) part = Replace(part, "</dd>", "") StaticText7.Text = "Sunrise: " + getData("Sunrise:</dt><dd>","</dd>", part) part = Replace(part, "</dd>", "") StaticText8.Text = "Sunset: " + getData("Sunset:</dt><dd>","</dd>", part) getImage //download and load the image into Canvas1 Control |
If you see this message you probably want access special material for Premium Members (Source Files, Bonus Tutorials, free Classes and More). You can do this by subscribing REALbasic City site for 10 USD (access for 3 months). There is also a option to buy access only to this material. Login or register to get option to buy and support REALbasic City.
Log in
Greetings, Jakub
As a former (version 1.0) VB programmer, I’ve just started out with RealBasic.
Your weather grab interested me, as it’s along the lines of the application I wish to use, grabbing text from an HTTP source.
However, I cannot find what I’ve done wrong having created the project as per your tutorial.
The static texts show only the words, “Feels like:” etc. with no actual data.
The source clip is:
Feels Like:28 °F
Barometer:29.68 in and steady
Humidity:63 %
Visibility:6.21 mi
Dewpoint:19 °F
Wind:SW 10 mph
Sunrise:7:53 AM
Sunset:3:47 PM
Can you suggest what I’e done wrong?
Cheers
Hello John,
maybe Yahoo changed the formatting of the weather data on their Weather Channel. I will check this in free time. What you get in s String after downloading the data?
Regards
Jakub
I have to be stupid!
All I have in s (which I used a listbox and addrow to collect s) is
fe7.weather.ac4.yahoo.com uncompressed Mon Dec 21 12:51:51 PST 2009
which doesn’t make any sense to me.
Ok, If you like to get the weather data from Wroclaw (POLAND) you must download this content. Yahoo changed something and the links are different now.
http://weather.yahoo.com/poland/lower-silesia/wroclaw-526363/
That’d better, although it needs some work.
Ok, a general comment that I need to think about; if the web page changes it’s appearance etc. then the software needs updating. With a more complex parsing method, we could cater for changes.
My planned application will access the Spot Tracker page (see findmespot.com) in order to download the position of an aircraft.
Thanks for looking at it all.
Cheers