Skip to content

Commit 9f0ffac

Browse files
committed
Image scanning; cli
1 parent a76e23a commit 9f0ffac

File tree

5 files changed

+52
-18
lines changed

5 files changed

+52
-18
lines changed

SmartImage.Lib/Searching/ImageResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public void FindDirectImages()
232232
}
233233
}
234234

235-
private void UpdateImageData()
235+
public void UpdateImageData()
236236
{
237237
if (Image is { }) {
238238

SmartImage.Lib/Utilities/ImageHelper.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using AngleSharp.Html.Parser;
1212
using SimpleCore.Net;
1313
using SimpleCore.Utilities;
14+
1415
// ReSharper disable UnusedParameter.Local
1516

1617
// ReSharper disable PossibleMultipleEnumeration
@@ -91,7 +92,6 @@ public static bool IsDirect(string url, DirectType directType = DirectType.Regex
9192
{
9293
DirectType.Binary => MediaTypes.IsDirect(url, MimeType.Image),
9394
DirectType.Regex =>
94-
// todo
9595
/*
9696
* https://github.com/PactInteractive/image-downloader
9797
*/
@@ -136,13 +136,6 @@ public static List<string> FindDirectImages(string url, out List<Image> images,
136136
// $"{fragmentSize} | {readImage} | " +
137137
// $"{imageFilter} | {count}");
138138

139-
140-
//<img.*?src="(.*?)"
141-
//href\s*=\s*"(.+?)"
142-
//var src = "<img.*?src=\"(.*?)\"";
143-
//var href = "href\\s*=\\s*\"(.+?)\"";
144-
145-
146139
IHtmlDocument document;
147140

148141
try {
@@ -165,6 +158,8 @@ public static List<string> FindDirectImages(string url, out List<Image> images,
165158
flat.AddRange(document.QuerySelectorAttributes("a", "href"));
166159
flat.AddRange(document.QuerySelectorAttributes("img", "src"));
167160

161+
flat = flat.Distinct().ToList();
162+
168163
var fragments = flat.Chunk(fragmentSize).ToArray();
169164

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

SmartImage/Core/MainDialog.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,18 @@ internal static class MainDialog
2727
{
2828
#region Colors
2929

30-
private static readonly Color ColorMain = Color.Yellow;
31-
private static readonly Color ColorOther = Color.Aquamarine;
32-
private static readonly Color ColorYes = Color.GreenYellow;
33-
private static readonly Color ColorNo = Color.Red;
30+
internal static readonly Color ColorMain = Color.Yellow;
31+
internal static readonly Color ColorOther = Color.Aquamarine;
32+
internal static readonly Color ColorYes = Color.GreenYellow;
33+
internal static readonly Color ColorNo = Color.Red;
3434

3535
#endregion
3636

3737
#region Elements
3838

39+
public const string Description = "Press the result number to open in browser\n" +
40+
"Ctrl: Load direct | Alt: Show other | Shift: Open raw | Alt+Ctrl: Download";
41+
3942
private static readonly string Enabled = StringConstants.CHECK_MARK.ToString().AddColor(ColorYes);
4043

4144
private static readonly string Disabled = StringConstants.MUL_SIGN.ToString().AddColor(ColorNo);

SmartImage/Core/NConsoleFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static NConsoleOption Create(SearchResult result)
8989
return option;
9090
}
9191

92-
private static NConsoleOption Create(ImageResult result, int i, Color c)
92+
public static NConsoleOption Create(ImageResult result, int i, Color c)
9393
{
9494

9595
const float correctionFactor = -.3f;

SmartImage/Program.cs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
using SmartImage.Lib.Utilities;
3131
using SmartImage.Utilities;
3232

33+
// ReSharper disable AssignNullToNotNullAttribute
34+
3335
// ReSharper disable ConvertSwitchStatementToSwitchExpression
3436

3537
// ReSharper disable UnusedParameter.Local
@@ -53,7 +55,7 @@ private static async Task Main(string[] args)
5355
{
5456
#if DEBUG
5557
if (!args.Any()) {
56-
//args = new[] {""};
58+
args = new[] {"find-direct", "https://danbooru.donmai.us/posts/3987008"};
5759
}
5860

5961

@@ -105,7 +107,42 @@ private static async Task Main(string[] args)
105107
object? arg = enumerator.Current;
106108

107109
switch (arg) {
110+
case "find-direct":
111+
enumerator.MoveNext();
112+
var argValue = (string) enumerator.Current;
113+
114+
var directImages = ImageHelper.FindDirectImages(argValue, out var im);
115+
var imageResults = new List<ImageResult>();
116+
117+
for (int i = 0; i < directImages.Count; i++) {
118+
string directUrl = directImages[i];
119+
120+
var ir = new ImageResult
121+
{
122+
Image = im[i],
123+
Url = new Uri(directUrl),
124+
Direct = new Uri(directUrl)
125+
};
126+
127+
ir.UpdateImageData();
128+
129+
imageResults.Add(ir);
130+
}
131+
132+
int i2 = 0;
133+
134+
var options = imageResults
135+
.Select(r => NConsoleFactory.Create(r, i2++, MainDialog.ColorOther))
136+
.ToArray();
137+
138+
139+
NConsole.ReadOptions(new NConsoleDialog
140+
{
141+
Options = options,
142+
Description = MainDialog.Description
143+
});
108144

145+
return;
109146
default:
110147
Config.Query = args[0];
111148
break;
@@ -179,9 +216,8 @@ private static void OnResultCompleted(object? sender, SearchResultEventArgs even
179216

180217
public static readonly NConsoleDialog ResultDialog = new()
181218
{
182-
Options = new List<NConsoleOption>(),
183-
Description = "Press the result number to open in browser\n" +
184-
"Ctrl: Load direct | Alt: Show other | Shift: Open raw | Alt+Ctrl: Download"
219+
Options = new List<NConsoleOption>(),
220+
Description = MainDialog.Description
185221
};
186222

187223
public static readonly SearchConfig Config = new();

0 commit comments

Comments
 (0)