-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
357 lines (296 loc) · 8.23 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
using Microsoft.Win32;
using Syroot.Windows.IO;
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Media;
using WPFLocalizeExtension.Engine;
using YoutubeDLSharp;
using YoutubeDLSharp.Metadata;
using YoutubeDLSharp.Options;
namespace GetTube;
public partial class MainWindow : Window
{
private string? SelectedTheme;
private string? SelectedLanguage;
private readonly string? DownloadsFolder;
private YoutubeDL? ytClient;
private VideoData ytData;
private OptionSet ytOpts;
private Progress<DownloadProgress>? ytProg;
private CancellationTokenSource? ytCancel;
private bool ytCanCancel;
public MainWindow()
{
InitializeComponent();
// deal with configuration
ReadConfig();
// modify UI according to config
SetUILang(SelectedLanguage);
SetUITheme(SelectedTheme);
// set the downloads folder
DownloadsFolder = new KnownFolder(KnownFolderType.Downloads).Path;
// setup downloading with youtubedlsharp
SetupDownloader();
}
// Setting downloading lib
private void SetupDownloader()
{
// make downloader client
ytClient = new();
// set downloader paths
ytClient.YoutubeDLPath = Path.Join("youtube-dl.exe");
ytClient.FFmpegPath = Path.Join("ffmpeg.exe");
ytClient.OutputFolder = DownloadsFolder;
// set cancellation
ytCancel = new CancellationTokenSource();
// initialize options
ytOpts = new OptionSet();
//ytOpts.EmbedThumbnail = true;
// set downloader prog element
ytProg = new(
p => uiStatus.Content =
Properties.Resources.Downloading + " " +
Utils.FloatToHundred(p.Progress));
}
// Configuration management
private void ReadConfig()
{
RegistryKey? key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\GetTube");
if (key == null)
{
CreateConfig();
ReadConfig();
return;
}
SelectedLanguage = key.GetValue("Language").ToString();
SelectedTheme = key.GetValue("Theme").ToString();
key.Close();
}
private void CreateConfig()
{
RegistryKey newKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\GetTube", true);
newKey.SetValue("Theme", "Light");
newKey.SetValue("Language", "en-US");
newKey.Close();
}
private void UpdateConfig()
{
RegistryKey? key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\GetTube", true);
key.SetValue("Language", SelectedLanguage);
key.SetValue("Theme", SelectedTheme);
key.Close();
}
// Fetching Info And Downloading
private async void EventGetVideo(object sender, RoutedEventArgs e)
{
// delete the info of the previous video
ClearUIInfo();
// say fetching and fetch info of new video
uiStatus.Content = Properties.Resources.Fetching;
var currentRes = await ytClient?.RunVideoDataFetch(uiURLBox.Text);
// check for no info found
if (!currentRes.Success)
{
uiStatus.Content = Properties.Resources.NotFound;
return;
}
// save fetch result
ytData = currentRes.Data;
// say info was found and set on ui
uiStatus.Content = Properties.Resources.Found;
ResetUIInfo();
// make buttons clickable
uiVideoBtn.Click += DownloadVideo;
uiAudioBtn.Click += DownloadAudio;
// make section appear
uiVidBox.Opacity = 0.9;
uiStatus.Content = Properties.Resources.WaitingFormat;
}
private async void DownloadAudio(object sender, RoutedEventArgs e)
{
// cancel previous download
if (ytCanCancel)
ytCancel.Cancel();
// able to cancel task
ytCanCancel = true;
// download while reporting progress
var result = await ytClient.RunAudioDownload(
ytData.WebpageUrl,
AudioConversionFormat.Mp3,
progress: ytProg,
overrideOptions: ytOpts,
ct: ytCancel.Token);
// report finishing
if (result.Success)
uiStatus.Content = Properties.Resources.DownloadedAudio;
else
uiStatus.Content = Properties.Resources.NotDownloadedAudio;
// can't cancel task
ytCanCancel = false;
}
private async void DownloadVideo(object sender, RoutedEventArgs e)
{
// cancel previous download
if (ytCanCancel)
ytCancel.Cancel();
// able to cancel task
ytCanCancel = true;
// download while reporting progress
var result = await ytClient.RunVideoDownload(
ytData.WebpageUrl,
progress: ytProg,
overrideOptions: ytOpts,
ct: ytCancel.Token);
// report finishing
if (result.Success)
uiStatus.Content = Properties.Resources.DownloadedVideo;
else
uiStatus.Content = Properties.Resources.NotDownloadedVideo;
// can't cancel task
ytCanCancel = false;
}
// Bottom bar events
private void EventLang(object sender, RoutedEventArgs e)
{
if (SelectedLanguage == "en-US")
SetUILang("ar-IQ");
else
SetUILang("en-US");
}
private void EventColor(object sender, RoutedEventArgs e)
{
if (SelectedTheme == "Dark")
SetUITheme("Light");
else
SetUITheme("Dark");
}
private void EventFlaticon(object sender, RoutedEventArgs e)
{
uiSecStatus.Content = Properties.Resources.Flaticon;
}
private void EventSource(object sender, RoutedEventArgs e)
{
uiSecStatus.Content = Properties.Resources.Github;
Process.Start(new ProcessStartInfo("cmd", $"/c start {"https://github.com/ahmadkabdullah/GetTube"}") { CreateNoWindow = true });
}
// Changing the UI
private void SetUILang(string language)
{
// change fonts and notify in secondary status
if (language == "ar-IQ")
{
uiVidAuthor.FontFamily =
uiVidDuration.FontFamily =
uiVidTitle.FontFamily =
uiVideoBtn.FontFamily =
uiAudioBtn.FontFamily =
uiSecStatus.FontFamily =
uiStatus.FontFamily = new FontFamily("NRT BOLD");
SelectedLanguage = "ar-IQ";
}
else
{
uiVidAuthor.FontFamily =
uiVidDuration.FontFamily =
uiVidTitle.FontFamily =
uiVideoBtn.FontFamily =
uiAudioBtn.FontFamily =
uiSecStatus.FontFamily =
uiStatus.FontFamily = new FontFamily("Fira Sans");
SelectedLanguage = "en-US";
}
// switch to set language
LocalizeDictionary.Instance.Culture = new CultureInfo(SelectedLanguage);
// say language was changed
uiSecStatus.Content = Properties.Resources.ChangedLanguage;
// update configuration
UpdateConfig();
// reset the ui info as it was cleared by new culture
ResetUIInfo();
}
private void SetUITheme(string theme)
{
// used colors
Brush bgCol, fgCol, fgColB;
if (theme == "Light")
{
// light theme colors
bgCol = Brushes.WhiteSmoke;
fgCol = new SolidColorBrush(Color.FromArgb(0xFF, 20, 20, 20));
fgColB = Brushes.Gray;
SelectedTheme = "Light";
uiSecStatus.Content = Properties.Resources.ToLight;
}
else
{
// dark theme colors
bgCol = new SolidColorBrush(Color.FromArgb(0xFF, 30, 30, 30));
fgCol = Brushes.WhiteSmoke;
fgColB = Brushes.Gray;
SelectedTheme = "Dark";
uiSecStatus.Content = Properties.Resources.ToDark;
}
// finally set all the colors
uiContainer.Background = bgCol;
uiURLBox.Background = bgCol;
uiURLBox.Foreground = fgCol;
uiStatus.Foreground = fgCol;
uiSecStatus.Foreground = fgColB;
// update configuration
UpdateConfig();
}
private void ClearUIInfo()
{
// reset elements after new link is given
uiVidBox.Opacity = 0.2;
uiVidTitle.Text = Properties.Resources.VidTitle;
uiVidAuthor.Text = Properties.Resources.VidAuthor;
uiVidDuration.Text = Properties.Resources.VidDuration;
// make buttons not clickable
uiVideoBtn.Click -= DownloadVideo;
uiAudioBtn.Click -= DownloadAudio;
}
private void ResetUIInfo()
{
if (ytData == null)
return;
uiVidTitle.Text = ytData.Title;
uiVidAuthor.Text = ytData.Uploader;
uiVidDuration.Text = Utils.SecToTime(ytData.Duration);
}
}
internal class Utils
{
public static string RemoveSpecialCharacters(string input)
{
string output = input;
var charsToRemove = new string[] { ",", "'", "\"" };
foreach (var c in charsToRemove)
output = output.Replace(c, string.Empty);
return output;
}
public static string FloatToHundred(float num)
{
var str = string.Format("{0:N5}", num);
if (str.Length <= 1)
return str;
else
return String.Format("{0}{1}{2}", str[0], str[2], str[3]);
}
public static string SecToTime(float? secondsFloat)
{
if (secondsFloat == null)
return "0";
var seconds = (int)secondsFloat;
var minutes = seconds / 60;
var pOne = seconds % 60;
var hours = minutes / 60;
var pTwo = minutes % 60;
var pThree = hours;
return String.Format("{0}:{1}:{2}", pThree, pTwo, pOne);
}
}