Skip to content

Commit 8a3a91c

Browse files
committed
Temp
1 parent c858e10 commit 8a3a91c

File tree

6 files changed

+180
-60
lines changed

6 files changed

+180
-60
lines changed

SmartImage/Core/Interface.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using SmartImage.Engines;
1010
using SmartImage.Utilities;
1111
using Novus.Win32;
12+
1213
// ReSharper disable ArrangeAccessorOwnerBody
1314

1415
#pragma warning disable IDE0052, HAA0502, HAA0505, HAA0601, HAA0502, HAA0101, RCS1213, RCS1036, CS8602
@@ -47,14 +48,7 @@ private static NConsoleOption[] AllOptions
4748
/// <summary>
4849
/// Main menu console interface
4950
/// </summary>
50-
internal static NConsoleInterface MainMenu
51-
{
52-
get
53-
{
54-
//
55-
return new(AllOptions, Info.NAME_BANNER, null, false, null);
56-
}
57-
}
51+
internal static NConsoleInterface MainMenu => new(AllOptions, Info.NAME_BANNER, null, false, null);
5852

5953
/// <summary>
6054
/// Runs when no arguments are given (and when the executable is double-clicked)
@@ -90,13 +84,13 @@ internal static NConsoleInterface MainMenu
9084
/// </summary>
9185
internal static readonly Color ColorVersion = Color.LightGreen;
9286

93-
87+
9488
/// <summary>
9589
/// Console window width
9690
/// </summary>
9791
internal const int ConsoleWindowWidth = 120;
9892

99-
93+
10094
/// <summary>
10195
/// Console window height
10296
/// </summary>
@@ -250,7 +244,8 @@ internal static NConsoleInterface MainMenu
250244
}
251245
};
252246

253-
private static string GetContextMenuString(bool added) => (!added ? "Add" : "Remove") + " context menu integration";
247+
private static string GetContextMenuString(bool added) =>
248+
(!added ? "Add" : "Remove") + " context menu integration";
254249

255250

256251
private static readonly NConsoleOption CheckForUpdateOption = new()
@@ -351,6 +346,5 @@ internal static NConsoleInterface MainMenu
351346
}
352347
};
353348
#endif
354-
355349
}
356350
}

SmartImage/Engines/SauceNao/SauceNaoEngine.cs

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Drawing;
45
using System.Json;
56
using System.Linq;
67
using System.Runtime.Serialization.Json;
78
using System.Text;
9+
using System.Threading.Tasks;
810
using System.Xml;
911
using RestSharp;
1012
using SimpleCore.Utilities;
@@ -82,7 +84,7 @@ private static ISearchResult[] ConvertResults(SauceNaoResult[] results)
8284
var rg = new List<ISearchResult>();
8385

