Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using SpeechLib;
- using System;
- using System.IO;
- using System.Net;
- using System.Xml;
- // GetTemp 0.3
- // Copyright © 2011 by Andrew Mock. All rights reserved.
- // Special thanks to: marc_s at stackoverflow.com
- // Description: Returns the current temperature (F). You'll need a
- // WeatherBug API Key and a WeatherBug Station ID. I have really
- // expanded this code beyond the basics so you could learn more.
- // Syntax: GetTemp.exe APIKey StationID
- // calling it like this:
- // GetTenp.exe A0000000000 STATN
- //
- // returns like this (using Text-to-Speech):
- // The current weather is: 99.9 degrees Fahrenheit, and Sunny at: Some Station.
- // Changelog:
- // 0.1 Initial Construct
- // 0.2 fixed errors where args not passing
- // 0.3 added current condition and station to result; uses SpeechLib
- namespace GetTemp
- {
- class Program
- {
- static void Main(string[] args)
- {
- // get fancy with special title
- Console.Title = "GetTemp 0.3 by Andrew Mock";
- // make sure that we have only two args
- if (args.Length != 2)
- {
- // we don't have two args; explain usage
- Console.WriteLine("Usage: GetTemp.exe APIKey StationID");
- // pause
- Console.Write("Press any key to continue...");
- Console.ReadKey(true);
- // close app
- Environment.Exit(0);
- }
- // new 'XML Document' object
- XmlDocument doc = new XmlDocument();
- try
- {
- // put the live weather in the "blank" XML Document per APIKey, stationID
- doc.Load(string.Format("http://{0}.api.wxbug.net/getLiveCompactWeatherRSS.aspx?ACode={0}&stationid={1}&UnitType=0&OutputType=1", args));
- }
- catch (Exception ex)
- {
- // "Access Denied" or other error
- Console.WriteLine("Error. Did you use an API Key valid for more than 30 minutes?");
- Console.WriteLine("Error:" + Environment.NewLine + ex);
- // pause
- Console.Write("Press any key to continue...");
- Console.ReadKey(true);
- // close app
- Environment.Exit(0);
- }
- // new 'XML Namespace Manager' object per our doc's NameTable
- XmlNamespaceManager xmlnsm = new XmlNamespaceManager(doc.NameTable);
- // add the "aws" XML Namespace (prefix) to the 'XML Namespace Manager'
- xmlnsm.AddNamespace("aws", "http://www.aws.com/aws");
- // <aws:temp> contains the temp
- string temp = doc.SelectSingleNode("/aws:weather/aws:temp", xmlnsm).InnerText;
- // <aws:current-condition> conatins the current condition
- string current = doc.SelectSingleNode("/aws:weather/aws:current-condition", xmlnsm).InnerText;
- // <aws:station> contains the current station's name
- string station = doc.SelectSingleNode("//aws:station/@name", xmlnsm).InnerText;
- // write the temp in console
- Console.WriteLine("The current weather is: " + temp + " degrees Fahrenheit, and " + current + " at: " + station + ".");
- // use SpeechLib to announce weather
- try
- {
- SpVoice voice = new SpVoice();
- voice.Speak("The current weather is: " + temp + " degrees Fahrenheit, and " + current + " at: " + station + ".", SpeechVoiceSpeakFlags.SVSFDefault);
- }
- catch (Exception ex)
- {
- // Anyone so up-to-date to have Framework 4 should have SpeechLib, but always expect someone to have this issue
- Console.WriteLine("Error:" + Environment.NewLine + ex);
- }
- // pause
- Console.Write("Press any key to continue...");
- Console.ReadKey(true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement