-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
127 lines (111 loc) · 5.34 KB
/
Form1.cs
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
using Newtonsoft.Json;
using RestSharp;
using System;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
using WeatherForecast.Properties;
namespace WeatherForecast
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> cityNames = new List<string>();
private async void cboxCountry_SelectedIndexChanged(object sender, EventArgs e)
{
cboxCity.Enabled = false;
progressBar1.Visible = true;
progressBar1.Style = ProgressBarStyle.Marquee;
cboxCity.Items.Clear();
string countryName = cboxCountry.Text;
RestRequest request = new RestRequest(Method.GET); //creating get RestRequest
request.AddHeader("apikey", "bxfF4nSrkpHHHjW8mWGThEP3IN5oNuWV"); //apikey for authentication
await Task.Run(()=> CountryIsoToCities(request, CountryNameToCountryIso(request, countryName))); //run methods for get cities
foreach (string item in cityNames) //add cities to combobox
{
if (!cboxCity.Items.Contains(item))
cboxCity.Items.Add(item);
}
cboxCity.Enabled = true;
progressBar1.Visible = false;
}
private string CountryNameToCountryIso(RestRequest request,string countryName)
{
string countryIsoCode = "";
RestClient countryToIsoClient = new RestClient("https://api.apilayer.com/geo/country/name/" + countryName); //country name to
//iso codes
//restclient for api
dynamic cityIsoResponse = JsonConvert.DeserializeObject(countryToIsoClient.Execute(request).Content);
foreach (dynamic i in cityIsoResponse)
{
//try
//{
countryIsoCode = i.alpha3code; //getting iso code by country
//}
//catch { }
}
return countryIsoCode;
}
private void CountryIsoToCities(RestRequest request,string countryIsoCode)
{
string cityName = "";
RestClient isoToCityClient = new RestClient("https://api.apilayer.com/geo/country/cities/" + countryIsoCode); //iso code to cities
//restclient for api
dynamic cityResponse = JsonConvert.DeserializeObject(isoToCityClient.Execute(request).Content); //getting and editing data
foreach (dynamic i in cityResponse) //selecting data required(city names)
{
try
{
cityName = i.state_or_region; //getting cities by country iso code
cityNames.Add(cityName);
}
catch { }
}
}
private void CityToWeatherForecast(string cityName)
{
string apiKey = "ade8d3238fea9c28b48cfdbcf3dbcefb";
string connection = "https://api.openweathermap.org/data/2.5/weather?q="+cityName+"&mode=xml&lang=en&units=metric&appid="+apiKey;
XDocument weather = XDocument.Load(connection);
string degree = weather.Descendants("temperature").ElementAt(0).Attribute("value").Value;
lblDegree.Text = degree + "°";
string state = weather.Descendants("weather").ElementAt(0).Attribute("value").Value;
lblWeather.Text = state;
string wind = weather.Descendants("speed").ElementAt(0).Attribute("value").Value;
lblWind.Text = wind+" m/s";
string humidity = weather.Descendants("humidity").ElementAt(0).Attribute("value").Value;
lblHumidity.Text = humidity+" %";
string feelsLike = weather.Descendants("feels_like").ElementAt(0).Attribute("value").Value;
lblFeelsLike.Text = feelsLike+ "°";
string pressure = weather.Descendants("pressure").ElementAt(0).Attribute("value").Value;
lblPressure.Text = pressure + " hPa";
if (state.Contains("broken"))
pboxWeather.Image = Resources.broken;
else if (state.Contains("cloud"))
pboxWeather.Image = Resources.cloudly;
else if (state.Contains("snow"))
pboxWeather.Image = Resources.snowy;
else if (state.Contains("rain"))
pboxWeather.Image = Resources.rainy;
else if (state.Contains("wind"))
pboxWeather.Image = Resources.windy;
else if (state.Contains("storm"))
pboxWeather.Image = Resources.storm;
else
pboxWeather.Image = Resources.clear;
}
private void cboxCity_SelectedIndexChanged(object sender, EventArgs e)
{
string cityName = cboxCity.Text;
string countryName = cboxCountry.Text;
gboxWeather.Visible = true;
lblCity.Text = cityName + ", " + countryName;
CityToWeatherForecast(cityName);
}
}
}