Refactor downloaders to use generic options and unify logic

Replaces specialized binary and HTML downloaders with a generic, options-driven UnitDownloader and UnitFragmentDownloader pattern. Introduces UnitDownloaderOptions and builder classes for flexible configuration, updates interfaces and method signatures to support progress reporting, and removes redundant binary-specific classes. Updates Playwright and Stealth downloaders to use the new generic base, and adds improved error handling and reporting. Also updates dependency versions and makes minor API consistency improvements across the Fluent and Models layers.
This commit is contained in:
qwsdcvghyu89
2025-09-29 21:27:56 +10:00
parent 8e60109f5e
commit 2958a26e4f
30 changed files with 621 additions and 422 deletions
+9 -19
View File
@@ -5,39 +5,29 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Beam.Abstractions;
using Beam.Downloaders;
using Beam.Models;
namespace Beam.Stealth {
public class StealthUnitPageDownloader<T> : UnitDownloader<T> {
public class StealthUnitPageDownloader<RawType, OutType> : UnitDownloader<RawType, OutType> where RawType : IDocument {
public StealthConfig Config { get; }
public StealthAsyncManipulator Manipulator { get; }
private ILogger? Logger => Config.Logger;
public StealthUnitPageDownloader(HtmlWeb web, StealthConfig config, StealthAsyncManipulator manipulator, AsyncTransformer<HtmlDocument, T> transformer, AsyncDownloadFailurePredicate<HtmlDocument>?[]? failurePredicate = null) : base(web, transformer, failurePredicate) {
public StealthUnitPageDownloader(UnitDownloaderOptions<RawType, OutType> options, StealthConfig config, StealthAsyncManipulator manipulator) : base(options) {
Config = config;
Manipulator = manipulator;
}
protected async override Task<(bool, T?)> TryDownloadWithNoRetries(string link, CancellationToken ct) {
try {
var driver = Config.Driver;
protected override async Task DownloadToStream(string url, int bufferSize, Stream destinationStream, IProgress<IDownloadReport> progress, CancellationToken ct) {
var driver = Config.Driver;
await driver.Navigate().GoToUrlAsync(link);
await Manipulator(driver);
await driver.Navigate().GoToUrlAsync(url);
await Manipulator(driver);
HtmlDocument doc = new();
doc.LoadHtml(driver.PageSource);
if (await IsFailure(doc))
return (false, default);
return (true, await Transformer(doc));
} catch (Exception e) {
Logger?.LogError(e, "Error occurred downloading {}", link);
return (false, default);
}
byte[] bytes = Encoding.UTF8.GetBytes(driver.PageSource);
await destinationStream.WriteAsync(bytes, ct);
}
}
}