diff --git a/WeatubeDL/MainWindow.xaml b/WeatubeDL/MainWindow.xaml index 6eae8b9..ffcbc50 100644 --- a/WeatubeDL/MainWindow.xaml +++ b/WeatubeDL/MainWindow.xaml @@ -5,8 +5,22 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WeatubeDL" mc:Ignorable="d" - Title="MainWindow" Height="450" Width="800"> + Title="Weatube DL" Height="450" Width="800"> - + + + + + + + + + + + + + diff --git a/WeatubeDL/MainWindow.xaml.cs b/WeatubeDL/MainWindow.xaml.cs index 88871a4..eb1b4ce 100644 --- a/WeatubeDL/MainWindow.xaml.cs +++ b/WeatubeDL/MainWindow.xaml.cs @@ -12,17 +12,51 @@ using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; +using System.Net; namespace WeatubeDL { - /// - /// Interaction logic for MainWindow.xaml - /// public partial class MainWindow : Window { + private YoutubeVideo[] videos; + private YoutubeVideo current; public MainWindow() { InitializeComponent(); } + + private async void Link_TextChanged(object sender, TextChangedEventArgs e) + { + async Task UserKeepsTyping() + { + string txt = (sender as TextBox).Text; + await Task.Delay(500); + return txt != (sender as TextBox).Text; + } + if (await UserKeepsTyping()) return; + if ((sender as TextBox).Text.Length < 1) + { + current = null; + return; + } + var text = (sender as TextBox).Text; + var vid = new YoutubeVideo(text); + if (await vid.Exists()) + { + current = vid; + while (current.name == null) + { + await Task.Delay(500); + } + AddLink.IsEnabled = true; + QueueItem.Text = current.name; + Uri uri = new Uri(current.imagePath, UriKind.RelativeOrAbsolute); + QueueImage.Source = BitmapFrame.Create(uri); + } + else + { + AddLink.IsEnabled = false; + } + } } } diff --git a/WeatubeDL/Properties/YoutubeVideo.cs b/WeatubeDL/Properties/YoutubeVideo.cs new file mode 100644 index 0000000..db6dc0c --- /dev/null +++ b/WeatubeDL/Properties/YoutubeVideo.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net; +using System.IO; +using System.ComponentModel; +using System.Drawing; +using System.Text.RegularExpressions; + +namespace WeatubeDL +{ + class YoutubeVideo + { + public string src { get; private set; } + public string id { get; private set; } + public string[] types { get; private set; } + public string imagePath { get; private set; } + public string name { get; private set; } + private WebClient client; + + public YoutubeVideo(string src) + { + this.src = src; + DownloadPreview(); + } + + ~YoutubeVideo() + { + if (File.Exists(imagePath)) File.Delete(imagePath); + if (client != null) client.Dispose(); + } + + private void IdFromUrl() + { + if (src.Contains("youtu.be/") && src.Contains("&")) + this.id = src.Replace("/youtu.be/", "").Split('/')[1].Split('&')[0]; + else if (src.Contains("youtube.com/watch?v=") && src.Contains("&")) + this.id = src.Split('=')[1].Split('&')[0]; + } + + private void DownloadPreview() + { + this.IdFromUrl(); + if (id == null) return; + client = new WebClient(); + client.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)"); + client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); + imagePath = Path.GetTempPath() + this.id + ".png"; + client.DownloadFileAsync(new Uri("http://img.youtube.com/vi/" + this.id + "/mqdefault.jpg"), imagePath); + } + + static void Completed(object sender, AsyncCompletedEventArgs e) + { + (sender as WebClient).Dispose(); + } + public async Task Exists() + { + if (id == null) return false; + while(client.IsBusy) + { + await Task.Delay(100); + } + if (imagePath.Length > 1 && File.Exists(imagePath)) + { + Bitmap img = new Bitmap(imagePath); + if (img.Width == 120) + { + img.Dispose(); + File.Delete(imagePath); + return false; + } + img.Dispose(); + this.GetVideoName(); + return true; + } + return false; + } + private void GetVideoName() + { + client = new WebClient(); + client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted); + client.DownloadStringAsync(new Uri(src)); + } + + void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) + { + this.name = Regex.Match(e.Result, @"\]*\>\s*(?[\s\S]*?)\</title\>", + RegexOptions.IgnoreCase).Groups["Title"].Value.Replace(" - YouTube", ""); + (sender as WebClient).Dispose(); + } + + } +} diff --git a/WeatubeDL/WeatubeDL.csproj b/WeatubeDL/WeatubeDL.csproj index e3e5a45..b2cdf79 100644 --- a/WeatubeDL/WeatubeDL.csproj +++ b/WeatubeDL/WeatubeDL.csproj @@ -37,6 +37,7 @@ <ItemGroup> <Reference Include="System" /> <Reference Include="System.Data" /> + <Reference Include="System.Drawing" /> <Reference Include="System.Xml" /> <Reference Include="Microsoft.CSharp" /> <Reference Include="System.Core" /> @@ -72,6 +73,7 @@ <Compile Include="Properties\AssemblyInfo.cs"> <SubType>Code</SubType> </Compile> + <Compile Include="Properties\YoutubeVideo.cs" /> <Compile Include="Properties\Resources.Designer.cs"> <AutoGen>True</AutoGen> <DesignTime>True</DesignTime>