7ed05abdb8
- Introduced modularity by splitting Beam into new projects: Beam.Abstractions, Beam.Models, and Beam.Downloaders. - Refactored existing classes into appropriate namespaces and projects. - Replaced specific implementations with abstractions (e.g., SourceLinkBuilder to LinkBuilder, State to IState, etc.). - Updated interfaces: added ITemplate, IArticleData, IDownloadReport, and others for improved extensibility. - Removed deprecated classes like SourceLinkBuilder and StateChangerFactory. - Enhanced link handling in downloaders by refactoring to use `string` over `SourceLink`. - Consolidated shared logic under Beam.Abstractions.
119 lines
4.4 KiB
C#
119 lines
4.4 KiB
C#
using Beam.Abstractions;
|
|
using Beam.Models;
|
|
using HtmlAgilityPack;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Beam.Downloaders {
|
|
|
|
public class DownloadContextBuilder<RawType> {
|
|
private HtmlWeb _web;
|
|
private HttpClient _client;
|
|
private IProgress<IDownloadReport>? _downloadReporter;
|
|
private IProgress<IRetryReport>? _retryReporter;
|
|
private AsyncDownloadFailurePredicate<RawType>?[] _asyncFailurePredicates = [];
|
|
private TimeSpan _timeOut;
|
|
private IEnumerable<string> _links;
|
|
private CancellationToken _cancellationToken;
|
|
private DocumentCache _cache;
|
|
private ILogger? _downloadLogger;
|
|
|
|
public DownloadContextBuilder(HttpClient? client = null, HtmlWeb? web = null) {
|
|
// You can initialize defaults here if needed, e.g.:
|
|
// _timeOut = TimeSpan.FromSeconds(30);
|
|
// _cancellationToken = CancellationToken.None;
|
|
_client = client ?? new();
|
|
_web = web ?? new();
|
|
_links = [];
|
|
}
|
|
|
|
public DownloadContextBuilder<RawType> WithWeb(HtmlWeb web) {
|
|
_web = web;
|
|
return this;
|
|
}
|
|
|
|
public DownloadContextBuilder<RawType> WithClient(HttpClient client) {
|
|
_client = client;
|
|
return this;
|
|
}
|
|
|
|
public DownloadContextBuilder<RawType> WithDownloadReporter(IProgress<IDownloadReport> downloadReporter) {
|
|
_downloadReporter = downloadReporter;
|
|
return this;
|
|
}
|
|
|
|
public DownloadContextBuilder<RawType> WithRetryReporter(IProgress<IRetryReport> retryReporter) {
|
|
_retryReporter = retryReporter;
|
|
return this;
|
|
}
|
|
|
|
public DownloadContextBuilder<RawType> WithAsyncFailurePredicates(params AsyncDownloadFailurePredicate<RawType>[] predicates) {
|
|
_asyncFailurePredicates = predicates;
|
|
return this;
|
|
}
|
|
|
|
public DownloadContextBuilder<RawType> WithTimeOut(TimeSpan timeOut) {
|
|
_timeOut = timeOut;
|
|
return this;
|
|
}
|
|
|
|
public DownloadContextBuilder<RawType> WithLinks(IEnumerable<string> links) {
|
|
_links = links;
|
|
return this;
|
|
}
|
|
|
|
public DownloadContextBuilder<RawType> WithCancellationToken(CancellationToken cancellationToken) {
|
|
_cancellationToken = cancellationToken;
|
|
return this;
|
|
}
|
|
|
|
public DownloadContextBuilder<RawType> WithCache(DocumentCache cache) {
|
|
_cache = cache;
|
|
return this;
|
|
}
|
|
|
|
public DownloadContextBuilder<RawType> WithDownloadLogger(ILogger downloadLogger) {
|
|
_downloadLogger = downloadLogger;
|
|
return this;
|
|
}
|
|
|
|
|
|
public DownloadContext<RawType> Build() {
|
|
// Construct the DownloadContext<T> using the collected values.
|
|
var context = new DownloadContext<RawType>(
|
|
web: _web,
|
|
client: _client,
|
|
links: _links,
|
|
cancellationToken: _cancellationToken,
|
|
downloadReporter: _downloadReporter,
|
|
retryReporter: _retryReporter,
|
|
asyncFailurePredicates: _asyncFailurePredicates,
|
|
timeOut: _timeOut,
|
|
downloadLogger: _downloadLogger
|
|
);
|
|
|
|
//// Assign the DocumentCache if it's been set in the builder.
|
|
//// (Even though Cache has a private setter, this code assumes builder
|
|
//// is in the same assembly or that the setter will be made internal.
|
|
//// Otherwise, remove or adjust this line.)
|
|
//context.Cache = _cache;
|
|
|
|
return context;
|
|
}
|
|
|
|
public static DownloadContextBuilder<RawType> FromContext(DownloadContext<RawType> existing) {
|
|
if (existing == null) throw new ArgumentNullException(nameof(existing));
|
|
|
|
return new DownloadContextBuilder<RawType>(existing.Client, existing.Web)
|
|
.WithLinks(existing.Links)
|
|
.WithCancellationToken(existing.CancellationToken)
|
|
.WithDownloadReporter(existing.DownloadReporter!)
|
|
.WithRetryReporter(existing.RetryReporter!)
|
|
.WithAsyncFailurePredicates(existing.AsyncFailurePredicates ?? Array.Empty<AsyncDownloadFailurePredicate<RawType>>())
|
|
.WithTimeOut(existing.TimeOut)
|
|
.WithDownloadLogger(existing.DownloadLogger!)
|
|
.WithCache(existing.Cache);
|
|
}
|
|
}
|
|
|
|
}
|