Skip to content

Commit

Permalink
v2.1.5; bug fixes; update packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Decimation committed Jul 16, 2021
1 parent e030c6e commit 6e79141
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 155 deletions.
43 changes: 12 additions & 31 deletions SmartImage.Lib/SearchClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,30 +212,12 @@ public List<SearchResult> MaximizeResults<T>(Func<SearchResult, T> property)
public ImageResult FindDirectResult() => FindDirectResults().FirstOrDefault();


public ImageResult[] FindDirectResults(int lim = 5)
public ImageResult[] FindDirectResults()
{
int lim = 15;

var best = FindBestResults().ToList();

//const int FRAG_SIZE = 10;

//var frag = best.Chunk(FRAG_SIZE).ToList();

//var tasks = new List<Task>();

//for (int i = 0; i < frag.Count; i++) {
// int iCopy = i;

// tasks.Add(Task.Factory.StartNew(() =>
// {
// foreach (var result in frag[iCopy]) {
// result.FindDirectImages();
// }
// }));
//}


//Task.WaitAll(tasks.ToArray());

var cts = new CancellationTokenSource();

Expand All @@ -246,19 +228,19 @@ public ImageResult[] FindDirectResults(int lim = 5)
CancellationToken = cts.Token
};

Debug.WriteLine($"{best.Count}");

var rx = new ConcurrentBag<ImageResult>();

var images = new ConcurrentBag<ImageResult>();


Parallel.For(0, best.Count, options, i =>
{

if (options.CancellationToken.IsCancellationRequested || rx.Count >= lim) {
Debug.WriteLine("stop");

if (options.CancellationToken.IsCancellationRequested || images.Count >= lim) {
return;
}


var item = best[i];

item.FindDirectImages();
Expand All @@ -269,7 +251,7 @@ public ImageResult[] FindDirectResults(int lim = 5)

if (ImageHelper.IsDirect(item.Direct.ToString(), DirectImageType.Binary)) {
Debug.WriteLine($"Adding {item.Direct}");
rx.Add(item);
images.Add(item);
}

});
Expand All @@ -278,16 +260,15 @@ public ImageResult[] FindDirectResults(int lim = 5)
Task.Factory.StartNew(() =>
{
//SpinWait.SpinUntil(() => rx.Count >= lim);
while (!(rx.Count >= lim)) { }

Debug.WriteLine($"Cancel");
while (!(images.Count >= lim)) { }

cts.Cancel();
});


Debug.WriteLine($"Found {rx.Count} direct results");
Debug.WriteLine($"Found {images.Count} direct results");

return rx.OrderByDescending(r => r.Similarity)
return images.OrderByDescending(r => r.Similarity)
.ToArray();
}

