WeatherProvider.cs 11 KB

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