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.cs 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Xml;
  3. namespace Combot.Modules.ModuleClasses
  4. {
  5. public class Weather : 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.Count > 0)
  41. {
  42. foreach (XmlNode node2 in nodes2)
  43. {
  44. XmlNodeList sub_node2 = doc2.SelectNodes("/current_observation/display_location");
  45. foreach (XmlNode xn2 in sub_node2)
  46. {
  47. location = xn2["full"].InnerText;
  48. }
  49. }
  50. }
  51. XmlDocument doc = new XmlDocument();
  52. // Load data
  53. doc.Load("http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=" + command.Arguments["Location"]);
  54. // Get forecast with XPath
  55. XmlNodeList nodes = doc.SelectNodes("/forecast/simpleforecast");
  56. string weekday = string.Empty;
  57. string highf = string.Empty;
  58. string lowf = string.Empty;
  59. string highc = string.Empty;
  60. string lowc = string.Empty;
  61. string conditions = string.Empty;
  62. if (location != ", " && !String.IsNullOrEmpty(location))
  63. {
  64. if (nodes != null && nodes.Count > 0)
  65. {
  66. string startMsg = string.Format("{0} day forecast for {1}", days, command.Arguments["Location"]);
  67. if (command.MessageType == MessageType.Channel || command.MessageType == MessageType.Notice)
  68. {
  69. Bot.IRC.SendNotice(command.Nick.Nickname, startMsg);
  70. }
  71. else
  72. {
  73. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, startMsg);
  74. }
  75. int index = 0;
  76. foreach (XmlNode node in nodes)
  77. {
  78. foreach (XmlNode sub_node in node)
  79. {
  80. if (index < days)
  81. {
  82. weekday = sub_node["date"].SelectSingleNode("weekday").InnerText;
  83. highf = sub_node["high"].SelectSingleNode("fahrenheit").InnerText;
  84. highc = sub_node["high"].SelectSingleNode("celsius").InnerText;
  85. lowf = sub_node["low"].SelectSingleNode("fahrenheit").InnerText;
  86. lowc = sub_node["low"].SelectSingleNode("celsius").InnerText;
  87. conditions = sub_node["conditions"].InnerText;
  88. 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);
  89. if (command.MessageType == MessageType.Channel || command.MessageType == MessageType.Notice)
  90. {
  91. Bot.IRC.SendNotice(command.Nick.Nickname, forecastMsg);
  92. }
  93. else
  94. {
  95. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, forecastMsg);
  96. }
  97. }
  98. index++;
  99. }
  100. }
  101. }
  102. else
  103. {
  104. string noWeather = string.Format("No weather information available for \u0002{0}\u000F", command.Arguments["Location"]);
  105. if (command.MessageType == MessageType.Channel || command.MessageType == MessageType.Notice)
  106. {
  107. Bot.IRC.SendNotice(command.Nick.Nickname, noWeather);
  108. }
  109. else
  110. {
  111. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, noWeather);
  112. }
  113. }
  114. }
  115. else
  116. {
  117. string noWeather = string.Format("No weather information available for \u0002{0}\u000F", command.Arguments["Location"]);
  118. if (command.MessageType == MessageType.Channel || command.MessageType == MessageType.Notice)
  119. {
  120. Bot.IRC.SendNotice(command.Nick.Nickname, noWeather);
  121. }
  122. else
  123. {
  124. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, noWeather);
  125. }
  126. }
  127. }
  128. private void getWeather(CommandMessage command)
  129. {
  130. XmlDocument doc = new XmlDocument();
  131. // Load data
  132. doc.Load("http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=" + command.Arguments["Location"]);
  133. // Get forecast with XPath
  134. XmlNodeList nodes = doc.SelectNodes("/current_observation");
  135. string weatherMsg = string.Empty;
  136. string location = string.Empty;
  137. string temp = string.Empty;
  138. string weather = string.Empty;
  139. string humidity = string.Empty;
  140. string wind_dir = string.Empty;
  141. string wind_mph = string.Empty;
  142. if (nodes != null && nodes.Count > 0)
  143. {
  144. foreach (XmlNode node in nodes)
  145. {
  146. XmlNodeList sub_node = doc.SelectNodes("/current_observation/display_location");
  147. foreach (XmlNode xn in sub_node)
  148. {
  149. location = xn["full"].InnerText;
  150. }
  151. temp = node["temperature_string"].InnerText;
  152. weather = node["weather"].InnerText;
  153. humidity = node["relative_humidity"].InnerText;
  154. wind_dir = node["wind_dir"].InnerText;
  155. wind_mph = node["wind_mph"].InnerText;
  156. }
  157. if (location != ", ")
  158. {
  159. 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);
  160. }
  161. else
  162. {
  163. weatherMsg = string.Format("No weather information available for \u0002{0}\u000F", command.Arguments["Location"]);
  164. }
  165. }
  166. else
  167. {
  168. weatherMsg = string.Format("No weather information available for \u0002{0}\u000F", command.Arguments["Location"]);
  169. }
  170. if (command.MessageType == MessageType.Channel)
  171. {
  172. Bot.IRC.SendPrivateMessage(command.Location, weatherMsg);
  173. }
  174. else if (command.MessageType == MessageType.Notice)
  175. {
  176. Bot.IRC.SendNotice(command.Nick.Nickname, weatherMsg);
  177. }
  178. else if (command.MessageType == MessageType.Query)
  179. {
  180. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, weatherMsg);
  181. }
  182. }
  183. }
  184. }