Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

AQ56DX - Weather #9

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Weather/API/WeatherAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Összességében szép munka. Pár apró komment amibe bele tudtam kötni, de tényleg szép. Well done.

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Weather.Controller
{
internal static class WeatherAPI
{
public static async Task<string> GetWeatherDataAsync()
{
HttpClient client = new HttpClient();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A HttpClient IDisposable, szóval egy using blokk-ban lenne érdemes használni, mert memory leakelni fog.

string res = String.Empty;
HttpResponseMessage response = await client.GetAsync("https://api.openweathermap.org/data/2.5/forecast?lat=46.253&lon=20.14824&units=metric&appid=9c5ec3c52fdd66e52bba3b00dc4fcc37");
if (response.IsSuccessStatusCode)
{
res = await response.Content.ReadAsStringAsync();
}
else
{
Console.WriteLine("An error occured during reaching weather web API.");
}
return res;
}
}
}
15 changes: 15 additions & 0 deletions Weather/Commands/IWeatherCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Weather.Commands
{
internal interface IWeatherCommand
{
string Name { get; }

Task<bool> Execute();
}
}
19 changes: 19 additions & 0 deletions Weather/Commands/WeatherCommandExit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Weather.Commands
{
internal class WeatherCommandExit : IWeatherCommand
{
public string Name => "exit";

public Task<bool> Execute()
{
Environment.Exit(0);
return null;
}
}
}
26 changes: 26 additions & 0 deletions Weather/Commands/WeatherCommandGetAll.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weather.Controllers;
using Weather.Models;

namespace Weather.Commands
{
internal class WeatherCommandGetAll : IWeatherCommand
{
public string Name => "get all";

public async Task<bool> Execute()
{
var response = await WeatherReportParser.GetCurrentWeather();
if (response == null || response.list == null || response.list.Count == 0) return false;
foreach (ModelWeatherReport i in response.list)
{
Console.WriteLine(i.ToString());
}
return true;
}
}
}
27 changes: 27 additions & 0 deletions Weather/Commands/WeatherCommandGetNow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weather.Controllers;
using Weather.Models;

namespace Weather.Commands
{
internal class WeatherCommandGetNow : IWeatherCommand
{
public string Name => "get now";

public async Task<bool> Execute()
{
var response = await WeatherReportParser.GetCurrentWeather();
if (response == null || response.list == null || response.list.Count == 0) return false;
DateTime time;
var now = response.list.Find(x => DateTime.TryParse(x.dt_txt, out time) && DateTime.Parse(x.dt_txt) > DateTime.Now);
if (now == null) return false;
var builder = new WeatherReportStringBuilder();
Console.WriteLine($"At the time: {builder.formatWeatherReport(now)}");
return true;
}
}
}
27 changes: 27 additions & 0 deletions Weather/Commands/WeatherCommandGetNowShort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weather.Controllers;
using Weather.Models;

namespace Weather.Commands
{
internal class WeatherCommandGetNowShort : IWeatherCommand
{
public string Name => "get now short";

public async Task<bool> Execute()
{
var response = await WeatherReportParser.GetCurrentWeather();
if (response == null || response.list == null || response.list.Count == 0) return false;
DateTime time;
var now = response.list.Find(x => DateTime.TryParse(x.dt_txt, out time) && DateTime.Parse(x.dt_txt) > DateTime.Now);
if (now == null) return false;
var builder = new WeatherReportStringBuilder();
Console.WriteLine($"{builder.shortFromatWeatherReport(now)}");
return true;
}
}
}
29 changes: 29 additions & 0 deletions Weather/Commands/WeatherCommandGetToday.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weather.Controllers;

namespace Weather.Commands
{
internal class WeatherCommandGetToday : IWeatherCommand
{
public string Name => "get today";

public async Task<bool> Execute()
{
var response = await WeatherReportParser.GetCurrentWeather();
if (response == null || response.list == null || response.list.Count == 0) return false;
DateTime time;
var today = response.list.FindAll(x => DateTime.TryParse(x.dt_txt, out time) && DateTime.Parse(x.dt_txt).Day == DateTime.Now.Day);
var builder = new WeatherReportStringBuilder();
if (today.Count == 0) return false;
Console.WriteLine("Todays forecast:");
foreach (var i in today) {
Console.WriteLine(builder.formatWeatherReport(i));
}
return true;
}
}
}
34 changes: 34 additions & 0 deletions Weather/Commands/WeatherCommandInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weather.Controllers;

