Repository navigation
Expand file tree
/
Copy pathWeatherHandler.cs
More file actions
57 lines (49 loc) · 2.24 KB
/
Copy pathWeatherHandler.cs
File metadata and controls
57 lines (49 loc) · 2.24 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
46
47
48
49
50
51
52
53
54
55
56
57
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
namespace WeatherDesktop
{
internal class WeatherHandler
{
private string _token;
private string _lat;
private string _lon;
public WeatherHandler()
{
FileStream stream = new FileStream(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "\\sys.env", FileMode.Open);
StreamReader sr = new StreamReader(stream);
_token = sr.ReadToEnd();
}
private void UpdateCoords(string cityName)
{
string _url = $"http://api.openweathermap.org/geo/1.0/direct?q={cityName},RUS&appid={_token}";
HttpWebRequest request = WebRequest.Create(_url) as HttpWebRequest;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
string json;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
json = reader.ReadToEnd();
}
List<Coordinates> coordinates = JsonConvert.DeserializeObject(json, typeof(List<Coordinates>)) as List<Coordinates>;
//List, так как может быть несколько городов с одинаковым названием, а значит несколько координат
_lat = coordinates[0].Lat.ToString();
_lon = coordinates[0].Lon.ToString();
}
public WeatherInfo GetActualWeather(string cityName)
{
UpdateCoords(cityName);
string _url = $"http://api.openweathermap.org/data/2.5/weather?lat={_lat}&lon={_lon}&appid={_token}&units=metric";
HttpWebRequest request = WebRequest.Create(_url) as HttpWebRequest;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
string json;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
json = reader.ReadToEnd();
}
WeatherInfo result = JsonConvert.DeserializeObject(json, typeof(WeatherInfo)) as WeatherInfo;
return result;
}
}
}