-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweatherAPI.cs
146 lines (121 loc) · 4.65 KB
/
weatherAPI.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* This program can run on Windows or Linux which takes as input the name of a
city and provides information about the weather for that city.
Name: Sean Chen
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace weatherAPI
{
class Program
{
static void Main(string[] args)
{
bool check = true;
//set global variables
HttpResponseMessage response;
string result;
Console.WriteLine("* Welcome to the weather station hosted by Sean Chen *");
Console.Write("\n\nPlease input a city name: ");
while (check)
{
string city = Console.ReadLine();
using (var client = new HttpClient())
{
//make the API call
client.BaseAddress = new Uri("http://api.openweathermap.org/data/2.5/");
response = client.GetAsync("weather?q=" + city + "&APPID=0bee2c23e9f918280c8cd64bde552d5a").Result;
try
{
check = false;
response.EnsureSuccessStatusCode();
}
catch
{
//catch the error
check = true;
Console.Write("Error: ");
Console.WriteLine(response.StatusCode);
Console.Write("\nPlease input a valid city name: ");
continue;
}
}
result = response.Content.ReadAsStringAsync().Result;
Rootobject weatherDetails = JsonConvert.DeserializeObject<Rootobject>(result);
//get the temperature and convert the int type celsius number
double tempCelsius = weatherDetails.main.temp - 273.15;
int tempInt = Convert.ToInt32(tempCelsius);
//display the weather information
Console.WriteLine("\nThe temperature: " + tempInt +" °C");
Console.WriteLine("\nThe pressure: "+ weatherDetails.main.pressure + " hpa");
Console.WriteLine("\nThe humidity: "+ weatherDetails.main.humidity + " %");
Console.WriteLine("\nThe wind speed: " + weatherDetails.wind.speed + " m/s");
Console.WriteLine("\nThe Geo coords: [" + weatherDetails.coord.lat + ", " + weatherDetails.coord.lon + "]");
}
Console.WriteLine("\nThank you for using the weather station!\n");
}
}
public class Rootobject
{
public Coord coord { get; set; }
public Weather[] weather { get; set; }
public string _base { get; set; }
public Main main { get; set; }
public Wind wind { get; set; }
public Clouds clouds { get; set; }
public Rain rain { get; set; }
public int dt { get; set; }
public Sys sys { get; set; }
public int id { get; set; }
public string name { get; set; }
public int cod { get; set; }
}
public class Coord
{
public float lon { get; set; }
public float lat { get; set; }
}
public class Main
{
public float temp { get; set; }
public int pressure { get; set; }
public int humidity { get; set; }
public float temp_min { get; set; }
public float temp_max { get; set; }
}
public class Wind
{
public float speed { get; set; }
public int deg { get; set; }
}
public class Clouds
{
public int all { get; set; }
}
public class Rain
{
public int _3h { get; set; }
}
public class Sys
{
public int type { get; set; }
public int id { get; set; }
public float message { get; set; }
public string country { get; set; }
public int sunrise { get; set; }
public int sunset { get; set; }
}
public class Weather
{
public int id { get; set; }
public string main { get; set; }
public string description { get; set; }
public string icon { get; set; }
}
}