8486
foreach (var sn in results) {
85-
if (sn.Url !=null) {
87+
if (sn.Url != null) {
8688
var url = sn.Url.FirstOrDefault(u => u != null);
8789

8890
rg.Add(new SauceNaoSimpleResult(sn.WebsiteTitle, url, sn.Similarity));
@@ -94,56 +96,74 @@ private static ISearchResult[] ConvertResults(SauceNaoResult[] results)
9496

9597
public override FullSearchResult GetResult(string url)
9698
{
97-
FullSearchResult result=base.GetResult(url);
99+
FullSearchResult result = base.GetResult(url);
98100

99101
try {
100-
var sn = GetResults(url)
102+
var orig = GetResults(url);
103+
104+
if (orig == null) {
105+
result.ExtendedInfo.Add($"Timed out after {Timeout}");
106+
return result;
107+
}
108+
109+
var sn = orig
101110
.OrderByDescending(r => r.Similarity)
102111
.ToArray();
103112

104113
var extended = ConvertResults(sn);
105114

106115
var best = extended
107-
.Where(e=>e.Url!=null)
108-
.OrderByDescending(e=>e.Similarity)
116+
.Where(e => e.Url != null)
117+
.OrderByDescending(e => e.Similarity)
109118
.First();
110119

111-
result.Url = best.Url;
120+
result.Url = best.Url;
112121
result.Similarity = best.Similarity;
113-
result.Caption = best.Caption;
122+
result.Caption = best.Caption;
114123

115124

116125
result.AddExtendedResults(extended);
117126

118127
if (!string.IsNullOrWhiteSpace(m_apiKey)) {
119128
result.ExtendedInfo.Add("Using API");
120129
}
121-
130+
122131
}
123132
catch (Exception e) {
124-
133+
125134
result.ExtendedInfo.Add(e.StackTrace);
126135
}
127136

128137

129138
return result;
130139
}
131140

132-
141+
/// <summary>
142+
/// Timeout duration
143+
/// </summary>
144+
public static readonly TimeSpan Timeout = TimeSpan.FromSeconds(15);
145+
133146
private IEnumerable<SauceNaoResult>? GetResults(string url)
134147
{
135-
var req = new RestRequest();
136-
req.AddQueryParameter("db", "999");
137-
req.AddQueryParameter("output_type", "2");
138-
req.AddQueryParameter("numres", "16");
139-
req.AddQueryParameter("api_key", m_apiKey);
140-
req.AddQueryParameter("url", url);
141148

142-
var res = m_client.Execute(req);
149+
var task = Task.Run(() =>
150+
{
151+
var req = new RestRequest();
152+
req.AddQueryParameter("db", "999");
153+
req.AddQueryParameter("output_type", "2");
154+
req.AddQueryParameter("numres", "16");
155+
req.AddQueryParameter("api_key", m_apiKey);
156+
req.AddQueryParameter("url", url);
157+
158+
var res = m_client.Execute(req);
159+
160+
string c = res.Content;
143161

144-
string c = res.Content;
162+
return ReadResults(c);
163+
});
145164

146-
return ReadResults(c);
165+
// Handle possible timeouts
166+
return task.Wait(Timeout) ? task.Result : null;
147167
}
148168

149169

@@ -159,12 +179,12 @@ public override FullSearchResult GetResult(string url)
159179
var jsonArray = jsonObject["results"];
160180

161181
for (int i = 0; i < jsonArray.Count; i++) {
162-
var header = jsonArray[i]["header"];
163-
var data = jsonArray[i]["data"];
164-
string obj = header.ToString();
165-
obj = obj.Remove(obj.Length - 1);
166-
obj += data.ToString().Remove(0, 1).Insert(0, ",");
167-
jsonArray[i] = JsonValue.Parse(obj);
182+
var header = jsonArray[i]["header"];
183+
var data = jsonArray[i]["data"];
184+
string obj = header.ToString();
185+
obj = obj.Remove(obj.Length - 1);
186+
obj += data.ToString().Remove(0, 1).Insert(0, ",");
187+
jsonArray[i] = JsonValue.Parse(obj);
168188
}
169189

170190
string json = jsonArray.ToString();
@@ -174,7 +194,7 @@ public override FullSearchResult GetResult(string url)
174194
JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(json),
175195
XmlDictionaryReaderQuotas.Max);
176196
var serializer = new DataContractJsonSerializer(typeof(SauceNaoResponse));
177-
var result = serializer.ReadObject(stream) as SauceNaoResponse;
197+
var result = serializer.ReadObject(stream) as SauceNaoResponse;
178198
stream.Dispose();
179199

180200
if (result is null)

SmartImage/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using SmartImage.Searching;
66
using System;
77
using System.Text;
8+
using System.Threading;
89
using SimpleCore.Console.CommandLine;
910
using SimpleCore.Net;
1011
using SmartImage.Core;
@@ -35,7 +36,6 @@ public static class Program
3536

3637
private static void Main(string[] args)
3738
{
38-
3939
/*
4040
* Setup
4141
* Check compatibility

SmartImage/Searching/SearchClient.cs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#nullable enable
22
using System;
33
using System.Collections.Generic;
4+
using System.Diagnostics;
45
using System.Drawing;
56
using System.IO;
67
using System.Linq;
@@ -78,10 +79,8 @@ private SearchClient(string img)
7879
bool useImgur = !String.IsNullOrWhiteSpace(auth);
7980

8081
SearchConfig.Config.EnsureConfig();
81-
82-
var engines = SearchConfig.Config.SearchEngines;
8382

84-
83+
var engines = SearchConfig.Config.SearchEngines;
8584

8685
m_results = null!;
8786
m_engines = engines;
@@ -94,7 +93,6 @@ private SearchClient(string img)
9493

9594
m_imgUrl = imgUrl ?? throw new SmartImageException("Image upload failed");
9695

97-
9896
//
9997

10098
m_tasks = CreateSearchTasks();
@@ -110,37 +108,53 @@ private SearchClient(string img)
110108

111109
Complete = false;
112110

113-
114-
115111
Interface = new NConsoleInterface(Results)
116112
{
117113
SelectMultiple = false,
118114
Prompt = InterfacePrompt
119115
};
120116
}
121117

122-
118+
/// <summary>
119+
/// Monitors the search process
120+
/// </summary>
123121
private void RunSearchMonitor()
124122
{
125123
while (m_tasks.Any(t => !t.IsCompleted)) {
126-
var inProgress = m_tasks.Count(t => t.IsCompleted);
127-
var len = m_tasks.Length;
124+
int inProgress = m_tasks.Count(t => t.IsCompleted);
125+
int len = m_tasks.Length;
128126

129127
Interface.Status = $"Searching: {inProgress}/{len}";
130128
}
131129

132-
//Task.WaitAll(m_tasks);
133-
134-
var p = new SoundPlayer(Info.Resources.SndHint);
135-
p.Play();
130+
/*
131+
* Search is complete
132+
*/
136133

137134
Complete = true;
138135
Interface.Status = "Search complete";
139136
NConsoleIO.Refresh();
137+
138+
Debug.WriteLine("Search complete");
139+
140+
/*
141+
* Alert user
142+
*/
140143

144+
// Play sound
145+
146+
//var p = new SoundPlayer(Info.Resources.SndHint);
147+
//p.Play();
148+
149+
SystemSounds.Exclamation.Play();
150+
151+
// Flash taskbar icon
152+
NativeImports.FlashConsoleWindow();
141153

154+
NativeImports.BringToFront();
155+
142156
if (SearchConfig.Config.PriorityEngines == SearchEngineOptions.Auto) {
143-
157+
144158
// Results will already be sorted
145159
// Open best result
146160
var best = m_results[1];
@@ -209,8 +223,7 @@ private FullSearchResult GetOriginalImageResult()
209223
result.Width = width;
210224
result.Height = height;
211225

212-
213-
var mpx = MathHelper.ConvertToUnit(width * height, MetricUnit.Mega);
226+
double mpx = MathHelper.ConvertToUnit(width * height, MetricUnit.Mega);
214227

215228
string infoStr = $"Info: {m_img.Name} ({fileSizeMegabytes:F} MB) ({mpx:F} MP) ({fileFormat.Name})";
216229

@@ -249,8 +262,6 @@ private void RunSearchTask(ISearchEngine currentEngine)
249262
result.Function();
250263
}
251264

252-
//Update();
253-
254265
// Sort results
255266
m_results.Sort(CompareResults);
256267

SmartImage/SmartImage.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
32
<PropertyGroup>
43
<OutputType>Exe</OutputType>
54

@@ -8,9 +7,7 @@
87

98
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
109
<PublishSingleFile>true</PublishSingleFile>
11-
1210
<PublishTrimmed>true</PublishTrimmed>
13-
1411
<!--<PublishReadyToRun>true</PublishReadyToRun>-->
1512
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
1613
<ApplicationIcon>Icon.ico</ApplicationIcon>

0 commit comments

Comments
 (0)