-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathWeatherClient.cs
More file actions
45 lines (41 loc) · 1.37 KB
/
WeatherClient.cs
File metadata and controls
45 lines (41 loc) · 1.37 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
namespace MSEvangelism.OpenWeatherMap
{
public class WeatherClient
{
public string AppID { get; set; } = "";
private HttpClient cli = new HttpClient();
public WeatherClient(string AppID)
{
this.AppID = AppID;
}
public async Task<WeatherRecord[]> Forecast(string city)
{
var res = await cli.GetStringAsync($"http://api.openweathermap.org/data/2.5/forecast/daily?q={city}&mode=json&units=metric&cnt=7&APPID={AppID}");
var f = new List<WeatherRecord>();
dynamic x = Newtonsoft.Json.JsonConvert.DeserializeObject(res);
foreach (var z in x.list)
{
f.Add(new WeatherRecord()
{
When = Convert((long)z.dt),
Temp = z.temp.day,
Pressure = z.pressure,
Humidity = z.humidity,
});
}
return f.ToArray();
}
private DateTime Convert(long x)
{
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
dtDateTime = dtDateTime.AddSeconds(x).ToLocalTime();
return dtDateTime;
}
}
}