Skip to content

Commit cb62918

Browse files
committed
Merge branch 'release/v1.2'
2 parents 5f61b24 + 480c9d0 commit cb62918

9 files changed

Lines changed: 152 additions & 52 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ riderModule.iml
55
/_ReSharper.Caches/
66
.idea/.idea.console-weather/.idea/
77
console-weather/config.json
8+
9+
console-weather/nupkg/

README.md

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,22 @@ Console Weather project is a simple application project to check weather in cons
99
- Show current Wind Speed and Direction
1010
- Show Air Pressure, Humidity and Cloud Cover
1111
- Show Weather Alerts (if exists)
12+
- Show Weather Forecast (for a next day)
1213

1314

1415

1516
## Installation
1617

1718
If you installed [.NET 7.0 or newer](https://dotnet.microsoft.com/en-us/download), just simply paste this command into console:
1819
```bash
19-
dotnet tool install --global console-weather --version 1.1.0
20+
dotnet tool install --global console-weather --version 1.2.0
2021
```
22+
23+
If you want to update, just type:
24+
```bash
25+
dotnet tool update --global console-weather
26+
```
27+
2128
If you running app first time, it will ask you for the [Weather API](https://www.weatherapi.com/):
2229
```
2330
Error: API returned status code Forbidden
@@ -39,31 +46,47 @@ To get weather info from a specified city, type:
3946
weather -c "City Name"
4047
```
4148

49+
To get weather information without alerts, type:
50+
```bash
51+
weather --no-alerts
52+
```
53+
54+
To get forecast for the next day, type:
55+
```bash
56+
weather -f
57+
```
58+
59+
Full command list:
60+
```bash
61+
-c, --city <city> Get city name
62+
--no-alerts Hide weather alerts [default: False]
63+
-f, --forecast Show weather forecast [default: False]
64+
--version Show version information
65+
-?, -h, --help Show help and usage information
66+
```
67+
4268

4369
## Demo
4470

4571
Example input:
46-
```bash
47-
weather -c "London"
72+
```
73+
weather -c "Kansas City" --no-alerts
4874
```
4975

5076
Example output:
5177
```
52-
Weather for London, United Kingdom (last update: 2023-04-15 16:00):
53-
54-
Teperature: 14,0°C (feels like: 13,7°C)
55-
Weather Condition: Partly cloudy
56-
Wind Speed: 13,0kmp (WNW)
57-
Air Pressure: 1021,0 mbar
58-
Humidity: 59%
59-
Cloud Cover: 25%
60-
61-
Weather Alerts:
62-
<none>
78+
The current weather for Kansas City in United States of America is Sunny
79+
The temperature is 16,1°C, but feels like: 16,1°C
80+
81+
Current Wind speed is 16,9 kmp SSW
82+
Current Air Pressure is 1020,0 mbar
83+
Current Humidity is 33%
84+
Current Cloud Cover is 0%
85+
Last update: 2023-04-24 12:30
6386
```
6487
## Acknowledgements
6588

66-
- [NuGet Package page](https://www.nuget.org/packages/console-weather/)
89+
- [NuGet Page](https://www.nuget.org/packages/console-weather/)
6790

6891

6992
## License and support
@@ -75,3 +98,7 @@ For any project-related cases, please contact me on Discord: pazurkota#1001
7598
## Lessons Learned
7699

77100
This project basically was created to learn how to work with the API (in this case, weather API) and creating CLI apps using C#
101+
102+
103+
104+

console-weather/API/ApiData.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
using Newtonsoft.Json;
66
using RestSharp;
77
using static console_weather.API.ApiKeyHandler;
8+
using static console_weather.Settings;
89

910
namespace console_weather.API;
1011

1112
public class ApiData {
1213
private const string BASE_URL = "http://api.weatherapi.com/v1/"; // Base API URL
13-
public static string CITYNAME; // City name
1414

1515
// Get request from API
1616
private string GetRequest() {
1717
string? apiKey = GetApiKey();
18-
string cityName = CITYNAME;
18+
string cityName = CityName;
1919

2020
try {
2121
// Get API Key if invalid or not given
@@ -30,7 +30,7 @@ private string GetRequest() {
3030
};
3131

3232
var client = new RestClient(options);
33-
var request = new RestRequest($"forecast.json?key={apiKey}&q={cityName}&aqi=no&alerts=yes");
33+
var request = new RestRequest($"forecast.json?key={apiKey}&q={cityName}&aqi=no&alerts=yes&days=2");
3434

3535
var response = client.Execute(request).Content;
3636

@@ -78,7 +78,8 @@ public Weather.Weather ParseData() {
7878
return new Weather.Weather {
7979
Current = jsonText.Current,
8080
Location = jsonText.Location,
81-
Alerts = jsonText.Alerts
81+
Alerts = jsonText.Alerts,
82+
Forecast = jsonText.Forecast
8283
};
8384
}
8485
}

console-weather/Program.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System.CommandLine;
22
using System.CommandLine.Builder;
33
using System.CommandLine.Parsing;
4-
using console_weather;
54
using console_weather.API;
5+
using static console_weather.Settings;
66

77
namespace console_weather;
88

@@ -13,10 +13,24 @@ public static async Task<int> Main(string[] args) {
1313
"Get city name"
1414
);
1515

16+
var alertsOption = new Option<bool>(
17+
"--no-alerts",
18+
() => false,
19+
"Hide weather alerts"
20+
);
21+
22+
var forecastOption = new Option<bool>(
23+
new []{"--forecast", "-f"},
24+
() => false,
25+
"Show weather forecast"
26+
);
27+
1628
var rootCommand = new RootCommand() {
17-
cityOption
29+
cityOption,
30+
alertsOption,
31+
forecastOption
1832
};
19-
rootCommand.SetHandler(OnHandle, cityOption);
33+
rootCommand.SetHandler(OnHandle, cityOption, alertsOption, forecastOption);
2034

2135
var commandLineBuilder = new CommandLineBuilder(rootCommand)
2236
.UseDefaults();
@@ -25,13 +39,15 @@ public static async Task<int> Main(string[] args) {
2539
return await parser.InvokeAsync(args).ConfigureAwait(false);
2640
}
2741

28-
private static void OnHandle(string str) {
29-
// Get city name from IP address if city name not provided
42+
private static void OnHandle(string str, bool showAlerts, bool showForecast) {
3043
str ??= "auto:ip";
3144

32-
ApiData.CITYNAME = str;
33-
ApiData data = new ApiData();
45+
CityName = str;
46+
DontShowAlerts = showAlerts;
47+
ShowForecast = showForecast;
3448

49+
// Parse and show data
50+
ApiData data = new ApiData();
3551
Console.WriteLine(data.ParseData());
3652
}
3753
}

console-weather/Settings.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace console_weather;
2+
3+
public struct Settings {
4+
public static string CityName { get; set; } = null!;
5+
public static bool DontShowAlerts { get; set; }
6+
public static bool ShowForecast { get; set; }
7+
}

console-weather/Weather/Alert.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,18 @@ public class Alert {
1616
public class Alerts {
1717
[JsonProperty("alert")]
1818
public List<Alert> WeatherAlerts { get; set; }
19+
20+
public override string ToString() {
21+
if (Settings.DontShowAlerts || WeatherAlerts.Count == 0) {
22+
return null;
23+
}
24+
25+
string str = "";
26+
27+
foreach (var alert in WeatherAlerts) {
28+
str += $"{alert.AlertHeadline}:\n{alert.AlertDescription}\n\n";
29+
}
30+
31+
return str;
32+
}
1933
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Newtonsoft.Json;
2+
3+
namespace console_weather.Weather;
4+
5+
public class ForecastDay {
6+
[JsonProperty("date")] public string Date { get; set; } = null!;
7+
[JsonProperty("day")] public Day Day { get; set; } = null!;
8+
}
9+
10+
public class Day {
11+
[JsonProperty("avgtemp_c")] public decimal AvgTemp { get; set; }
12+
[JsonProperty("maxtemp_c")] public decimal MaxTemp { get; set; }
13+
[JsonProperty("mintemp_c")] public decimal MinTemp { get; set; }
14+
[JsonProperty("maxwind_kph")] public decimal MaxWindSpeed { get; set; }
15+
[JsonProperty("daily_chance_of_rain")] public decimal ChanceOfRain { get; set; }
16+
[JsonProperty("daily_chance_of_snow")] public decimal ChanceOfSnow { get; set; }
17+
[JsonProperty("condition")] public Condition Condition { get; set; } = null!;
18+
}
19+
20+
public class Forecast {
21+
[JsonProperty("forecastday")] public List<ForecastDay> ForecastsDay { get; set; } = null!;
22+
23+
public override string ToString() {
24+
if (!Settings.ShowForecast) {
25+
return null;
26+
}
27+
28+
string conditionState = ForecastsDay[1].Day.Condition.ConditionState;
29+
30+
string maxTemp = ForecastsDay[1].Day.MaxTemp.ToString();
31+
string minTemp = ForecastsDay[1].Day.MinTemp.ToString();
32+
string avgTemp = ForecastsDay[1].Day.MinTemp.ToString();
33+
34+
string maxWind = ForecastsDay[1].Day.MaxTemp.ToString();
35+
36+
string chanceOfRain = ForecastsDay[1].Day.ChanceOfRain.ToString();
37+
string chanceOfSnow = ForecastsDay[1].Day.ChanceOfSnow.ToString();
38+
39+
string str = $"\n\nTomorrow it will be {conditionState}"
40+
+ $"\nThe temperature range will be around {minTemp}°C to {maxTemp}°C, with average of {avgTemp}°C"
41+
+ $"\nThe maximum wind speed will be around {maxWind} kph"
42+
+ $"\nThe chance of rain/snow: {chanceOfRain}% / {chanceOfSnow}%";
43+
44+
return str;
45+
}
46+
}

console-weather/Weather/Weather.cs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,19 @@ public class Weather {
55
public Location Location;
66
public Current Current;
77
public Alerts Alerts;
8+
public Forecast Forecast;
89

910
public override string ToString() {
10-
string str = $"Weather for {Location.Name}, {Location.Country} (last update: {Current.LastUpdated}):";
11+
string str = $"The current weather for {Location.Name} in {Location.Country} is {Current.Condition.ConditionState}\n" +
12+
$"The temperature is {Current.Temperature}°C, but feels like: {Current.FeelsLikeTemp}°C\n\n";
1113

12-
str += $"\n\nTemperature: {Current.Temperature}°C (feels like: {Current.FeelsLikeTemp}°C)";
13-
str += $"\nWeather Condition: {Current.Condition.ConditionState}";
14-
str += $"\nWind Speed: {Current.WindSpeed}kmp ({Current.WindDirection})";
15-
str += $"\nAir Pressure: {Current.Pressure} mbar";
16-
str += $"\nHumidity: {Current.Humidity}%";
17-
str += $"\nCloud Cover: {Current.Cloud}%";
18-
str += $"\n\nWeather Alerts:\n{ShowWeatherAlerts()}";
19-
20-
return str;
21-
}
22-
23-
private string ShowWeatherAlerts() {
24-
// Return "<none>" if no alerts issued
25-
if (Alerts.WeatherAlerts.Count == 0) {
26-
return "<none>";
27-
}
28-
29-
string str = "";
30-
31-
foreach (var alert in Alerts.WeatherAlerts) {
32-
str += $"{alert.AlertEvent} issued by {alert.AlertHeadline}:\n{alert.AlertDescription}\n\n";
33-
}
14+
str += $"{Alerts}";
15+
str += $"Current Wind speed is {Current.WindSpeed} kmp {Current.WindDirection}\n";
16+
str += $"Current Air Pressure is {Current.Pressure} mbar\n";
17+
str += $"Current Humidity is {Current.Humidity}%\n";
18+
str += $"Current Cloud Cover is {Current.Cloud}%\n";
19+
str += $"Last update: {Current.LastUpdated}";
20+
str += $"{Forecast}";
3421

3522
return str;
3623
}

console-weather/console-weather.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1616
<PackageProjectUrl>https://github.com/pazurkota/console-weather</PackageProjectUrl>
1717
<Description>Simple CLI App to check weather in C#</Description>
18-
<PackageVersion>1.1</PackageVersion>
19-
<VersionPrefix>1.1</VersionPrefix>
18+
<PackageVersion>1.2</PackageVersion>
19+
<VersionPrefix>1.2</VersionPrefix>
2020
<Title>Console Weather</Title>
2121
<Copyright>Copyright (c) 2023 pazurk0ta</Copyright>
2222
<RepositoryUrl>https://github.com/pazurkota/console-weather</RepositoryUrl>
2323
<RepositoryType>git</RepositoryType>
24-
<PackageReleaseNotes>Added auto-city lookup via IP</PackageReleaseNotes>
24+
<PackageReleaseNotes>Added "--no-alerts" and "--forecast" arguments :D</PackageReleaseNotes>
2525
</PropertyGroup>
2626

2727
<ItemGroup>

0 commit comments

Comments
 (0)