-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
141 lines (131 loc) · 5.03 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
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Net;
using System.Configuration;
using System.Collections;
using Microsoft.Win32;
using VideoLibrary;
// https://github.com/mrklintscher/YouTubeSearch/blob/master/ExampleApplication/Program.cs
namespace MyYoutubeDownloader
{
public partial class MainWindow : Window
{
private YouTube youtubeService;
private string DownloadFolder = Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "{374DE290-123F-4565-9164-39C4925E467B}", String.Empty).ToString() + "\\";
private VideoClient client;
private string defaultFormat = ConfigurationSettings.AppSettings["default_format"];
private string defaultQuality = ConfigurationSettings.AppSettings["default_quality"];
public MainWindow()
{
InitializeComponent();
youtubeService = YouTube.Default;
string disp = "";
foreach (ListBoxItem elem in resolutionBox.Items)
{
if (elem.Content.ToString() == defaultQuality)
{
resolutionBox.SelectedItem = elem;
break;
}
}
resolutionBox.SelectedItem = defaultQuality;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var url = VideoUrl.Text;
switch(CheckURL(url))
{
case HttpStatusCode.OK:
DownloadVideo(url);
break;
case HttpStatusCode.ServiceUnavailable:
DisplayError("erreur");
break;
}
}
private void DisplayError(string msg)
{
MessageBox.Show(msg);
}
private HttpStatusCode CheckURL(string url)
{
try
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "HEAD";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
response.Close();
return (response.StatusCode);
}
catch
{
return (HttpStatusCode)1;
}
}
private void DownloadVideo(string url)
{
try
{
var audio = getAudio(url);
var video = getVideo(url);
File.WriteAllBytes(DownloadFolder + "audio_ " + audio.FullName, audio.GetBytes());
File.WriteAllBytes(DownloadFolder + "video_ " + video.FullName, video.GetBytes());
MessageBox.Show("FINISH : " + DownloadFolder + audio.FullName);
}
catch (Exception exception)
{
MessageBox.Show("An error occured : " + exception.ToString());
}
}
private void basicDownload(string url)
{
var video = youtubeService.GetVideo(url);
File.WriteAllBytes(DownloadFolder + video.FullName, video.GetBytes());
}
private YouTubeVideo getVideo(string url)
{
var videos = youtubeService.GetAllVideos(url);
ListBoxItem selectedRes = (ListBoxItem)resolutionBox.SelectedItem;
string resolution = selectedRes.Content.ToString();
bool highest = resolution == "highest" ? true : false;
YouTubeVideo selectVideo = null;
YouTubeVideo highestVideo = null;
foreach (var v in videos)
{
string currentFormat = v.Format.ToString().ToLower();
if (currentFormat == defaultFormat && highestVideo == null)
highestVideo = v;
else if (currentFormat == defaultFormat && highestVideo.Resolution < v.Resolution)
{
highestVideo = v;
if (highest)
selectVideo = highestVideo;
}
if (currentFormat == defaultFormat && v.Resolution.ToString() == resolution)
selectVideo = v;
}
if (selectVideo == null)
{
MessageBox.Show("No video found with the selected resolution. Use resolution " + highestVideo.Resolution.ToString() + " instead.");
selectVideo = highestVideo;
}
return (selectVideo);
}
private YouTubeVideo getAudio(string url)
{
var videoInfos = youtubeService.GetAllVideos(url);
YouTubeVideo toDownload = null;
foreach (var video in videoInfos)
{
if (video.AdaptiveKind == AdaptiveKind.Audio)
{
toDownload = video;
break;
}
}
return toDownload;
}
}
}