WeatherProvider.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using MediaBrowser.Common.Logging;
  2. using MediaBrowser.Common.Serialization;
  3. using MediaBrowser.Model.Weather;
  4. using System;
  5. using System.ComponentModel.Composition;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. namespace MediaBrowser.Controller.Weather
  10. {
  11. /// <summary>
  12. /// Based on http://www.worldweatheronline.com/free-weather-feed.aspx
  13. /// The classes in this file are a reproduction of the json output, which will then be converted to our weather model classes
  14. /// </summary>
  15. [Export(typeof(BaseWeatherProvider))]
  16. public class WeatherProvider : BaseWeatherProvider
  17. {
  18. public override async Task<WeatherInfo> GetWeatherInfoAsync(string zipCode)
  19. {
  20. if (string.IsNullOrWhiteSpace(zipCode))
  21. {
  22. return null;
  23. }
  24. const int numDays = 5;
  25. const string apiKey = "24902f60f1231941120109";
  26. string url = "http://free.worldweatheronline.com/feed/weather.ashx?q=" + zipCode + "&format=json&num_of_days=" + numDays + "&key=" + apiKey;
  27. Logger.LogInfo("Accessing weather from " + url);
  28. using (Stream stream = await HttpClient.GetStreamAsync(url).ConfigureAwait(false))
  29. {
  30. WeatherData data = JsonSerializer.DeserializeFromStream<WeatherResult>(stream).data;
  31. return GetWeatherInfo(data);
  32. }
  33. }
  34. /// <summary>
  35. /// Converst the json output to our WeatherInfo model class
  36. /// </summary>
  37. private WeatherInfo GetWeatherInfo(WeatherData data)
  38. {
  39. var info = new WeatherInfo();
  40. if (data.current_condition != null)
  41. {
  42. if (data.current_condition.Any())
  43. {
  44. info.CurrentWeather = data.current_condition.First().ToWeatherStatus();
  45. }
  46. }
  47. if (data.weather != null)
  48. {
  49. info.Forecasts = data.weather.Select(w => w.ToWeatherForecast()).ToArray();
  50. }
  51. return info;
  52. }
  53. }
  54. class WeatherResult
  55. {
  56. public WeatherData data { get; set; }
  57. }
  58. public class WeatherData
  59. {
  60. public WeatherCondition[] current_condition { get; set; }
  61. public DailyWeatherInfo[] weather { get; set; }
  62. }
  63. public class WeatherCondition
  64. {
  65. public string temp_C { get; set; }
  66. public string temp_F { get; set; }
  67. public string humidity { get; set; }
  68. public string weatherCode { get; set; }
  69. public WeatherStatus ToWeatherStatus()
  70. {
  71. return new WeatherStatus
  72. {
  73. TemperatureCelsius = int.Parse(temp_C),
  74. TemperatureFahrenheit = int.Parse(temp_F),
  75. Humidity = int.Parse(humidity),
  76. Condition = DailyWeatherInfo.GetCondition(weatherCode)
  77. };
  78. }
  79. }
  80. public class DailyWeatherInfo
  81. {
  82. public string date { get; set; }
  83. public string precipMM { get; set; }
  84. public string tempMaxC { get; set; }
  85. public string tempMaxF { get; set; }
  86. public string tempMinC { get; set; }
  87. public string tempMinF { get; set; }
  88. public string weatherCode { get; set; }
  89. public string winddir16Point { get; set; }
  90. public string winddirDegree { get; set; }
  91. public string winddirection { get; set; }
  92. public string windspeedKmph { get; set; }
  93. public string windspeedMiles { get; set; }
  94. public WeatherForecast ToWeatherForecast()
  95. {
  96. return new WeatherForecast
  97. {
  98. Date = DateTime.Parse(date),
  99. HighTemperatureCelsius = int.Parse(tempMaxC),
  100. HighTemperatureFahrenheit = int.Parse(tempMaxF),
  101. LowTemperatureCelsius = int.Parse(tempMinC),
  102. LowTemperatureFahrenheit = int.Parse(tempMinF),
  103. Condition = GetCondition(weatherCode)
  104. };
  105. }
  106. public static WeatherConditions GetCondition(string weatherCode)
  107. {
  108. switch (weatherCode)
  109. {
  110. case "362":
  111. case "365":
  112. case "320":
  113. case "317":
  114. case "182":
  115. return WeatherConditions.Sleet;
  116. case "338":
  117. case "335":
  118. case "332":
  119. case "329":
  120. case "326":
  121. case "323":
  122. case "377":
  123. case "374":
  124. case "371":
  125. case "368":
  126. case "395":
  127. case "392":
  128. case "350":
  129. case "227":
  130. case "179":
  131. return WeatherConditions.Snow;
  132. case "314":
  133. case "311":
  134. case "308":
  135. case "305":
  136. case "302":
  137. case "299":
  138. case "296":
  139. case "293":
  140. case "284":
  141. case "281":
  142. case "266":
  143. case "263":
  144. case "359":
  145. case "356":
  146. case "353":
  147. case "185":
  148. case "176":
  149. return WeatherConditions.Rain;
  150. case "260":
  151. case "248":
  152. return WeatherConditions.Fog;
  153. case "389":
  154. case "386":
  155. case "200":
  156. return WeatherConditions.Thunderstorm;
  157. case "230":
  158. return WeatherConditions.Blizzard;
  159. case "143":
  160. return WeatherConditions.Mist;
  161. case "122":
  162. return WeatherConditions.Overcast;
  163. case "119":
  164. return WeatherConditions.Cloudy;
  165. case "115":
  166. return WeatherConditions.PartlyCloudy;
  167. default:
  168. return WeatherConditions.Sunny;
  169. }
  170. }
  171. }
  172. }