namespace Weather.Commands
{
internal class WeatherCommandInfo : IWeatherCommand
{
public string Name => "info";

public async Task<bool> Execute()
{
var response = await WeatherReportParser.GetCurrentWeather();
if (response == null || response.list == null || response.list.Count == 0) return false;
DateTime time;
foreach (var weather in response.list)
{
if (String.IsNullOrEmpty(weather.dt_txt) || !DateTime.TryParse(weather.dt_txt, out time))
{
Console.WriteLine("Couldn't find oldest and youngest weather report.");
return false;
}
}
var youngest = response.list.MaxBy(x => DateTime.Parse(x.dt_txt)); // line 19: checked already if x.dt_txt is null or empty or can not be parsed
var oldest = response.list.MinBy(x => DateTime.Parse(x.dt_txt)); // line 19: checked already if x.dt_txt is null or empty or can not be parsed
Console.WriteLine($"\tOldest data: {oldest.dt_txt}");
Console.WriteLine($"\tYoungest data: {youngest.dt_txt}");
return true;
}
}
}
36 changes: 36 additions & 0 deletions Weather/Commands/WeatherCommandRefresh.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weather.Controller;
using Weather.Controllers;
using Weather.Models;

namespace Weather.Commands
{
internal class WeatherCommandRefresh : IWeatherCommand
{
public string Name => "refresh";

public async Task<bool> Execute()
{
string apiResponse = String.Empty;
try
{
apiResponse = await WeatherAPI.GetWeatherDataAsync();
}
catch (Exception e)
{/*aswswarfgddujjjklkÉL122
65 <--- ez itt a macska műve*/
Console.WriteLine(e.Message);
return false;
}
if(!(await WeatherIO.WriteOut(apiResponse)))
{
return false;
}
return true;
}
}
}
51 changes: 51 additions & 0 deletions Weather/Controllers/WeatherIO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Weather.Controllers
{
internal static class WeatherIO
{
private static string path = Path.Combine(AppContext.BaseDirectory, "weather_data.json");

public static async Task<bool> WriteOut(string message)
{
try
{
using (StreamWriter sw = File.CreateText(path))
{
await sw.WriteLineAsync(message);
}
}
catch (IOException ioe)
{
Console.WriteLine(ioe.Message);
return false;
}
return true;
}

public static async Task<string> ReadIn()
{
string data = string.Empty;
try
{
using (StreamReader sr = File.OpenText(path))
{
data = await sr.ReadToEndAsync();
}
}
catch (FileNotFoundException fnfe)
{
Console.WriteLine($"Couldn't find file at: {path}. TRY RUNNING 'refresh'!");
}
catch (IOException ioe)
{
Console.WriteLine(ioe.Message);
}
return data;
}
}
}
56 changes: 56 additions & 0 deletions Weather/Controllers/WeatherReportParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
using Weather.Models;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using System.Text.Json.Serialization;

namespace Weather.Controllers
{
internal static class WeatherReportParser
{
private static JsonSerializerOptions parserOptions = new()
{
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
};

private static ModelJsonResponse? DeserializeModelWeather(string input)
{
ModelJsonResponse? wObject = JsonSerializer.Deserialize<ModelJsonResponse>(input, parserOptions);
if (wObject == null)
{
Console.WriteLine("Couldn't parse to ModelJsonResponse object from input string.");
}
return wObject;
}

public static string SerializeModelWeather(ModelJsonResponse input)
{
return JsonSerializer.Serialize(input, parserOptions);
}

public static async Task<ModelJsonResponse?> GetCurrentWeather()
{
string data = await WeatherIO.ReadIn();
ModelJsonResponse modelJsonResponse;
try
{
modelJsonResponse = DeserializeModelWeather(data);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}
if (modelJsonResponse == null || modelJsonResponse.list == null)
{
Console.WriteLine("The weather report which is provided by the weather API doesn't conatain any actual data.");
}
return modelJsonResponse;
}
}
}
Loading