Thursday, October 21, 2010

How do I load weather data into my VB application?

I am new to vb, trying to learn it on my own (I have a c++ backgound). I am attempting to build a simple application that logs date, time and weather. What is the best way to extract weather data for one city from the web and put it into my VB app?How do I load weather data into my VB application?
This will work for temp, just got through and make functions for (local time, humidity dewpoint, high-low, etc)



Function temperature(ByVal url As String) As String



temperature = Nothing



Dim page As String = GetPage(url)



Dim body As String = ExtractBody(page)



body = strip_HTML(body)



Dim temperature_finder() As String = body.Split(';F';)



For Each temp As String In temperature_finder



'REPLACE the SYMBOL with ';%26amp; # 176 ;'; with no spaces

If temp.EndsWith(';SYMBOL';) Then



temp = temp.Substring(temp.Length - 12, temp.Length - (temp.Length - 6))



temperature = temp.Trim



Exit For



End If



Next



End Function



Function GetPage(ByVal pageUrl As String) As String



Dim s As String = ';';



Dim request As Net.HttpWebRequest



Dim response As Net.HttpWebResponse



Dim reader As IO.StreamReader



Try

request = Net.WebRequest.Create(pageUrl)



response = request.GetResponse()



reader = New IO.StreamReader(response.GetResponseStre鈥?br>


s = reader.ReadToEnd()



Catch ex As Exception



MsgBox(';FAIL: '; + ex.Message)



End Try



Return s



End Function



Function ExtractBody(ByVal page As String) As String



Return System.Text.RegularExpressions.Regex.Rep鈥?';.*%26lt;body[^%26gt;]*%26gt;(.*)%26lt;/body%26gt;.*';, ';$1';, _

System.Text.RegularExpressions.RegexOpti鈥?br>


End Function





Function strip_HTML(ByVal HTML As String)



If Len(HTML) = 0 Then



strip_HTML = HTML



Exit Function



End If



Dim array_split, I, C, string_Output



array_split = Split(HTML, ';%26lt;';)



If Len(array_split(0)) %26gt; 0 Then C = 1 Else C = 0



For I = C To UBound(array_split)



If InStr(array_split(I), ';%26gt;';) Then



array_split(I) = Mid(array_split(I), InStr(array_split(I), ';%26gt;';) + 1)



Else



array_split(I) = ';%26lt;'; %26amp; array_split(I)



End If



Next



string_Output = Join(array_split, ';';)



string_Output = Mid(string_Output, 2 - C)



string_Output = Replace(string_Output, ';%26gt;';, ';%26gt;';)



string_Output = Replace(string_Output, ';%26lt;';, ';%26lt;';)



string_Output = Replace(string_Output, ';聽';, ';';)



strip_HTML = string_Output



End Function



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click



If TextBox1.Text.Length = 5 Then



Dim zip_code As String = TextBox1.Text



MsgBox(temperature(';Http://www.wundergro鈥?+ zip_code + ';%26amp;searchType=WEATHER';))



Else



MsgBox(';FAIL: Need 5 digit zip code!';)



End If

End Sub



Good Luck.How do I load weather data into my VB application?
Well... ';best'; way?

First: Find some place that contains the information you need.

Second: Let the program load the information.

Third: Evaluate the data. Do whatever you want with it (convert celsius to fahrenheit, i don't know)

Fourth: Display the results.



PS: Basic is a horrible language. I don't know why anyone would want to learn it.
wunderground contains historical weather data that you can download as a csv file. You could download one copy of the file and get used to its format. Then you need to make a HTTP request like a browser does when you want to grab data for any city for any time. If you're using VB.NET, then you'll find the System.Net.WebClient class helpful. If you're using VB6, then you might want to look at this: http://www.vbforums.com/showthread.php?t鈥?/a> although you can achieve things faster in VB.NET

No comments:

Post a Comment