WeatherClient.cs 6.5 KB

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