diff --git a/WeatubeDL/MainWindow.xaml b/WeatubeDL/MainWindow.xaml index ffcbc50..424dc70 100644 --- a/WeatubeDL/MainWindow.xaml +++ b/WeatubeDL/MainWindow.xaml @@ -5,22 +5,30 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WeatubeDL" mc:Ignorable="d" - Title="Weatube DL" Height="450" Width="800"> - + Title="Weatube DL" Height="450" Width="810"> + + + + + + + + + - - - + + + + - - - - + + + - - + diff --git a/WeatubeDL/MainWindow.xaml.cs b/WeatubeDL/MainWindow.xaml.cs index bad2a20..7f749e2 100644 --- a/WeatubeDL/MainWindow.xaml.cs +++ b/WeatubeDL/MainWindow.xaml.cs @@ -14,6 +14,7 @@ using System.Windows.Navigation; using System.Windows.Shapes; using System.Net; using System.IO; +using System.Windows.Media.Animation; namespace WeatubeDL { @@ -37,26 +38,48 @@ namespace WeatubeDL if (await UserKeepsTyping()) return; if ((sender as TextBox).Text.Length < 1) { + DisableSuggestion(); current = null; return; } var text = (sender as TextBox).Text; + if (current != null && current.id.Contains(YoutubeVideo.IdFromUrl(text))) + { + EnableSuggestion(); + return; + } 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; - QueueImage.Source = current.image; + while (current.name == null) await Task.Delay(500); + EnableSuggestion(); } - else + else DisableSuggestion(); + } + + public void EnableSuggestion() + { + AddLink.IsEnabled = true; + if (SuggestedVideo.Margin.Top < 0) { - AddLink.IsEnabled = false; + Storyboard sb = Resources["ShowSuggested"] as Storyboard; + sb.Begin(SuggestedVideo); } + SuggestedItem.Text = current.name; + SuggestedImage.Source = current.image; + } + + public void DisableSuggestion() + { + AddLink.IsEnabled = false; + if (SuggestedVideo.Margin.Top == 0) + { + Storyboard sb = Resources["HideSuggested"] as Storyboard; + sb.Begin(SuggestedVideo); + } + SuggestedItem.Text = null; + SuggestedImage.Source = null; } protected override void OnClosed(EventArgs e) { diff --git a/WeatubeDL/Properties/YoutubeVideo.cs b/WeatubeDL/Properties/YoutubeVideo.cs index 3beadfd..cc9b3f2 100644 --- a/WeatubeDL/Properties/YoutubeVideo.cs +++ b/WeatubeDL/Properties/YoutubeVideo.cs @@ -34,24 +34,25 @@ namespace WeatubeDL if (client != null) client.Dispose(); } - private void IdFromUrl() + public static string IdFromUrl(string src) { if (src.Contains("youtu.be/") && src.Contains("&")) - this.id = src.Replace("/youtu.be/", "").Split('/')[1].Split('&')[0]; + return 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]; + return src.Split('=')[1].Split('&')[0]; + return " "; } private void DownloadPreview() { - this.IdFromUrl(); - if (id == null) return; + id = IdFromUrl(src); + if (id == null || id == " ") return; client = new WebClient(); client.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0)"); client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); - imagePath = Path.GetTempPath() + "WeatubeDL/" + this.id + ".png"; + imagePath = Path.GetTempPath() + "WeatubeDL/" + id + ".png"; Directory.CreateDirectory(Path.GetTempPath() + "WeatubeDL"); - client.DownloadFileAsync(new Uri("http://img.youtube.com/vi/" + this.id + "/mqdefault.jpg"), imagePath); + client.DownloadFileAsync(new Uri("http://img.youtube.com/vi/" + id + "/mqdefault.jpg"), imagePath); } static void Completed(object sender, AsyncCompletedEventArgs e) @@ -60,12 +61,12 @@ namespace WeatubeDL } public async Task Exists() { - if (id == null) return false; + if (id == null || client == null) return false; while(client.IsBusy) { await Task.Delay(100); } - if (id.Length > 1 && File.Exists(imagePath)) + if (id.Length > 2 && File.Exists(imagePath) && new FileInfo(imagePath).Length > 0) { image = new BitmapImage(); @@ -82,7 +83,7 @@ namespace WeatubeDL File.Delete(imagePath); return false; } - this.GetVideoName(); + GetVideoName(); return true; } return false; @@ -96,7 +97,7 @@ namespace WeatubeDL void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { - this.name = Regex.Match(e.Result, @"\]*\>\s*(?[\s\S]*?)\</title\>", + name = Regex.Match(e.Result, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value.Replace(" - YouTube", ""); (sender as WebClient).Dispose(); }