The easy to use and full featured Irc Bot everyone is talking about!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Weather_Information.cs 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Xml;
  3. namespace Combot.Modules.Plugins
  4. {
  5. public class Weather_Information : Module
  6. {
  7. public override void Initialize()
  8. {
  9. Bot.CommandReceivedEvent += HandleCommandEvent;
  10. }
  11. public override void ParseCommand(CommandMessage command)
  12. {
  13. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  14. switch (foundCommand.Name)
  15. {
  16. case "Weather":
  17. getWeather(command);
  18. break;
  19. case "Forecast":
  20. getForecast(command);
  21. break;
  22. }
  23. }
  24. private void getForecast(CommandMessage command)
  25. {
  26. int days = 6;
  27. if (command.Arguments.ContainsKey("Days"))
  28. {
  29. if (!int.TryParse(command.Arguments["Days"], out days))
  30. {
  31. days = 6;
  32. }
  33. }
  34. XmlDocument doc2 = new XmlDocument();
  35. // Load data
  36. doc2.Load("http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=" + command.Arguments["Location"]);
  37. // Get forecast with XPath
  38. XmlNodeList nodes2 = doc2.SelectNodes("/current_observation");
  39. string location = "";
  40. if (nodes2 != null && nodes2.Count > 0)
  41. {
  42. foreach (XmlNode node2 in nodes2)
  43. {
  44. XmlNodeList sub_node2 = doc2.SelectNodes("/current_observation/display_location");
  45. if (sub_node2 != null)
  46. {
  47. foreach (XmlNode xn2 in sub_node2)
  48. {
  49. location = xn2["full"].InnerText;
  50. }
  51. }
  52. }
  53. }
  54. XmlDocument doc = new XmlDocument();
  55. // Load data
  56. doc.Load("http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=" + command.Arguments["Location"]);
  57. // Get forecast with XPath
  58. XmlNodeList nodes = doc.SelectNodes("/forecast/simpleforecast");
  59. string weekday = string.Empty;
  60. string highf = string.Empty;
  61. string lowf = string.Empty;
  62. string highc = string.Empty;
  63. string lowc = string.Empty;
  64. string conditions = string.Empty;
  65. if (location != ", " && !String.IsNullOrEmpty(location))
  66. {
  67. if (nodes != null && nodes.Count > 0)
  68. {
  69. string startMsg = string.Format("{0} day forecast for {1}", days, command.Arguments["Location"]);
  70. if (command.MessageType == MessageType.Channel || command.MessageType == MessageType.Notice)
  71. {
  72. Bot.IRC.Command.SendNotice(command.Nick.Nickname, startMsg);
  73. }
  74. else
  75. {
  76. Bot.IRC.Command.SendPrivateMessage(command.Nick.Nickname, startMsg);
  77. }
  78. int index = 0;
  79. foreach (XmlNode node in nodes)
  80. {
  81. foreach (XmlNode sub_node in node)
  82. {
  83. if (index < days)
  84. {
  85. weekday = sub_node["date"].SelectSingleNode("weekday").InnerText;
  86. highf = sub_node["high"].SelectSingleNode("fahrenheit").InnerText;
  87. highc = sub_node["high"].SelectSingleNode("celsius").InnerText;
  88. lowf = sub_node["low"].SelectSingleNode("fahrenheit").InnerText;
  89. lowc = sub_node["low"].SelectSingleNode("celsius").InnerText;
  90. conditions = sub_node["conditions"].InnerText;
  91. string forecastMsg = string.Format("{0}: {1} with a high of {2} F ({3} C) and a low of {4} F ({5} C).", weekday, conditions, highf, highc, lowf, lowc);
  92. if (command.MessageType == MessageType.Channel || command.MessageType == MessageType.Notice)
  93. {
  94. Bot.IRC.Command.SendNotice(command.Nick.Nickname, forecastMsg);
  95. }
  96. else
  97. {
  98. Bot.IRC.Command.SendPrivateMessage(command.Nick.Nickname, forecastMsg);
  99. }
  100. }
  101. index++;
  102. }
  103. }
  104. }
  105. else
  106. {
  107. string noWeather = string.Format("No weather information available for \u0002{0}\u000F", command.Arguments["Location"]);
  108. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, noWeather);
  109. }
  110. }
  111. else
  112. {
  113. string noWeather = string.Format("No weather information available for \u0002{0}\u000F", command.Arguments["Location"]);
  114. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, noWeather);
  115. }
  116. }
  117. private void getWeather(CommandMessage command)
  118. {
  119. XmlDocument doc = new XmlDocument();
  120. // Load data
  121. doc.Load("http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=" + command.Arguments["Location"]);
  122. // Get forecast with XPath
  123. XmlNodeList nodes = doc.SelectNodes("/current_observation");
  124. string weatherMsg = string.Empty;
  125. string location = string.Empty;
  126. string temp = string.Empty;
  127. string weather = string.Empty;
  128. string humidity = string.Empty;
  129. string wind_dir = string.Empty;
  130. string wind_mph = string.Empty;
  131. if (nodes != null && nodes.Count > 0)
  132. {
  133. foreach (XmlNode node in nodes)
  134. {
  135. XmlNodeList sub_node = doc.SelectNodes("/current_observation/display_location");
  136. foreach (XmlNode xn in sub_node)
  137. {
  138. location = xn["full"].InnerText;
  139. }
  140. temp = node["temperature_string"].InnerText;
  141. weather = node["weather"].InnerText;
  142. humidity = node["relative_humidity"].InnerText;
  143. wind_dir = node["wind_dir"].InnerText;
  144. wind_mph = node["wind_mph"].InnerText;
  145. }
  146. if (location != ", ")
  147. {
  148. weatherMsg = string.Format("{0} is currently {1} with a temperature of {2}. The humidity is {3} with winds blowing {4} at {5} mph", location, weather, temp, humidity, wind_dir, wind_mph);
  149. }
  150. else
  151. {
  152. weatherMsg = string.Format("No weather information available for \u0002{0}\u000F", command.Arguments["Location"]);
  153. }
  154. }
  155. else
  156. {
  157. weatherMsg = string.Format("No weather information available for \u0002{0}\u000F", command.Arguments["Location"]);
  158. }
  159. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, weatherMsg);
  160. }
  161. }
  162. }