SHOW:
|
|
- or go back to the newest paste.
1 | using SpeechLib; | |
2 | using System; | |
3 | using System.IO; | |
4 | using System.Net; | |
5 | using System.Xml; | |
6 | - | // GetTemp 0.1 |
6 | + | |
7 | - | // Copyright (c) 2011 by Andrew Mock. All rights reserved. |
7 | + | // GetTemp 0.3 |
8 | - | // Special thanks to: marc_s on Stack Overflow |
8 | + | // Copyright © 2011 by Andrew Mock. All rights reserved. |
9 | - | // |
9 | + | // Special thanks to: marc_s at stackoverflow.com |
10 | - | // Description: Returns the current temp (F). You'll need a WeatherBug API key, |
10 | + | |
11 | - | // and a WeatherBug Station ID. |
11 | + | // Description: Returns the current temperature (F). You'll need a |
12 | - | // |
12 | + | // WeatherBug API Key and a WeatherBug Station ID. I have really |
13 | - | // Syntax: GetTemp.exe APIKey stationID |
13 | + | // expanded this code beyond the basics so you could learn more. |
14 | - | // |
14 | + | |
15 | - | // calling it like this: GetTenp.exe A0000000000 STATN |
15 | + | // Syntax: GetTemp.exe APIKey StationID |
16 | - | // returns like this: 99.9 |
16 | + | |
17 | // calling it like this: | |
18 | // GetTenp.exe A0000000000 STATN | |
19 | // | |
20 | // returns like this (using Text-to-Speech): | |
21 | // The current weather is: 99.9 degrees Fahrenheit, and Sunny at: Some Station. | |
22 | ||
23 | ||
24 | // Changelog: | |
25 | // 0.1 Initial Construct | |
26 | // 0.2 fixed errors where args not passing | |
27 | - | // put the live weather "page" in the "blank" XML Document |
27 | + | // 0.3 added current condition and station to result; uses SpeechLib |
28 | - | doc.Load("http://www.andrewmock.com/uploads/9/1/0/7/9107466/example.xml"); |
28 | + | |
29 | namespace GetTemp | |
30 | { | |
31 | class Program | |
32 | { | |
33 | static void Main(string[] args) | |
34 | { | |
35 | // get fancy with special title | |
36 | - | // create the 'XML Node' object called weather for <aws:weather> |
36 | + | Console.Title = "GetTemp 0.3 by Andrew Mock"; |
37 | - | XmlNode weather = doc.SelectSingleNode("aws:weather", xmlnsm); |
37 | + | |
38 | // make sure that we have only two args | |
39 | - | // create the 'XML Node' object called weather for <aws:temp> |
39 | + | if (args.Length != 2) |
40 | - | XmlNode temp = weather.SelectSingleNode("aws:temp", xmlnsm); |
40 | + | { |
41 | // we don't have two args; explain usage | |
42 | Console.WriteLine("Usage: GetTemp.exe APIKey StationID"); | |
43 | - | Console.WriteLine(temp.InnerText); |
43 | + | |
44 | // pause | |
45 | - | //Console.ReadKey(false); |
45 | + | Console.Write("Press any key to continue..."); |
46 | Console.ReadKey(true); | |
47 | ||
48 | // close app | |
49 | Environment.Exit(0); | |
50 | } | |
51 | ||
52 | // new 'XML Document' object | |
53 | XmlDocument doc = new XmlDocument(); | |
54 | ||
55 | try | |
56 | { | |
57 | // put the live weather in the "blank" XML Document per APIKey, stationID | |
58 | doc.Load(string.Format("http://{0}.api.wxbug.net/getLiveCompactWeatherRSS.aspx?ACode={0}&stationid={1}&UnitType=0&OutputType=1", args)); | |
59 | } | |
60 | catch (Exception ex) | |
61 | { | |
62 | // "Access Denied" or other error | |
63 | Console.WriteLine("Error. Did you use an API Key valid for more than 30 minutes?"); | |
64 | Console.WriteLine("Error:" + Environment.NewLine + ex); | |
65 | ||
66 | // pause | |
67 | Console.Write("Press any key to continue..."); | |
68 | Console.ReadKey(true); | |
69 | ||
70 | // close app | |
71 | Environment.Exit(0); | |
72 | } | |
73 | ||
74 | // new 'XML Namespace Manager' object per our doc's NameTable | |
75 | XmlNamespaceManager xmlnsm = new XmlNamespaceManager(doc.NameTable); | |
76 | ||
77 | // add the "aws" XML Namespace (prefix) to the 'XML Namespace Manager' | |
78 | xmlnsm.AddNamespace("aws", "http://www.aws.com/aws"); | |
79 | ||
80 | // <aws:temp> contains the temp | |
81 | string temp = doc.SelectSingleNode("/aws:weather/aws:temp", xmlnsm).InnerText; | |
82 | ||
83 | // <aws:current-condition> conatins the current condition | |
84 | string current = doc.SelectSingleNode("/aws:weather/aws:current-condition", xmlnsm).InnerText; | |
85 | ||
86 | // <aws:station> contains the current station's name | |
87 | string station = doc.SelectSingleNode("//aws:station/@name", xmlnsm).InnerText; | |
88 | ||
89 | // write the temp in console | |
90 | Console.WriteLine("The current weather is: " + temp + " degrees Fahrenheit, and " + current + " at: " + station + "."); | |
91 | ||
92 | // use SpeechLib to announce weather | |
93 | try | |
94 | { | |
95 | SpVoice voice = new SpVoice(); | |
96 | voice.Speak("The current weather is: " + temp + " degrees Fahrenheit, and " + current + " at: " + station + ".", SpeechVoiceSpeakFlags.SVSFDefault); | |
97 | } | |
98 | catch (Exception ex) | |
99 | { | |
100 | // Anyone so up-to-date to have Framework 4 should have SpeechLib, but always expect someone to have this issue | |
101 | Console.WriteLine("Error:" + Environment.NewLine + ex); | |
102 | } | |
103 | ||
104 | // pause | |
105 | Console.Write("Press any key to continue..."); | |
106 | Console.ReadKey(true); | |
107 | } | |
108 | } | |
109 | } | |
110 | ||
111 |