-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
167 lines (136 loc) · 5.11 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
using Microsoft.Win32;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
namespace BBCD3_Desktop;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
string savePath = Environment.GetEnvironmentVariable("USERPROFILE") + @"\" + "Downloads";
public MainWindow()
{
InitializeComponent();
foreach(string key in SOURCES.All.Keys)
{
ChannelsList.Items.Add(key);
}
Downloader.StatusUpdate += Downloader_StatusUpdate;
Downloader.DownloadError += Downloader_DownloadError;
Downloader.ProgressUpdated += (sender, progress) =>
{
Dispatcher.Invoke(() => StatusProgress.Value = progress);
};
StartTimePicker.Value = DateTime.Now;
DatePicker.SelectedDate = DateTime.Now;
}
private void Downloader_StatusUpdate(object sender, string e)
{
//invoke
Dispatcher.Invoke(() =>
{
StatusText.Text = $"Status: {e}";
});
}
private void Downloader_DownloadError(object sender, string e)
{
//invoke
Dispatcher.Invoke(() =>
{
if(e.Contains("Forbidden"))
{
ShowError("Couldn't download. This channel link is geo-blocked and can't be accessed from your location.\n\nDebug information\nError occured at stage '" + StatusText.Text.Replace("Status: ", "") + "': " + e);
}
else if(e.Contains("Not Found"))
{
ShowError("Couldn't download. You can only grab as far back as ~13 days.");
}
ShowError("Couldn't download. Check the date you've selected isn't in the future and not too far in the past (todo: check exactly how far back it can grab !!!)\n\nDebug information\nError occured at stage '"+ StatusText.Text.Replace("Status: ","")+ "': " + e);
});
}
private void ChangePathBtn_Click(object sender, RoutedEventArgs e)
{
//open file dialog
var folderDialog = new OpenFolderDialog
{
Title = "Select a folder to save to",
InitialDirectory = savePath
};
if (folderDialog.ShowDialog() == true)
{
var folderName = folderDialog.FolderName;
savePath = folderName;
SavePathText.Text = "Saving to: "+savePath;
}
}
public void ShowError(string content)
{
PrettyError error = new PrettyError();
error.errorContent = content;
error.ShowDialog();
}
private bool Validate()
{
//validate all fields
if (ChannelsList.SelectedItem == null)
{
ShowError("Please select a channel");
return false;
}
if (DatePicker.SelectedDate == null)
{
ShowError("Please select a date");
return false;
}
if (StartTimePicker.Value == null)
{
ShowError("Please select a start time");
return false;
}
if (DurationHours.Text == "")
{
ShowError("Please enter a duration");
return false;
}
if (DurationMinutes.Text == "")
{
ShowError("Please enter a duration" );
return false;
}
if (DurationSeconds.Text == "")
{
ShowError("Please enter a duration");
return false;
}
return true;
}
private void DownloadButton_Click(object sender, RoutedEventArgs e)
{
if (!Validate()) { return; }
DateTime pickedDate = DatePicker.SelectedDate.Value;
DateTime pickedTime = (DateTime)StartTimePicker.Value;
DateTime pickedDateTime = new DateTime(pickedDate.Year, pickedDate.Month, pickedDate.Day, pickedTime.Hour, pickedTime.Minute, pickedTime.Second);
//is uk in daylight savings? if so, subtrack 1 hour
if (TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time").IsDaylightSavingTime(pickedDateTime))
{
pickedDateTime = pickedDateTime.AddHours(-1);
}
//convert to str yyyy-MM-ddTHH:mm:ssZ
string pickedDateTimeStr = pickedDateTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
string channel = ChannelsList.SelectedItem.ToString();
int durationHrs = Convert.ToInt32(DurationHours.Text);
int durationMins = Convert.ToInt32(DurationMinutes.Text);
int durationSecs = Convert.ToInt32(DurationSeconds.Text);
//add to start time, converted to yyyy-MM-ddTHH:mm:ssZ
DateTime endTime = pickedDateTime.AddHours(durationHrs).AddMinutes(durationMins).AddSeconds(durationSecs);
string endTimeStr = endTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
bool encode = EncodeCheckbox.IsChecked.Value;
Downloader.StartDownload(pickedDateTimeStr, endTimeStr, channel, savePath, encode);
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}