Expand Down
10 changes: 5 additions & 5 deletions SmartImage.Lib/SmartImage.Lib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AngleSharp" Version="0.15.0" />
<PackageReference Include="AngleSharp.Css" Version="0.15.0" />
<PackageReference Include="AngleSharp.Io" Version="0.15.0" />
<PackageReference Include="AngleSharp.Js" Version="0.14.0" />
<PackageReference Include="AngleSharp" Version="0.16.0" />
<PackageReference Include="AngleSharp.Css" Version="0.16.0" />
<PackageReference Include="AngleSharp.Io" Version="0.16.0" />
<PackageReference Include="AngleSharp.Js" Version="0.15.0" />
<PackageReference Include="AngleSharp.XPath" Version="1.1.7" />
<PackageReference Include="JetBrains.Annotations" Version="2021.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="RestSharp" Version="106.11.7" />
<PackageReference Include="RestSharp" Version="106.12.0" />
<PackageReference Include="System.Drawing.Common" Version="5.0.2" />
<PackageReference Include="System.Json" Version="4.7.1" />
<PackageReference Include="System.Windows.Extensions" Version="5.0.0" />
Expand Down
8 changes: 1 addition & 7 deletions SmartImage.Lib/Utilities/ImageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,6 @@ public static Image GetImage(string s)
public static List<string> FindDirectImages(string url, DirectImageType directType = DirectImageType.Regex,
int count = 5, double pingTimeSec = 1)
{
/*
* TODO
*
* This function creates an insane memory leak.
* Disposing neither the images nor the streams does anything (?)
*/

var images = new List<string>();

Expand Down Expand Up @@ -209,6 +203,7 @@ public static List<string> FindDirectImages(string url, DirectImageType directTy
string str = standardOutput.ReadLine()
.Split('|')
.First();

if (!string.IsNullOrWhiteSpace(str)) {
images.Add(str);

Expand Down Expand Up @@ -335,7 +330,6 @@ public static List<string> FindDirectImages(string url, DirectImageType directTy

ret:


return images;

}
Expand Down
67 changes: 37 additions & 30 deletions SmartImage/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using System.Text.Unicode;
using System.Threading;
using System.Threading.Tasks;
using Windows.ApplicationModel.Background;
using Windows.UI.Notifications;
using Microsoft.Toolkit.Uwp.Notifications;
using Novus.Win32;
Expand All @@ -39,6 +40,7 @@
using SmartImage.Lib.Searching;
using SmartImage.Lib.Utilities;
using SmartImage.Utilities;
using SmartImage.UX;

// ReSharper disable CognitiveComplexity

Expand All @@ -65,7 +67,6 @@ public static class Program
Description = AppInterface.Description
};


#endregion

/// <summary>
Expand All @@ -80,20 +81,18 @@ private static async Task Main(string[] args)
}




#endif

/*
* Setup
* Check compatibility
*/


ToastNotificationManagerCompat.OnActivated += AppToast.OnActivated;

Native.SetConsoleOutputCP(Native.CP_IBM437);


Console.Title = $"{AppInfo.NAME}";

NConsole.Init();
Expand Down Expand Up @@ -125,37 +124,44 @@ private static async Task Main(string[] args)
* Handle CLI args
*/

var argEnumerator = args.GetEnumerator();
try {

var argEnumerator = args.GetEnumerator();

while (argEnumerator.MoveNext()) {
object? arg = argEnumerator.Current;

while (argEnumerator.MoveNext()) {
object? arg = argEnumerator.Current;
switch (arg) {
case CMD_FIND_DIRECT:
argEnumerator.MoveNext();

switch (arg) {
case CMD_FIND_DIRECT:
argEnumerator.MoveNext();
var directImages = ImageHelper.FindDirectImages((string) argEnumerator.Current);

var directImages = ImageHelper.FindDirectImages((string) argEnumerator.Current);

var imageResults = directImages.Select(ImageResult.FromDirectImage);
var directOptions = AppInterface.CreateResultOptions(imageResults, "Image");
var imageResults = directImages.Select(ImageResult.FromDirectImage);
var directOptions = AppInterface.CreateResultOptions(imageResults, "Image");


NConsole.ReadOptions(new NConsoleDialog
{
Options = directOptions,
Description = AppInterface.Description
});
NConsole.ReadOptions(new NConsoleDialog
{
Options = directOptions,
Description = AppInterface.Description
});

return;
case CMD_SEARCH:
argEnumerator.MoveNext();
Config.Query = (string) argEnumerator.Current;
break;
default:
Config.Query = args.First();
break;
return;
case CMD_SEARCH:
argEnumerator.MoveNext();
Config.Query = (string) argEnumerator.Current;
break;
default:
Config.Query = args.First();
break;
}
}
}
catch (Exception e) {
Console.WriteLine(e);
Console.ReadLine();
}
}

try {
Expand Down Expand Up @@ -197,6 +203,7 @@ private static async Task Main(string[] args)
}
}


private static void OnSearchCompleted(object? sender, EventArgs eventArgs, CancellationTokenSource cts)
{
AppInterface.FlashConsoleWindow();
Expand All @@ -205,7 +212,7 @@ private static void OnSearchCompleted(object? sender, EventArgs eventArgs, Cance
cts.Dispose();

if (Config.Notification) {
AppInterface.ShowToast();
AppToast.Show();
}
else {
SystemSounds.Exclamation.Play();
Expand Down
2 changes: 1 addition & 1 deletion SmartImage/SmartImage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.0.2" />
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="RestSharp" Version="106.11.7" />
<PackageReference Include="RestSharp" Version="106.12.0" />
<PackageReference Include="System.Drawing.Common" Version="5.0.2" />
<PackageReference Include="System.Json" Version="4.7.1" />
<PackageReference Include="System.Windows.Extensions" Version="5.0.0" />
Expand Down
81 changes: 3 additions & 78 deletions SmartImage/Core/AppInterface.cs → SmartImage/UX/AppInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using Windows.ApplicationModel.Background;
using Windows.Foundation;
using Windows.UI.Notifications;
using JetBrains.Annotations;
using Microsoft.Toolkit.Uwp.Notifications;
using Novus.Memory;
Expand Down Expand Up @@ -422,84 +425,6 @@ private static void UpdateConfig()
AppConfig.SaveConfigFile();
}

public static void ShowToast()
{
var button = new ToastButton();

button.SetContent("Open")
.AddArgument("action", "open");

var button2 = new ToastButton();

button2.SetContent("Dismiss")
.AddArgument("action", "dismiss");

var builder = new ToastContentBuilder();

var bestResult = Client.FindBestResult();

builder.AddButton(button)
.AddButton(button2)
.AddText("Search complete")
.AddText($"{bestResult}")
.AddText($"Results: {Client.Results.Count}");

if (Config.NotificationImage) {

var direct = Client.FindDirectResult();

Debug.WriteLine(direct);
Debug.WriteLine(direct.Direct.ToString());


if (direct is {Direct: { }}) {


var tmp = Path.GetTempPath();

string filename = Path.GetFileName(direct.Direct.AbsolutePath);

var file = Path.Combine(tmp, filename);

new WebClient().DownloadFile(direct.Direct, file);

//string file = WebUtilities.Download(abs.ToString(), s);

Debug.WriteLine($"Downloaded {file}");

builder.AddHeroImage(new Uri(file));

AppDomain.CurrentDomain.ProcessExit += (sender, args) =>
{
File.Delete(file);
};
}
}


ToastNotificationManagerCompat.OnActivated += compat =>
{
// Obtain the arguments from the notification
var args = ToastArguments.Parse(compat.Argument);

foreach (var argument in args) {
Debug.WriteLine($">>> {argument}");

if (argument.Key == "action" && argument.Value == "open") {
//Client.Results.Sort();


if (bestResult is {Url: { }}) {
WebUtilities.OpenUrl(bestResult.Url.ToString());
}
}
}
};

builder.Show();

//ToastNotificationManager.CreateToastNotifier();
}

#region Native

Expand Down
Loading

0 comments on commit 6e79141

Please sign in to comment.