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 : UnitDownloader { public StealthConfig Config { get; } public StealthAsyncManipulator Manipulator { get; } private ILogger? Logger => Config.Logger; private IDownloadStrategy _downloadStrategy { get; } public StealthUnitDownloader(UnitDownloaderOptions 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 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); } } }