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 8.8KB

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