2965270928
- ApiResponse: add readToBuffer option to defer/stream body instead of eagerly buffering - TableDataProvider: implement HTML table parser with per-column provider support - StealthConfig: add 10s page load timeout and copyCookiesFrom parameter for cookie sharing - StealthUnitDownloader: catch WebDriverTimeoutException on navigation, log warning instead of throwing - Bump version to 2.9.0
55 lines
1.9 KiB
C#
55 lines
1.9 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using OpenQA.Selenium.Chrome;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Beam.Abstractions;
|
|
using Beam.Downloaders;
|
|
using Beam.Models;
|
|
using Beam.Stealth.Strategies;
|
|
using OpenQA.Selenium;
|
|
using OpenQA.Selenium.Firefox;
|
|
|
|
namespace Beam.Stealth {
|
|
using File = System.IO.File;
|
|
|
|
public class StealthUnitDownloader<OutType> : UnitDownloader<OutType> {
|
|
public StealthConfig Config { get; }
|
|
public StealthAsyncManipulator Manipulator { get; }
|
|
private ILogger? Logger => Config.Logger;
|
|
|
|
private IDownloadStrategy _downloadStrategy { get; }
|
|
|
|
public StealthUnitDownloader(UnitDownloaderOptions<OutType> options, StealthConfig config, StealthAsyncManipulator manipulator) : base(options) {
|
|
Config = config;
|
|
Manipulator = manipulator;
|
|
|
|
_downloadStrategy = options.Target switch {
|
|
DownloadTarget.URL or DownloadTarget.InURL => new PageDownloadStrategy(),
|
|
DownloadTarget.Complex => new WaitingDownloadStrategy(),
|
|
_ => throw new NotSupportedException() // TODO add an exception message
|
|
};
|
|
}
|
|
|
|
protected override async Task DownloadToStream(string url, int bufferSize, Stream destinationStream,
|
|
IProgress<IDownloadReport> progress, CancellationToken ct) {
|
|
var driver = Config.Driver;
|
|
try {
|
|
await driver.Navigate().GoToUrlAsync(url);
|
|
}
|
|
catch (WebDriverTimeoutException) {
|
|
Logger?.LogWarning("Timeout navigating to {url}", url);
|
|
}
|
|
|
|
await Manipulator(driver);
|
|
|
|
await _downloadStrategy.DownloadToStream(url, bufferSize, destinationStream, progress, Config, Logger, ct);
|
|
}
|
|
|
|
|
|
}
|
|
}
|