Skip to content

Commit

Permalink
improved searching algorithm
Browse files Browse the repository at this point in the history
added one setting option
some ui stuff
  • Loading branch information
MisterX2000 committed Jul 26, 2016
1 parent 7bec81f commit 978cce6
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 40 deletions.
5 changes: 3 additions & 2 deletions gfycat Random/About.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:gfycat_Random"
mc:Ignorable="d"
Title="About" Width="200" ResizeMode="NoResize" Height="130" Topmost="True">
Title="About" Topmost="True" Icon="gfycat Random.ico" ResizeMode="NoResize" SizeToContent="WidthAndHeight">
<Grid>
<TextBlock x:Name="textBlock" Margin="10,0,10,10" TextWrapping="Wrap"><Hyperlink NavigateUri="http://misterx-hp.3server.de/"><Run Text="Homepage"/></Hyperlink><LineBreak/><Hyperlink NavigateUri="https://github.com/MisterX2000"><Run Text="Github Profile"/></Hyperlink><LineBreak/><Hyperlink NavigateUri="https://github.com/MisterX2000/gfycat-Random"><Run Text="Github Project"/></Hyperlink><LineBreak/><LineBreak/><Run Text="Made by Mister X"/></TextBlock>
<TextBlock x:Name="textBlock" Margin="10,5,10,10" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top"><Hyperlink NavigateUri="http://misterx-hp.3server.de/"><Run Text="Homepage"/></Hyperlink><LineBreak/><Hyperlink NavigateUri="https://github.com/MisterX2000"><Run Text="Github Profile"/></Hyperlink><LineBreak/><Hyperlink NavigateUri="https://github.com/MisterX2000/gfycat-Random"><Run Text="Github Project"/></Hyperlink><LineBreak/><LineBreak/><Run Text="Made b"/><Run Text="y Mis"/><Run Text="ter X"/></TextBlock>
<Label x:Name="l_version" Content="v" HorizontalAlignment="Left" Margin="120,0,5,5" VerticalAlignment="Bottom"/>
</Grid>
</Window>
3 changes: 3 additions & 0 deletions gfycat Random/About.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Windows;
using System.Diagnostics;
using System.Reflection;
using System.Windows.Navigation;

namespace gfycat_Random
Expand All @@ -12,6 +13,8 @@ public partial class About : Window
public About()
{
InitializeComponent();

l_version.Content = "v" + Assembly.GetExecutingAssembly().GetName().Version;
}

private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
Expand Down
9 changes: 5 additions & 4 deletions gfycat Random/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:gfycat_Random"
mc:Ignorable="d"
Title="gfycat Random" Height="400" Width="600" MinWidth="300" MinHeight="200">
Title="gfycat Random" Height="400" Width="600" MinWidth="300" MinHeight="200" Icon="gfycat Random.ico">
<Grid>
<MediaElement x:Name="mediaElement" Margin="0,51,0,0" Volume="0" IsMuted="True" MediaEnded="mediaElement_MediaEnded" LoadedBehavior="Manual" MediaFailed="mediaElement_MediaFailed" MediaOpened="mediaElement_MediaOpened"/>
<MediaElement x:Name="mediaElement" Margin="0,51,0,0" Volume="0" IsMuted="True" MediaEnded="mediaElement_MediaEnded" LoadedBehavior="Manual"/>
<Menu x:Name="menu" VerticalAlignment="Top" IsHitTestVisible="True">
<MenuItem Header="File">
<MenuItem x:Name="mi_save" Header="Save gfycat" Click="mi_save_Click" IsEnabled="False"/>
Expand All @@ -19,11 +19,12 @@
<MenuItem x:Name="mi_webm" Header="webm" IsCheckable="True" Checked="mi_webm_Checked"/>
<MenuItem x:Name="mi_gif" Header="gif" IsCheckable="True" Checked="mi_gif_Checked" ToolTip="WARNING! Very long loading times."/>
</MenuItem>
<MenuItem x:Name="mi_stopmedia" Header="Stop media while searching" IsCheckable="True" ToolTip="Stops the playback of the gfycat video while searching new ones." IsChecked="True"/>
</MenuItem>
<MenuItem x:Name="mi_about" Header="About" Click="mi_about_Click"/>
</Menu>
<Button x:Name="bt_random" Content="Random" HorizontalAlignment="Left" Margin="10,23,0,0" VerticalAlignment="Top" Click="bt_random_Click" Width="54" ToolTip="May takes some time before you find something."/>
<Label x:Name="l_link" Content="http://giant.gfycat.com/" HorizontalAlignment="Left" Margin="69,20,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.725,1.734"/>
<Button x:Name="bt_copylink" Content="Copy Link" Margin="0,23,10,0" VerticalAlignment="Top" HorizontalAlignment="Right" Click="bt_copylink_Click" Width="61"/>
<Label x:Name="l_link" Content="https://gfycat.com/" HorizontalAlignment="Left" Margin="69,20,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.725,1.734"/>
<Button x:Name="bt_copylink" Content="Copy Link" Margin="0,23,10,0" VerticalAlignment="Top" HorizontalAlignment="Right" Click="bt_copylink_Click" Width="61" IsEnabled="False"/>
</Grid>
</Window>
83 changes: 51 additions & 32 deletions gfycat Random/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Windows;
using Newtonsoft.Json.Linq;

