WeatherProvider.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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.Linq;
  7. using System.Threading;
  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. /// <summary>
  19. /// The _weather semaphore
  20. /// </summary>
  21. private readonly SemaphoreSlim _weatherSemaphore = new SemaphoreSlim(10, 10);
  22. /// <summary>
  23. /// Gets the weather info async.
  24. /// </summary>
  25. /// <param name="location">The location.</param>
  26. /// <param name="cancellationToken">The cancellation token.</param>
  27. /// <returns>Task{WeatherInfo}.</returns>
  28. /// <exception cref="System.ArgumentNullException">location</exception>
  29. public override async Task<WeatherInfo> GetWeatherInfoAsync(string location, CancellationToken cancellationToken)
  30. {
  31. if (string.IsNullOrWhiteSpace(location))
  32. {
  33. throw new ArgumentNullException("location");
  34. }
  35. if (cancellationToken == null)
  36. {
  37. throw new ArgumentNullException("cancellationToken");
  38. }
  39. const int numDays = 5;
  40. const string apiKey = "24902f60f1231941120109";
  41. var url = "http://free.worldweatheronline.com/feed/weather.ashx?q=" + location + "&format=json&num_of_days=" + numDays + "&key=" + apiKey;
  42. Logger.LogInfo("Accessing weather from " + url);
  43. using (var stream = await Kernel.Instance.HttpManager.Get(url, _weatherSemaphore, cancellationToken).ConfigureAwait(false))
  44. {
  45. var data = JsonSerializer.DeserializeFromStream<WeatherResult>(stream).data;
  46. return GetWeatherInfo(data);
  47. }
  48. }
  49. /// <summary>
  50. /// Converst the json output to our WeatherInfo model class
  51. /// </summary>
  52. /// <param name="data">The data.</param>
  53. /// <returns>WeatherInfo.</returns>
  54. private WeatherInfo GetWeatherInfo(WeatherData data)
  55. {
  56. var info = new WeatherInfo();
  57. if (data.current_condition != null)
  58. {
  59. var condition = data.current_condition.FirstOrDefault();
  60. if (condition != null)
  61. {
  62. info.CurrentWeather = condition.ToWeatherStatus();
  63. }
  64. }
  65. if (data.weather != null)
  66. {
  67. info.Forecasts = data.weather.Select(w => w.ToWeatherForecast()).ToArray();
  68. }
  69. return info;
  70. }
  71. }
  72. /// <summary>
  73. /// Class WeatherResult
  74. /// </summary>
  75. class WeatherResult
  76. {
  77. /// <summary>
  78. /// Gets or sets the data.
  79. /// </summary>
  80. /// <value>The data.</value>
  81. public WeatherData data { get; set; }
  82. }
  83. /// <summary>
  84. /// Class WeatherData
  85. /// </summary>
  86. public class WeatherData
  87. {
  88. /// <summary>
  89. /// Gets or sets the current_condition.
  90. /// </summary>
  91. /// <value>The current_condition.</value>
  92. public WeatherCondition[] current_condition { get; set; }
  93. /// <summary>
  94. /// Gets or sets the weather.
  95. /// </summary>
  96. /// <value>The weather.</value>
  97. public DailyWeatherInfo[] weather { get; set; }
  98. }
  99. /// <summary>
  100. /// Class WeatherCondition
  101. /// </summary>
  102. public class WeatherCondition
  103. {
  104. /// <summary>
  105. /// Gets or sets the temp_ C.
  106. /// </summary>
  107. /// <value>The temp_ C.</value>
  108. public string temp_C { get; set; }
  109. /// <summary>
  110. /// Gets or sets the temp_ F.
  111. /// </summary>
  112. /// <value>The temp_ F.</value>
  113. public string temp_F { get; set; }
  114. /// <summary>
  115. /// Gets or sets the humidity.
  116. /// </summary>
  117. /// <value>The humidity.</value>
  118. public string humidity { get; set; }
  119. /// <summary>
  120. /// Gets or sets the weather code.
  121. /// </summary>
  122. /// <value>The weather code.</value>
  123. public string weatherCode { get; set; }
  124. /// <summary>
  125. /// To the weather status.
  126. /// </summary>
  127. /// <returns>WeatherStatus.</returns>
  128. public WeatherStatus ToWeatherStatus()
  129. {
  130. return new WeatherStatus
  131. {
  132. TemperatureCelsius = int.Parse(temp_C),
  133. TemperatureFahrenheit = int.Parse(temp_F),
  134. Humidity = int.Parse(humidity),
  135. Condition = DailyWeatherInfo.GetCondition(weatherCode)
  136. };
  137. }
  138. }
  139. /// <summary>
  140. /// Class DailyWeatherInfo
  141. /// </summary>
  142. public class DailyWeatherInfo
  143. {
  144. /// <summary>
  145. /// Gets or sets the date.
  146. /// </summary>
  147. /// <value>The date.</value>
  148. public string date { get; set; }
  149. /// <summary>
  150. /// Gets or sets the precip MM.
  151. /// </summary>
  152. /// <value>The precip MM.</value>
  153. public string precipMM { get; set; }
  154. /// <summary>
  155. /// Gets or sets the temp max C.
  156. /// </summary>
  157. /// <value>The temp max C.</value>
  158. public string tempMaxC { get; set; }
  159. /// <summary>
  160. /// Gets or sets the temp max F.
  161. /// </summary>
  162. /// <value>The temp max F.</value>
  163. public string tempMaxF { get; set; }
  164. /// <summary>
  165. /// Gets or sets the temp min C.
  166. /// </summary>
  167. /// <value>The temp min C.</value>
  168. public string tempMinC { get; set; }
  169. /// <summary>
  170. /// Gets or sets the temp min F.
  171. /// </summary>
  172. /// <value>The temp min F.</value>
  173. public string tempMinF { get; set; }
  174. /// <summary>
  175. /// Gets or sets the weather code.
  176. /// </summary>
  177. /// <value>The weather code.</value>
  178. public string weatherCode { get; set; }
  179. /// <summary>
  180. /// Gets or sets the winddir16 point.
  181. /// </summary>
  182. /// <value>The winddir16 point.</value>
  183. public string winddir16Point { get; set; }
  184. /// <summary>
  185. /// Gets or sets the winddir degree.
  186. /// </summary>
  187. /// <value>The winddir degree.</value>
  188. public string winddirDegree { get; set; }
  189. /// <summary>
  190. /// Gets or sets the winddirection.
  191. /// </summary>
  192. /// <value>The winddirection.</value>
  193. public string winddirection { get; set; }
  194. /// <summary>
  195. /// Gets or sets the windspeed KMPH.
  196. /// </summary>
  197. /// <value>The windspeed KMPH.</value>
  198. public string windspeedKmph { get; set; }
  199. /// <summary>
  200. /// Gets or sets the windspeed miles.
  201. /// </summary>
  202. /// <value>The windspeed miles.</value>
  203. public string windspeedMiles { get; set; }
  204. /// <summary>
  205. /// To the weather forecast.
  206. /// </summary>
  207. /// <returns>WeatherForecast.</returns>
  208. public WeatherForecast ToWeatherForecast()
  209. {
  210. return new WeatherForecast
  211. {
  212. Date = DateTime.Parse(date),
  213. HighTemperatureCelsius = int.Parse(tempMaxC),
  214. HighTemperatureFahrenheit = int.Parse(tempMaxF),
  215. LowTemperatureCelsius = int.Parse(tempMinC),
  216. LowTemperatureFahrenheit = int.Parse(tempMinF),
  217. Condition = GetCondition(weatherCode)
  218. };
  219. }
  220. /// <summary>
  221. /// Gets the condition.
  222. /// </summary>
  223. /// <param name="weatherCode">The weather code.</param>
  224. /// <returns>WeatherConditions.</returns>
  225. public static WeatherConditions GetCondition(string weatherCode)
  226. {
  227. switch (weatherCode)
  228. {
  229. case "362":
  230. case "365":
  231. case "320":
  232. case "317":
  233. case "182":
  234. return WeatherConditions.Sleet;
  235. case "338":
  236. case "335":
  237. case "332":
  238. case "329":
  239. case "326":
  240. case "323":
  241. case "377":
  242. case "374":
  243. case "371":
  244. case "368":
  245. case "395":
  246. case "392":
  247. case "350":
  248. case "227":
  249. case "179":
  250. return WeatherConditions.Snow;
  251. case "314":
  252. case "311":
  253. case "308":
  254. case "305":
  255. case "302":
  256. case "299":
  257. case "296":
  258. case "293":
  259. case "284":
  260. case "281":
  261. case "266":
  262. case "263":
  263. case "359":
  264. case "356":
  265. case "353":
  266. case "185":
  267. case "176":
  268. return WeatherConditions.Rain;
  269. case "260":
  270. case "248":
  271. return WeatherConditions.Fog;
  272. case "389":
  273. case "386":
  274. case "200":
  275. return WeatherConditions.Thunderstorm;
  276. case "230":
  277. return WeatherConditions.Blizzard;
  278. case "143":
  279. return WeatherConditions.Mist;
  280. case "122":
  281. return WeatherConditions.Overcast;
  282. case "119":
  283. return WeatherConditions.Cloudy;
  284. case "115":
  285. return WeatherConditions.PartlyCloudy;
  286. default:
  287. return WeatherConditions.Sunny;
  288. }
  289. }
  290. }
  291. }