-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
146 lines (128 loc) · 4.73 KB
/
Program.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
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace WeatherApp2
{
class Program
{
const string API_KEY_FILE = "apiKey.txt";
//static string filePath = Path.Combine(Environment.CurrentDirectory, @"Data\", API_KEY_FILE);
static string filePath = Path.Combine(Environment.CurrentDirectory, @"Data\", API_KEY_FILE);
const string BASE_URL = "https://api.openweathermap.org/data/2.5/weather?";
static string url;
static string API_KEY;
static string CITY;
static async Task Main(string[] args)
{
SetUpAPI();
Console.WriteLine("API Key will be removed from Console Screen shortly");
Thread.Sleep(3000);
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Clearing API Key from Console...");
Thread.Sleep(500);
Console.Clear();
Thread.Sleep(100);
}
Console.WriteLine("What City do you want to know the Weather?");
CITY = Console.ReadLine();
url = BASE_URL + "appid=" + API_KEY + "&q=" + CITY;
try
{
WeatherData weatherData = await GetWeatherDataAsync();
Console.WriteLine($"Currently, it's {weatherData.Temperature} °C in {CITY}!");
Console.WriteLine($"The Weather is {weatherData.WeatherCondition}!");
Console.WriteLine(GetResponseOnWeather(weatherData.Temperature));
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
private static void SetUpAPI()
{
if (!File.Exists(filePath))
{
Console.WriteLine("Error reading API Key: File does not Exist");
throw new Exception($"Error reading API Key: File does not Exist");
}
FileStream fileStream = new FileStream(filePath, FileMode.Open);
StreamReader streamReader = new StreamReader(fileStream);
string apiKey = streamReader.ReadLine();
API_KEY = apiKey;
if (string.IsNullOrEmpty(API_KEY))
{
Console.WriteLine("Error reading API Key: File is Empty");
throw new Exception($"Error reading API Key: File is Empty");
}
Console.WriteLine($"Sucessfully read API Key: {apiKey}");
fileStream.Close();
streamReader.Close();
//old reading file method
//string apiKey = File.ReadAllText(filePath);
//API_KEY = apiKey;
}
static async Task<WeatherData> GetWeatherDataAsync()
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
string data = await response.Content.ReadAsStringAsync();
return ParseWeatherData(data);
}
else
{
throw new Exception($"Failed to fetch data. Status code: {response.StatusCode}");
}
}
}
private static WeatherData ParseWeatherData(string _data)
{
JObject jsonResponse = JObject.Parse(_data);
double temperatureKelvin = jsonResponse["main"]["temp"].Value<double>();
double temperatureCelsius = temperatureKelvin - 273.15;
string weatherCondition = jsonResponse["weather"]?[0]?["main"]?.ToString() ?? "Unknown";
return new WeatherData
{
Temperature = temperatureCelsius,
WeatherCondition = weatherCondition
};
}
private static string GetResponseOnWeather(double _temperature)
{
if (_temperature >= 30)
{
return "It's so HOT!";
}
else if (_temperature >= 15 && _temperature < 30)
{
return "Such a nice Weather!";
}
else if (_temperature <= 14 && _temperature >= 5)
{
return "Cold! But it's ok";
}
else if (_temperature <= 4)
{
return "Brrrr... It's freezing";
}
else
{
return "Dont even know what to say";
}
}
}
}
class WeatherData
{
public double Temperature { get; set; }
public string WeatherCondition { get; set; }
}