PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Weather.cs

https://github.com/mishagl/weather
C# | 233 lines | 197 code | 19 blank | 17 comment | 3 complexity | 97eb74d044670f218feb69fe947e0839 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Linq;
  6. using System.Xml;
  7. using System.Xml.XPath;
  8. namespace weather
  9. {
  10. public class Weather
  11. {
  12. public class Forecast : IComparer<Forecast>
  13. {
  14. public string Day { get; private set; }
  15. public DateTime Date { get; private set; }
  16. public int Low { get; private set; }
  17. public int High { get; private set; }
  18. public string Text { get; private set; }
  19. public Conditions Condition { get; private set; }
  20. public string Scale { get; private set; }
  21. public string GetHighTemp()
  22. {
  23. return renderTemp(this.High);
  24. }
  25. public string GetLowTemp()
  26. {
  27. return renderTemp(this.Low);
  28. }
  29. private string renderTemp(int temp)
  30. {
  31. return string.Format("{0}°{1}", temp, this.Scale);
  32. }
  33. public static Forecast Parse(XElement forecast, string scale)
  34. {
  35. return new Forecast
  36. {
  37. Day = forecast.Attribute("day").Value,
  38. Date = DateTime.Parse(forecast.Attribute("date").Value),
  39. Condition = (Conditions)int.Parse(forecast.Attribute("code").Value),
  40. High = int.Parse(forecast.Attribute("high").Value),
  41. Low = int.Parse(forecast.Attribute("low").Value),
  42. Text = forecast.Attribute("text").Value,
  43. Scale = scale
  44. };
  45. }
  46. public int Compare(Forecast x, Forecast y)
  47. {
  48. return x.Date.CompareTo(y.Date);
  49. }
  50. }
  51. #region Enums
  52. public enum PressureRising { Steady, Rising, Falling };
  53. public enum Conditions
  54. {
  55. Tornado,
  56. [EnumDescription("Tropical storm")]
  57. TropicalStorm,
  58. Hurricane,
  59. [EnumDescription("Severe thunderstorms")]
  60. SevereThunderstorms,
  61. Thunderstorms,
  62. [EnumDescription("Mixed rain and snow")]
  63. MixedRainAndSnow,
  64. [EnumDescription("Mixed rain and sleet")]
  65. MixedRainAndSleet,
  66. [EnumDescription("Mixed snow and sleet")]
  67. MixedSnowAndSleet,
  68. [EnumDescription("Freezing drizzle")]
  69. FreezingDrizzle,
  70. Drizzle,
  71. [EnumDescription("Freezing rain")]
  72. FreezingRain,
  73. [EnumDescription("Showers")]
  74. Showers1,
  75. [EnumDescription("Showers")]
  76. Showers2,
  77. [EnumDescription("Snow flurries")]
  78. SnowFlurries,
  79. [EnumDescription("Light snow showers")]
  80. LightSnowShowers,
  81. [EnumDescription("Blowing snow")]
  82. BlowingSnow,
  83. Snow,
  84. Hail,
  85. Sleet,
  86. Dust,
  87. Foggy,
  88. Haze,
  89. Smoky,
  90. Blustery,
  91. Windy,
  92. Cold,
  93. Cloudy,
  94. [EnumDescription("Mostly cloudy night")]
  95. MostlyCloudyNight,
  96. [EnumDescription("Mostly cloudy day")]
  97. MostlyCloudyDay,
  98. [EnumDescription("Partly cloudy night")]
  99. PartlyCloudyNight,
  100. [EnumDescription("Partly cloudy day")]
  101. PartlyCloudyDay,
  102. [EnumDescription("Clear night")]
  103. ClearNight,
  104. Sunny,
  105. [EnumDescription("Fair night")]
  106. FairNight,
  107. [EnumDescription("Fair day")]
  108. FairDay,
  109. [EnumDescription("Mixed rain and hail")]
  110. MixedRainAndHail,
  111. Hot,
  112. [EnumDescription("Isolated thunderstorms")]
  113. IsolatedThunderstorms,
  114. [EnumDescription("Scattered thunderstorms")]
  115. ScatteredThunderstorms1,
  116. [EnumDescription("Scattered thunderstorms")]
  117. ScatteredThunderstorms2,
  118. [EnumDescription("Scattered showers")]
  119. ScatteredShowers,
  120. [EnumDescription("Heavy snow")]
  121. HeavySnow1,
  122. [EnumDescription("Scattered snow showers")]
  123. ScatteredSnowShowers,
  124. [EnumDescription("Heavy snow")]
  125. HeavySnow2,
  126. [EnumDescription("Partly cloudy")]
  127. PartlyCloudy,
  128. Thundershowers,
  129. [EnumDescription("Snow showers")]
  130. SnowShowers,
  131. [EnumDescription("Isolated thundershowers")]
  132. IsolatedThundershowers,
  133. [EnumDescription("Not Available")]
  134. NotAvailable = 3200
  135. }
  136. #endregion
  137. #region Properties
  138. public string ImgSource { get; private set; }
  139. public string Title { get; private set; }
  140. public string Humidity { get; private set; }
  141. public string Pressure { get; private set; }
  142. public PressureRising Rising { get; private set; }
  143. public string Temperature { get; private set; }
  144. public string Scale { get; private set; }
  145. public string UnitsPressure { get; private set; }
  146. public Conditions Condition { get; private set; }
  147. public List<Forecast> Forecasts { get; private set; }
  148. #endregion
  149. #region Public methods
  150. /// <summary>
  151. /// Get Temperature string
  152. /// </summary>
  153. /// <returns>[temperature]°[scale]</returns>
  154. public string GetTemperature()
  155. {
  156. return Temperature + "°" + Scale;
  157. }
  158. /// <summary>
  159. /// Get Humidity string
  160. /// </summary>
  161. /// <returns>Humidiy: [value]%</returns>
  162. public string GetHumidity()
  163. {
  164. return "Humidiy: " + Humidity + "%";
  165. }
  166. /// <summary>
  167. /// Get Barometric pressure string
  168. /// </summary>
  169. /// <returns>Pressure: [pressure][units] [rising?]</returns>
  170. public string GetPressure()
  171. {
  172. return "Pressure: " + Pressure + UnitsPressure + " " + Rising.ToString();
  173. }
  174. #endregion
  175. #region YAHOO Weather Parser
  176. /// <summary>
  177. /// Parses YAHOO Weather API XML and returns an instance of Weather object
  178. /// </summary>
  179. /// <param name="weather"></param>
  180. /// <returns></returns>
  181. public static Weather Parse(XDocument weather)
  182. {
  183. if (weather == null) return null;
  184. XmlNamespaceManager ns = new XmlNamespaceManager(weather.FirstNode.CreateNavigator().NameTable);
  185. ns.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
  186. string html = weather.XPathSelectElement("/rss/channel/item/description").Value;
  187. var units = weather.XPathSelectElement("/rss/channel/yweather:units", ns);
  188. var atmosphere = weather.XPathSelectElement("/rss/channel/yweather:atmosphere", ns);
  189. var condition = weather.XPathSelectElement("/rss/channel/item/yweather:condition", ns);
  190. var forecast = weather.XPathSelectElements("/rss/channel/item/yweather:forecast", ns);
  191. if (weather.XPathSelectElement("/rss/channel/item/title").Value.Contains("not found")) return null;
  192. return new Weather
  193. {
  194. ImgSource = XElement.Parse("<body>" + html + "</body>").XPathSelectElement("//img").Attribute("src").Value,
  195. Title = weather.XPathSelectElement("/rss/channel/item/title").Value,
  196. Temperature = condition.Attribute("temp").Value,
  197. Scale = units.Attribute("temperature").Value,
  198. Humidity = atmosphere.Attribute("humidity").Value,
  199. Pressure = atmosphere.Attribute("pressure").Value,
  200. Rising = (PressureRising)int.Parse(atmosphere.Attribute("rising").Value),
  201. UnitsPressure = units.Attribute("pressure").Value,
  202. Condition = (Conditions)int.Parse(condition.Attribute("code").Value),
  203. Forecasts = new List<Forecast>(forecast.Select(f => Forecast.Parse(f, units.Attribute("temperature").Value)))
  204. };
  205. }
  206. #endregion
  207. public override string ToString()
  208. {
  209. return this.Title + "\nTemperature: " + this.GetTemperature()
  210. + "\n" + this.GetHumidity()
  211. + "\n" + this.GetPressure();
  212. }
  213. }
  214. }