namespace gfycat_Random
{
Expand All @@ -11,12 +13,18 @@ namespace gfycat_Random
/// </summary>
public partial class MainWindow : Window
{
private string format = "mp4";
private readonly string[] adj;
private readonly string[] ani;
private int fails;

public MainWindow()
{
InitializeComponent();
}

private string format = "mp4";
adj = File.ReadAllLines(@"adjectives.txt");
ani = File.ReadAllLines(@"animals.txt");
}

private void mediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
Expand All @@ -25,29 +33,50 @@ private void mediaElement_MediaEnded(object sender, RoutedEventArgs e)

private void bt_random_Click(object sender, RoutedEventArgs e)
{
DoStuff();
}
if (mi_stopmedia.IsChecked)
mediaElement.Close();

private void DoStuff()
{
mediaElement.Source = new Uri(GetRandomLink());
mediaElement.Play();
l_link.Content = mediaElement.Source.AbsoluteUri;
DoStuff();
}

private string GetRandomLink()
private async void DoStuff()
{
var adj = File.ReadAllLines(@"adjectives.txt");
var ani = File.ReadAllLines(@"animals.txt");
var r = new Random();

var word1 = UppercaseFirst(adj[r.Next(0, adj.Count() - 1)]);
var word2 = UppercaseFirst(adj[r.Next(0, adj.Count() - 1)]);
var word3 = UppercaseFirst(ani[r.Next(0, ani.Count() - 1)]);

var link = string.Format("http://giant.gfycat.com/{0}.{1} ", word1 + word2 + word3, format);
var wordcomb = word1 + word2 + word3;
//wordcomb = "NervousInsistentGroundbeetle";

return link;
using (var httpClient = new HttpClient())
{
var jstring = await httpClient.GetStringAsync(string.Format("https://gfycat.com/cajax/get/" + wordcomb));

var json = JObject.Parse(jstring);

if (json.Property("error") == null)
{
var url = json.Property("gfyItem").Value[format + "Url"].ToObject<string>();

mediaElement.Source = new Uri(url.Replace("https://", "http://"));
mediaElement.Play();

l_link.Content = url;
mi_save.IsEnabled = true;
bt_copylink.IsEnabled = true;
fails = 0;
}
else
{
fails++;
l_link.Content = $"Searching ({fails})";
mi_save.IsEnabled = false;
bt_copylink.IsEnabled = false;
DoStuff();
}
}
}

static string UppercaseFirst(string s)
Expand All @@ -67,37 +96,27 @@ private void mi_exit_Click(object sender, RoutedEventArgs e)
Close();
}

private void mediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
DoStuff();

mi_save.IsEnabled = false;
}

private void mi_save_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = mediaElement.Source.LocalPath.Substring(1);
dlg.DefaultExt = "." + format;
dlg.Filter = "Video|*.mp4;*.webm;*.gif";
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog
{
FileName = mediaElement.Source.LocalPath.Substring(1),
DefaultExt = "." + format,
Filter = "Video|*.mp4;*.webm;*.gif"
};

// Show save file dialog box
Nullable<bool> result = dlg.ShowDialog();
var result = dlg.ShowDialog();

// Process save file dialog box results
if (result == true)
{
// Save document
string filename = dlg.FileName;
var filename = dlg.FileName;
new WebClient().DownloadFile(mediaElement.Source.AbsoluteUri, filename);
}
}

private void mediaElement_MediaOpened(object sender, RoutedEventArgs e)
{
mi_save.IsEnabled = true;
}

private void mi_mp4_Checked(object sender, RoutedEventArgs e)
{
format = "mp4";
Expand Down
4 changes: 2 additions & 2 deletions gfycat Random/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
// übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
5 changes: 5 additions & 0 deletions gfycat Random/gfycat Random.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
<ApplicationIcon>gfycat Random.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
Expand Down Expand Up @@ -111,6 +115,7 @@
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
4 changes: 4 additions & 0 deletions gfycat Random/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452" />
</packages>

0 comments on commit 978cce6

Please sign in to comment.