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.
44 lines
1.5 KiB
C#
44 lines
1.5 KiB
C#
using HtmlAgilityPack;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Beam.Downloaders;
|
|
using Beam.Models;
|
|
|
|
namespace Beam.Stealth {
|
|
public class StealthUnitPageDownloader<T> : UnitDownloader<T> {
|
|
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) {
|
|
Config = config;
|
|
Manipulator = manipulator;
|
|
}
|
|
|
|
protected async override Task<(bool, T?)> TryDownloadWithNoRetries(string link, CancellationToken ct) {
|
|
try {
|
|
var driver = Config.Driver;
|
|
|
|
await driver.Navigate().GoToUrlAsync(link);
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|