482a46b568
Updated project files for `Beam.Dynamic`, `Beam.Exports`, `Beam.Temporary.Cli`, and `Beam` to include additional metadata and specific package versions. Refactored `DataBindings` and `ResolvedBindings` to records, added a new `Text` property in `Binding.cs`, and introduced `ParseNumbers` in `OnlineCleaner`. New classes `PuppetContext` and `PuppetUnitDownloader` added for Playwright integration. Introduced `ImmutableState` struct and `UnitDownloaderBinary` class for improved download management. Updated tests in `UnitTest1.cs` for number localization. Added `Beam.Puppeteer` project to the solution.
40 lines
1.4 KiB
C#
40 lines
1.4 KiB
C#
|
|
using HtmlAgilityPack;
|
|
using Microsoft.Playwright;
|
|
|
|
namespace Beam.Puppeteer {
|
|
public class PuppetContext(IPlaywright playwright, IBrowser browser) {
|
|
public IPlaywright Playwright { get; set; } = playwright;
|
|
public IBrowser Browser { get; set; } = browser;
|
|
}
|
|
|
|
public class PuppetUnitDownloader<T> : UnitDownloader<T> {
|
|
public PuppetContext Context { get; }
|
|
|
|
public PuppetUnitDownloader(PuppetContext pc, DownloadContext<T> context)
|
|
: base(context.Web, context.AsyncTranformer, context.AsyncFailurePredicates) {
|
|
Context = pc;
|
|
}
|
|
|
|
protected override async Task<(bool, T?)> TryDownloadWithNoRetries(string link, CancellationToken ct) {
|
|
var page = await Context.Browser.NewPageAsync();
|
|
try {
|
|
var content = await page.ContentAsync();
|
|
await page.CloseAsync();
|
|
|
|
HtmlDocument doc = new();
|
|
doc.LoadHtml(content);
|
|
var transformed = await Transformer(doc);
|
|
if (FailurePredicates is null || !(await IsFailure(doc)))
|
|
return (true, transformed);
|
|
return (false, default);
|
|
} catch (Exception) {
|
|
return (false, default);
|
|
} finally {
|
|
if (!page.IsClosed)
|
|
await page.CloseAsync();
|
|
}
|
|
}
|
|
}
|
|
}
|