-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
120 lines (108 loc) · 3.81 KB
/
Program.cs
File metadata and controls
120 lines (108 loc) · 3.81 KB
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
using System.Net;
using System.Text.Json;
namespace PrApp1PDA;
public class Clouds
{
public int all { get; set; }
}
public class Coord
{
public double lon { get; set; }
public double lat { get; set; }
}
public class Main
{
public double temp { get; set; }
public double feels_like { get; set; }
public double temp_min { get; set; }
public double temp_max { get; set; }
public int pressure { get; set; }
public int humidity { get; set; }
}
public class WetherReport
{
public Coord coord { get; set; }
public Weather[] weather { get; set; }
public string Base { get; set; }
public Main main { get; set; }
public int visibility { get; set; }
public Wind wind { get; set; }
public Clouds clouds { get; set; }
public int dt { get; set; }
public Sys sys { get; set; }
public int timezone { get; set; }
public int id { get; set; }
public string name { get; set; }
public int cod { get; set; }
}
public class Sys
{
public int type { get; set; }
public int id { 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; }
}
public class Wind
{
public double speed { get; set; }
public int deg { get; set; }
}
static class Program
{
public static string GetJsonStringFromUrl(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new(stream);
string jsonString = reader.ReadToEnd();
response.Close();
return jsonString;
}
static void Main()
{
string city = "Tomsk";
try
{
city = File.ReadAllText("def.txt");
}
catch (System.IO.FileNotFoundException)
{
File.Create("def.txt");
}
Console.WriteLine($"Введите город/Нажмите Enter чтобы выбрать город по умолчанию ({city}) или впишите edit чтобы редактировать город по умолчанию");
string inputCity = Console.ReadLine();
if (inputCity != "")
{
if (inputCity == "edit")
{
File.WriteAllText("def.txt", Console.ReadLine());
city = File.ReadAllText("def.txt");
}
}
string key = "fbbba203f51172e21e58ec311cc4c9a3";
string jsonString = GetJsonStringFromUrl($"http://api.openweathermap.org/data/2.5/weather?q={city}&units=metric&APPID={key}");
jsonString = '[' + jsonString + ']'; //figuring this out took severel hours of my life ;_;
var report = JsonSerializer.Deserialize<List<WetherReport>>(jsonString);
foreach (var wetherReport in report)
{
Console.WriteLine($"Прогноз погоды для {wetherReport.name}\n" +
$"Температура за окном {wetherReport.main.temp}, по ощущениям {wetherReport.main.feels_like}\n" +
$"Небо: {wetherReport.weather[0].description}\n" +
$"Влажность воздуха {wetherReport.main.humidity}%\n" +
$"Атмосферное давление {wetherReport.main.pressure}\n" +
$"Скорость ветра {wetherReport.wind.speed} метров в секунду, направление: {wetherReport.wind.deg} градусов\n" +
$"Код ошибки: {wetherReport.cod}\n");
}
Console.ReadKey();
}
}