video discovery testing

This commit is contained in:
2021-07-12 13:29:19 +03:00
parent a495fd5975
commit a90602c27a
4 changed files with 150 additions and 5 deletions
+15 -1
View File
@@ -5,8 +5,22 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WeatubeDL" xmlns:local="clr-namespace:WeatubeDL"
mc:Ignorable="d" mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"> Title="Weatube DL" Height="450" Width="800">
<Grid> <Grid>
<StackPanel x:Name="MainPanel">
<DockPanel Height="30" Margin="0, 20, 0,0">
<TextBox x:Name="Link" Width="600" TextChanged="Link_TextChanged"></TextBox>
<ComboBox Name="VideoType" Width="100">
</ComboBox>
<Button x:Name="AddLink" Width="100" HorizontalAlignment="Right" IsEnabled="False">
<TextBlock>Add to queue</TextBlock>
</Button>
</DockPanel>
<StackPanel Orientation="Horizontal" x:Name="Queue" Margin="0, 20, 0,0" Width="800" Height="50">
<Image Stretch="Fill" Name="QueueImage" Height="50" Width="88" HorizontalAlignment="Left" Margin="0, 0, 10, 0"/>
<TextBlock x:Name="QueueItem"></TextBlock>
</StackPanel>
</StackPanel>
</Grid> </Grid>
</Window> </Window>
+37 -3
View File
@@ -12,17 +12,51 @@ using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Windows.Navigation; using System.Windows.Navigation;
using System.Windows.Shapes; using System.Windows.Shapes;
using System.Net;
namespace WeatubeDL namespace WeatubeDL
{ {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
private YoutubeVideo[] videos;
private YoutubeVideo current;
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
} }
private async void Link_TextChanged(object sender, TextChangedEventArgs e)
{
async Task<bool> 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;
}
}
} }
} }
+95
View File
@@ -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<bool> 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, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>",
RegexOptions.IgnoreCase).Groups["Title"].Value.Replace(" - YouTube", "");
(sender as WebClient).Dispose();
}
}
}
+2
View File
@@ -37,6 +37,7 @@
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
@@ -72,6 +73,7 @@
<Compile Include="Properties\AssemblyInfo.cs"> <Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="Properties\YoutubeVideo.cs" />
<Compile Include="Properties\Resources.Designer.cs"> <Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen> <AutoGen>True</AutoGen>
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>