Advertisement
Pctech37

GetTemp 0.3

Nov 23rd, 2011
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using SpeechLib;
  2. using System;
  3. using System.IO;
  4. using System.Net;
  5. using System.Xml;
  6.  
  7. // GetTemp 0.3
  8. // Copyright © 2011 by Andrew Mock. All rights reserved.
  9. // Special thanks to: marc_s at stackoverflow.com
  10.  
  11. // Description: Returns the current temperature (F). You'll need a
  12. // WeatherBug API Key and a WeatherBug Station ID. I have really
  13. // expanded this code beyond the basics so you could learn more.
  14.  
  15. // Syntax: GetTemp.exe APIKey StationID
  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. // 0.3  added current condition and station to result; uses SpeechLib
  28.  
  29. namespace GetTemp
  30. {
  31.     class Program
  32.     {
  33.         static void Main(string[] args)
  34.         {
  35.             // get fancy with special title
  36.             Console.Title = "GetTemp 0.3 by Andrew Mock";
  37.  
  38.             // make sure that we have only two args
  39.             if (args.Length != 2)
  40.             {
  41.                 // we don't have two args; explain usage
  42.                 Console.WriteLine("Usage: GetTemp.exe APIKey StationID");
  43.  
  44.                 // pause
  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.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement