WeatherClient.cs 6.4 KB

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