Skip to content

Commit e3f4bce

Browse files
committed
Integrate gallery-dl
1 parent b14eb2d commit e3f4bce

File tree

4 files changed

+76
-43
lines changed

4 files changed

+76
-43
lines changed

SmartImage.Lib/Utilities/ImageHelper.cs

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44
using System.Drawing;
55
using System.IO;
66
using System.Linq;
7+
using System.Net;
78
using System.Text.RegularExpressions;
89
using System.Threading;
910
using System.Threading.Tasks;
1011
using AngleSharp.Html.Dom;
1112
using AngleSharp.Html.Parser;
13+
using Novus.Win32;
1214
using SimpleCore.Net;
1315
using SimpleCore.Utilities;
1416

17+
// ReSharper disable CognitiveComplexity
18+
// ReSharper disable PossibleNullReferenceException
19+
1520
// ReSharper disable UnusedParameter.Local
1621

1722
// ReSharper disable PossibleMultipleEnumeration
@@ -109,6 +114,36 @@ public static bool IsDirect(string url, DirectImageType directType = DirectImage
109114

110115
}
111116

117+
public static Dictionary<string,string> InstalledUtilities
118+
{
119+
get
120+
{
121+
var utils = new List<string>
122+
{
123+
FFMPEG_EXE, FFPROBE_EXE, MAGICK_EXE, YOUTUBE_DL_EXE, GALLERY_DL_EXE
124+
};
125+
126+
var rg = new Dictionary<string, string>();
127+
128+
foreach (string s in utils) {
129+
var x = FileSystem.SearchInPath(s);
130+
131+
132+
rg.Add(s,x);
133+
}
134+
135+
return rg;
136+
137+
}
138+
}
139+
140+
141+
private const string MAGICK_EXE = "magick.exe";
142+
private const string GALLERY_DL_EXE = "gallery-dl.exe";
143+
private const string YOUTUBE_DL_EXE = "youtube-dl.exe";
144+
private const string FFPROBE_EXE = "ffprobe.exe";
145+
private const string FFMPEG_EXE = "ffmpeg.exe";
146+
112147

113148
/// <summary>
114149
/// Scans for direct images within a webpage.
@@ -124,14 +159,45 @@ public static List<DirectImage> FindDirectImages(string url, DirectImageType dir
124159
int count = 5, int fragmentSize = 10, double pingTimeSec = 1,
125160
bool readImage = true, Predicate<Image> imageFilter = null)
126161
{
162+
var directImages = new List<DirectImage>();
127163

164+
string gallerydl = InstalledUtilities[GALLERY_DL_EXE];
128165

129-
imageFilter ??= (x) => true;
166+
if (gallerydl != null) {
130167

131-
var pingTime = TimeSpan.FromSeconds(pingTimeSec);
168+
Trace.WriteLine($"Using gallery-dl!");
169+
170+
var output = new Process
171+
{
172+
StartInfo = new ProcessStartInfo
173+
{
174+
FileName = gallerydl,
175+
Arguments = $"-G {url}",
176+
RedirectStandardOutput = true,
177+
CreateNoWindow = true
178+
}
179+
};
132180

181+
output.Start();
133182

134-
var directImages = new List<DirectImage>();
183+
var standardOutput = output.StandardOutput;
184+
185+
while (!standardOutput.EndOfStream) {
186+
string str = standardOutput.ReadLine().Trim().Trim('|');
187+
188+
directImages.Add(new DirectImage
189+
{
190+
Direct = new Uri(str),
191+
Image = Image.FromStream(WebUtilities.GetStream(str))
192+
});
193+
}
194+
195+
return directImages;
196+
}
197+
198+
imageFilter ??= (x) => true;
199+
200+
var pingTime = TimeSpan.FromSeconds(pingTimeSec);
135201

136202

137203
IHtmlDocument document;
@@ -174,7 +240,7 @@ public static List<DirectImage> FindDirectImages(string url, DirectImageType dir
174240
{
175241

176242
foreach (string currentUrl in fragments[iCopy]) {
177-
243+
178244
if (directImages.Count >= count) {
179245
return;
180246
}

SmartImage/Core/AppInfo.cs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -92,35 +92,7 @@ public static string ExeLocation
9292
}
9393
}
9494

95-
public static List<string> InstalledUtilities
96-
{
97-
get
98-
{
99-
var utils = new List<string>
100-
{
101-
"ffmpeg.exe", "ffprobe.exe", "magick.exe", "youtube-dl.exe"
102-
};
103-
104-
var rg = new List<string>();
105-
106-
foreach (string s in utils) {
107-
string[] path = Environment.GetEnvironmentVariable("PATH").Split(';');
108-
109-
foreach (string directory in path) {
110-
if (Directory.Exists(directory)) {
111-
foreach (string file in Directory.EnumerateFiles(directory)) {
112-
if (Path.GetFileName(file) == s) {
113-
rg.Add(file);
114-
}
115-
}
116-
}
117-
}
118-
}
119-
120-
return rg;
121-
122-
}
123-
}
95+
12496

12597

12698
public static bool IsAppFolderInPath => FileSystem.IsFolderInPath(AppFolder);

SmartImage/Program.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,30 +73,25 @@ private static async Task Main(string[] args)
7373
#if TEST_DEBUG
7474
if (!args.Any()) {
7575
//args = new string[] {CMD_SEARCH, "https://i.imgur.com/QtCausw.png"};
76-
76+
args = new[] {CMD_FIND_DIRECT, "https://twitter.com/sciamano240/status/1186775807655587841"};
7777
}
7878

7979

8080
#endif
81-
82-
8381
/*
8482
* Setup
8583
* Check compatibility
8684
*/
8785

88-
8986
Native.SetConsoleOutputCP(Native.CP_WIN32_UNITED_STATES);
9087

91-
9288
Console.Title = $"{AppInfo.NAME}";
9389

9490
NConsole.Init();
9591
Console.Clear();
9692

9793
Console.CancelKeyPress += (sender, eventArgs) => { };
9894

99-
10095
var process = Process.GetCurrentProcess();
10196
process.PriorityClass = ProcessPriorityClass.AboveNormal;
10297

@@ -134,13 +129,12 @@ private static async Task Main(string[] args)
134129

135130
var imageResults = directImages.Select(ImageResult.FromDirectImage);
136131

137-
138-
var options = AppInterface.CreateResultOptions(imageResults, "Image");
132+
var directOptions = AppInterface.CreateResultOptions(imageResults, "Image");
139133

140134

141135
NConsole.ReadOptions(new NConsoleDialog
142136
{
143-
Options = options,
137+
Options = directOptions,
144138
Description = AppInterface.Description
145139
});
146140

Test/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Text;
77
using System.Threading.Tasks;
88
using RestSharp;
9+
using RestSharp.Authenticators;
910
using SimpleCore.Utilities;
1011
using SmartImage.Lib;
1112
using SmartImage.Lib.Engines;
@@ -174,7 +175,7 @@ public static async Task Main(string[] args)
174175
// Console.WriteLine(image);
175176
// }
176177

177-
178+
178179
}
179180
}
180181
}

0 commit comments

Comments
 (0)