WeatherProvider.cs 11 KB

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