-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
272 lines (229 loc) · 9.04 KB
/
MainWindow.xaml.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Weather_Images_Downloader.Annotations;
namespace Weather_Images_Downloader
{
public partial class MainWindow : INotifyPropertyChanged
{
private double _progress;
public double Progress
{
get { return _progress; }
private set
{
if (Equals(_progress, value)) return;
_progress = value;
OnPropertyChanged();
}
}
private string _progressString;
public string ProgressString
{
get { return _progressString; }
private set
{
if (Equals(_progressString, value)) return;
_progressString = value;
OnPropertyChanged();
}
}
private string _error;
public string Error
{
get { return _error; }
set
{
if (value == _error) return;
_error = value;
OnPropertyChanged();
}
}
private ImageSource _weatherImage;
public ImageSource WeatherImage
{
get { return _weatherImage; }
set
{
if (Equals(value, _weatherImage)) return;
_weatherImage = value;
OnPropertyChanged();
}
}
private bool _liveView;
public bool LiveView
{
get { return _liveView; }
set
{
if (value == _liveView) return;
_liveView = value;
OnPropertyChanged();
if (!LiveView)
WeatherImage = null;
}
}
private int _total;
private int _counter;
public MainWindow()
{
InitializeComponent();
Progress = 0f;
ProgressString = "-";
Static.LoadSettings();
DataContext = Static.SettingsInstance;
}
private void downloadButton_Click(object sender, RoutedEventArgs e)
{
var weatherImages = new List<WeatherImage>();
var types = new List<string>();
if(Static.SettingsInstance.Rain)
types.Add("grey_flat");
if (Static.SettingsInstance.Weather)
types.Add("composite");
var pattern = new Regex(@"(\w+)");
var matches = pattern.Matches(areaTextBox.Text);
foreach (Match match in matches)
{
foreach (var type in types)
{
if (Static.SettingsInstance.Today)
{
var currentUtcTime = DateTime.UtcNow;
currentUtcTime = currentUtcTime.AddMinutes(- currentUtcTime.Minute % 5);
weatherImages.AddRange(CreateWeatherImages(match.Value, type, DateTime.Today, currentUtcTime));
}
if (Static.SettingsInstance.Yesterday)
weatherImages.AddRange(CreateWeatherImages(match.Value, type, DateTime.Today.AddDays(- 1), DateTime.Today.AddDays(- 1).AddHours(23)));
if (Static.SettingsInstance.YesterYesterday)
weatherImages.AddRange(CreateWeatherImages(match.Value, type, DateTime.Today.AddDays(- 2), DateTime.Today.AddDays(- 2).AddHours(23)));
}
}
_total = weatherImages.Count;
_counter = 0;
var errorCounter = 0;
Error = "";
Task.Run(() =>
{
Dispatcher.Invoke(() =>
{
downloadButton.IsEnabled = false;
});
Parallel.For(0, weatherImages.Count, (i =>
{
try
{
_counter++;
Progress = (double)(_counter) / _total;
ProgressString = (_counter) + "/" + _total + " - " + Math.Round(Progress * 100, 1) + "%";
var weatherImage = weatherImages[i];
var request = WebRequest.CreateHttp(weatherImage.Url);
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36";
var response = request.GetResponse();
var responseStream = response.GetResponseStream();
var directory = Path.Combine(Environment.CurrentDirectory, "weatherimages", weatherImage.Area, weatherImage.Type);
var path = Path.Combine(directory, weatherImage.Filename);
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
using (var fs = new FileStream(path, FileMode.Create))
{
var buffer = new byte[1024];
var totalBytesRead = 0;
while (totalBytesRead < response.ContentLength)
{
var bytesRead = responseStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
}
}
if (LiveView)
{
Dispatcher.Invoke(() =>
{
try
{
WeatherImage = new BitmapImage(new Uri(path));
}
catch
{
// ignored
}
});
}
}
catch (Exception ex)
{
Error = "Error (" + ++errorCounter + "): " + ex.Message;
}
}));
Dispatcher.Invoke(() =>
{
downloadButton.IsEnabled = true;
});
});
}
private WeatherImage[] CreateWeatherImages(string area, string type, DateTime start, DateTime end)
{
var weatherImages = new List<WeatherImage>();
do
{
weatherImages.Add(CreateWeatherImage(area, type, start));
start = start.AddMinutes(5);
} while (start < end);
return weatherImages.ToArray();
}
private WeatherImage CreateWeatherImage(string area, string type, DateTime dateTime)
{
var year = dateTime.ToString("yyyy");
var month = dateTime.ToString("MM");
var day = dateTime.ToString("dd");
var hour = dateTime.ToString("HH");
var minute = dateTime.ToString("mm");
var url = string.Format("http://www.wetteronline.de/?ireq=true&pid=p_radar_map&src=wmapsextract/vermarktung/global2maps/{0}/{1}/{2}/{5}/{6}/{0}{1}{2}{3}{4}_{5}.{7}", year, month, day, hour, minute, area, type, type == "grey_flat" ? "png" : "jpeg");
var file = string.Format("{0}-{1}-{2} {3}-{4} {5} {6}.jpeg", year, month, day, hour, minute, area, type);
return new WeatherImage(type, area, year, month, day, hour,minute, url, file);
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void window_Closed(object sender, EventArgs e)
{
Static.SaveSettings();
}
}
struct WeatherImage
{
public string Type { get; set; }
public string Area { get; set; }
public string Year { get; set; }
public string Month { get; set; }
public string Day { get; set; }
public string Hour { get; set; }
public string Minute { get; set; }
public string Url { get; set; }
public string Filename { get; set; }
public WeatherImage(string type, string area, string year, string month, string day, string hour,string minute, string url = "", string filename = "")
{
Type = type;
Area = area;
Year = year;
Month = month;
Day = day;
Hour = hour;
Minute = minute;
Url = url;
Filename = filename;
}
}
}