WeatherClient.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.Any())
  46. {
  47. info.CurrentWeather = data.current_condition.First().ToWeatherStatus();
  48. }
  49. info.Forecasts = data.weather.Select(w => w.ToWeatherForecast()).ToArray();
  50. return info;
  51. }
  52. }
  53. class WeatherResult
  54. {
  55. public WeatherData data { get; set; }
  56. }
  57. public class WeatherData
  58. {
  59. public WeatherCondition[] current_condition { get; set; }
  60. public DailyWeatherInfo[] weather { get; set; }
  61. }
  62. public class WeatherCondition
  63. {
  64. public string temp_C { get; set; }
  65. public string temp_F { get; set; }
  66. public string humidity { get; set; }
  67. public string weatherCode { get; set; }
  68. public WeatherStatus ToWeatherStatus()
  69. {
  70. return new WeatherStatus()
  71. {
  72. TemperatureCelsius = int.Parse(temp_C),
  73. TemperatureFahrenheit = int.Parse(temp_F),
  74. Humidity = int.Parse(humidity),
  75. Condition = DailyWeatherInfo.GetCondition(weatherCode)
  76. };
  77. }
  78. }
  79. public class DailyWeatherInfo
  80. {
  81. public string date { get; set; }
  82. public string precipMM { get; set; }
  83. public string tempMaxC { get; set; }
  84. public string tempMaxF { get; set; }
  85. public string tempMinC { get; set; }
  86. public string tempMinF { get; set; }
  87. public string weatherCode { get; set; }
  88. public string winddir16Point { get; set; }
  89. public string winddirDegree { get; set; }
  90. public string winddirection { get; set; }
  91. public string windspeedKmph { get; set; }
  92. public string windspeedMiles { get; set; }
  93. public WeatherForecast ToWeatherForecast()
  94. {
  95. return new WeatherForecast()
  96. {
  97. Date = DateTime.Parse(date),
  98. HighTemperatureCelsius = int.Parse(tempMaxC),
  99. HighTemperatureFahrenheit = int.Parse(tempMaxF),
  100. LowTemperatureCelsius = int.Parse(tempMinC),
  101. LowTemperatureFahrenheit = int.Parse(tempMinF),
  102. Condition = GetCondition(weatherCode)
  103. };
  104. }
  105. public static WeatherConditions GetCondition(string weatherCode)
  106. {
  107. switch (weatherCode)
  108. {
  109. case "362":
  110. case "365":
  111. case "320":
  112. case "317":
  113. case "182":
  114. return WeatherConditions.Sleet;
  115. case "338":
  116. case "335":
  117. case "332":
  118. case "329":
  119. case "326":
  120. case "323":
  121. case "377":
  122. case "374":
  123. case "371":
  124. case "368":
  125. case "395":
  126. case "392":
  127. case "350":
  128. case "227":
  129. case "179":
  130. return WeatherConditions.Snow;
  131. case "314":
  132. case "311":
  133. case "308":
  134. case "305":
  135. case "302":
  136. case "299":
  137. case "296":
  138. case "293":
  139. case "284":
  140. case "281":
  141. case "266":
  142. case "263":
  143. case "359":
  144. case "356":
  145. case "353":
  146. case "185":
  147. case "176":
  148. return WeatherConditions.Rain;
  149. case "260":
  150. case "248":
  151. return WeatherConditions.Fog;
  152. case "389":
  153. case "386":
  154. case "200":
  155. return WeatherConditions.Thunderstorm;
  156. case "230":
  157. return WeatherConditions.Blizzard;
  158. case "143":
  159. return WeatherConditions.Mist;
  160. case "122":
  161. return WeatherConditions.Overcast;
  162. case "119":
  163. return WeatherConditions.Cloudy;
  164. case "115":
  165. return WeatherConditions.PartlyCloudy;
  166. default:
  167. return WeatherConditions.Sunny;
  168. }
  169. }
  170. }
  171. }