PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/MetroHome1.1/Extensions/Weather/MSN/Weather.cs

#
C# | 221 lines | 195 code | 24 blank | 2 comment | 28 complexity | 8e3af8de94e40ac0cc89583b629d492d MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Xml;
  6. using System.Xml.Linq;
  7. using HTCHome.Core;
  8. using WeatherClockWidget;
  9. using WeatherClockWidget.Domain;
  10. namespace MSN
  11. {
  12. public class Weather : IWeatherProvider
  13. {
  14. private const string RequestForCelsius = "http://weather.service.msn.com/data.aspx?culture={0}&wealocations={1}&weadegreetype=C";
  15. private const string RequestForFahrenheit = "http://weather.service.msn.com/data.aspx?culture={0}&wealocations={1}&weadegreetype=F";
  16. private const string RequestForLocation = "http://weather.msn.com/find.aspx?weasearchstr={0}";
  17. public Coordinates GetCoordinates(string locationCode)
  18. {
  19. var f = new NumberFormatInfo {CurrencyDecimalSeparator = "."};
  20. var reader = new XmlTextReader(string.Format(RequestForCelsius, "", locationCode));
  21. var coord = new Coordinates();
  22. while (reader.Read())
  23. {
  24. if (reader.NodeType != XmlNodeType.Element || !reader.Name.Equals("weather"))
  25. {
  26. continue;
  27. }
  28. reader.MoveToAttribute("lat");
  29. coord.X = Math.Round(Convert.ToDouble(reader.Value, f), 1);
  30. reader.MoveToAttribute("long");
  31. coord.Y = Math.Round(Convert.ToDouble(reader.Value, f), 1);
  32. }
  33. return coord;
  34. }
  35. public List<CityLocation> GetLocation(string s)
  36. {
  37. var l = new CityLocation();
  38. var result = new List<CityLocation>();
  39. var reader = new XmlTextReader(string.Format(RequestForLocation, s));
  40. while (reader.Read())
  41. {
  42. if (reader.NodeType == XmlNodeType.Element && reader.Name.Equals("weather"))
  43. {
  44. reader.MoveToAttribute("weatherlocationname");
  45. l.City = reader.Value;
  46. reader.MoveToAttribute("weatherlocationcode");
  47. l.Code = reader.Value;
  48. result.Add(l);
  49. }
  50. }
  51. reader.Close();
  52. return result;
  53. }
  54. WeatherReport IWeatherProvider.GetWeatherReport(string locale, string locationcode, int degreeType)
  55. {
  56. string url = string.Format(degreeType == 0 ? RequestForCelsius : RequestForFahrenheit, locale, locationcode);
  57. string s = GeneralHelper.GetXml(url);
  58. XDocument doc = XDocument.Parse(s);
  59. //parse current weather
  60. var weather = from x in doc.Descendants("weather")
  61. let xElement = x.Element("current")
  62. select
  63. new {
  64. temp = xElement.Attribute("temperature").Value,
  65. text = xElement.Attribute("skytext").Value,
  66. skycode = xElement.Attribute("skycode").Value
  67. };
  68. var result = new WeatherReport();
  69. var currentWeather = weather.FirstOrDefault();
  70. if (currentWeather != null)
  71. {
  72. result.NowTemp = Convert.ToInt32(currentWeather.temp);
  73. result.NowSky = currentWeather.text;
  74. result.NowSkyCode = GetWeatherPic(Convert.ToInt32(currentWeather.skycode), 4, 22);
  75. }
  76. result.Location = doc.Descendants("weather").FirstOrDefault().Attribute("weatherlocationname").Value;
  77. //parse forecast
  78. var days = from x in doc.Descendants("forecast")
  79. select
  80. new {
  81. l = x.Attribute("low").Value,
  82. h = x.Attribute("high").Value,
  83. skycode = x.Attribute("skycodeday").Value,
  84. text = x.Attribute("skytextday").Value
  85. };
  86. List<DayForecast> f = new List<DayForecast>();
  87. foreach (var d in days)
  88. {
  89. f.Add(new DayForecast());
  90. f[f.Count - 1].HighTemperature = Convert.ToInt32(d.h);
  91. f[f.Count - 1].LowTemperature = Convert.ToInt32(d.l);
  92. f[f.Count - 1].Text = d.text;
  93. f[f.Count - 1].SkyCode = GetWeatherPic(Convert.ToInt32(d.skycode), -1, 25);
  94. }
  95. result.High = f[0].HighTemperature;
  96. result.Low = f[0].LowTemperature;
  97. result.Forecast = f;
  98. return result;
  99. }
  100. public static int GetWeatherPic(int skycode, int sunrise, int sunset)
  101. {
  102. switch (skycode)
  103. {
  104. case 26:
  105. if (DateTime.Now.Hour >= sunset || DateTime.Now.Hour <= sunrise)
  106. return 34;
  107. else
  108. return 2;
  109. case 27:
  110. if (DateTime.Now.Hour >= sunset || DateTime.Now.Hour <= sunrise)
  111. return 35;
  112. else
  113. return 3;
  114. case 28:
  115. if (DateTime.Now.Hour >= sunset || DateTime.Now.Hour <= sunrise)
  116. return 38;
  117. else
  118. return 6;
  119. case 35:
  120. case 39:
  121. return 12;
  122. case 45:
  123. case 46:
  124. return 8;
  125. case 19:
  126. case 20:
  127. case 21:
  128. case 22:
  129. if (DateTime.Now.Hour >= sunset || DateTime.Now.Hour <= sunrise)
  130. return 37;
  131. else
  132. return 11;
  133. case 29:
  134. case 30:
  135. if (DateTime.Now.Hour >= sunset || DateTime.Now.Hour <= sunrise)
  136. return 35;
  137. else
  138. return 3;
  139. case 33:
  140. if (DateTime.Now.Hour >= sunset || DateTime.Now.Hour <= sunrise)
  141. return 38;
  142. else
  143. return 6;
  144. case 5:
  145. case 13:
  146. case 14:
  147. case 15:
  148. case 16:
  149. return 22;
  150. case 18:
  151. case 25:
  152. case 41:
  153. case 42:
  154. case 43:
  155. return 25;
  156. case 1:
  157. case 2:
  158. case 3:
  159. case 4:
  160. case 37:
  161. case 38:
  162. case 47:
  163. return 15;
  164. case 31:
  165. case 32:
  166. case 34:
  167. case 36:
  168. case 44:
  169. if (DateTime.Now.Hour >= sunset || DateTime.Now.Hour <= sunrise)
  170. {
  171. return 33;
  172. }
  173. else
  174. return 1;
  175. case 23:
  176. case 24:
  177. return 32;
  178. case 9:
  179. case 10:
  180. case 11:
  181. case 12:
  182. case 40:
  183. return 18;
  184. case 6:
  185. case 7:
  186. case 8:
  187. case 17:
  188. return 15;
  189. default:
  190. if (DateTime.Now.Hour >= sunset || DateTime.Now.Hour <= sunrise)
  191. return 33;
  192. else
  193. return 1;
  194. }
  195. }
  196. }
  197. }