Skip to content

Commit

Permalink
initial commit of code
Browse files Browse the repository at this point in the history
  • Loading branch information
bagbag committed Mar 16, 2016
1 parent 8faaae0 commit c3a842d
Show file tree
Hide file tree
Showing 15 changed files with 1,864 additions and 0 deletions.
6 changes: 6 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
9 changes: 9 additions & 0 deletions App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="Weather_Images_Downloader.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Weather_Images_Downloader"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>

</Application>
17 changes: 17 additions & 0 deletions App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace Weather_Images_Downloader
{
/// <summary>
/// Interaktionslogik für "App.xaml"
/// </summary>
public partial class App : Application
{
}
}
73 changes: 73 additions & 0 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<Window x:Class="Weather_Images_Downloader.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Name="window" Title="Weather Images Downloader" SizeToContent="Height" Width="525" Closed="window_Closed" ResizeMode="CanMinimize">
<Grid Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<Label Grid.Column="0" Grid.Row="0" Content="Lade Bilder von:" Margin="2" Padding="0"/>
<CheckBox Grid.Column="1" Grid.Row="0" Content="Heute" Margin="2" IsChecked="{Binding Path=Today}"/>
<CheckBox Grid.Column="2" Grid.Row="0" Content="Gestern" Margin="2" IsChecked="{Binding Path=Yesterday}"/>
<CheckBox Grid.Column="3" Grid.Row="0" Content="Vorgestern" Margin="2" IsChecked="{Binding Path=YesterYesterday}"/>

<Label Grid.Column="0" Grid.Row="1" Content="Typ:" Margin="2" Padding="0" />
<CheckBox Grid.Column="1" Grid.Row="1" Content="Regen" Margin="2" IsChecked="{Binding Path=Rain}"/>
<CheckBox Grid.Column="2" Grid.Row="1" Content="Wetter" Margin="2" IsChecked="{Binding Path=Weather}"/>
</Grid>

<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>

<Label Content="Ort(e): " Padding="0" Margin="2"/>
<TextBox Name="areaTextBox" Grid.Column="1" Text="{Binding Path=Areas}" VerticalAlignment="Center" Margin="2"/>
</Grid>

<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Name="downloadButton" Grid.Column="1" Content="Download" Margin="5" Click="downloadButton_Click"/>

<CheckBox Grid.Column="2" Grid.Row="0" Content="Live View" VerticalAlignment="Center" Margin="2" IsChecked="{Binding ElementName=window, Path=LiveView}"/>
</Grid>

<Grid Grid.Row="3">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Name="progressLabel" Content="{Binding Path=ProgressString, ElementName=window}" Padding="0" Margin="2,0,2,-2"/>
<ProgressBar Grid.Row="1" Name="progressBar" Value="{Binding Progress, ElementName=window, Mode=OneWay}" Height="10" Maximum="1" Margin="2,0,2,0"/>
<Label Grid.Row="2" Content="{Binding Error, ElementName=window, Mode=OneWay}" Margin="0,15,0,0"/>
<Image Grid.Row="3" Source="{Binding ElementName=window, Path=WeatherImage}" Height="Auto" Visibility="{Binding ElementName=window, Path=LiveView}"/>
</Grid>
</Grid>
</Window>
272 changes: 272 additions & 0 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
}
}
Loading

0 comments on commit c3a842d

Please sign in to comment.