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