using Febe.Core.Abstractions; using Febe.Core.Collection.Diagnostics; using Febe.Core.Collection.Readers; using Febe.Core.Domain; using Febe.Core.Engine; using Febe.Core.Storage; namespace Febe.Core.Collection.Recipes; /// Everything the recipes work with. public sealed record RecipeEnvironment { /// The page, through the browser (or a fake in tests and simulation). public required IPageDriver Driver { get; init; } /// What the page receives, read by the site's readers. public required CaptureCollector Capture { get; init; } /// The single point every counted action passes through (rule 7). public required IGovernor Governor { get; init; } /// Stop signals: raised by the capture, reported by the recipes (empty pages, session cookie). public required IStopSignals Signals { get; init; } public required IFebeStorage Storage { get; init; } /// Pages and targets of the site (bersagli.*.json). public required SiteMap SiteMap { get; init; } public required IClock Clock { get; init; } public required IRandomSource Random { get; init; } /// Remaining budgets (stories need at least ten). Null: the check is left to the Governor alone. public IBudgetView? Budget { get; init; } /// Daily data budget: the backfill stops when it is used up (spec §6.3). Null: not checked here. public IDataBudget? DataBudget { get; init; } /// The circuit breaker, to stop when it opens. Null: only the signals of the run count. public ICircuitBreaker? Breaker { get; init; } public RecipeOptions Options { get; init; } = new(); /// The recorder of the diagnostic report (issue #39). Null: nothing is recorded. public CollectionDiagnostics? Diagnostics { get; init; } /// /// The turns of the page shared with the profile search (issue #37): recipes give the page to the search before an /// action that opens a page, when the search waits. Null: no search, no turn points. /// public PageTurns? PageTurns { get; init; } } /// /// The recipes of spec §6.3 over a page driver: Giro dei seguiti, Storie di un account, Controllo del profilo, Storico, /// Raccolta in evidenza, Taggati, Reel, Post singolo, Elenco dei seguiti. /// /// /// Common rules: the Governor is asked before every counted action and a refusal stops the recipe in order; a stop /// signal stops it where it is and leaves the page as it is; a missing target stops it with a report, never with /// coordinates; an unknown response shape stops it with and an event /// that carries only the key structure. A recipe refuses to run, as , while the /// site's map lacks a page or target it needs or the reader vocabulary is not usable (Instagram's targets, H11). /// public sealed partial class RecipeRunner : IRecipeRunner { private readonly Lock _refusalsGate = new(); private readonly HashSet _refusalsReported = []; private long? _sessionId; private RecipeKind? _lastKind; public RecipeRunner(RecipeEnvironment environment) { Env = environment ?? throw new ArgumentNullException(nameof(environment)); Writer = new CollectionWriter(environment.Storage, environment.Clock) { Diagnostics = environment.Diagnostics }; } public RecipeEnvironment Env { get; } public CollectionWriter Writer { get; } /// Italian name of a recipe, for messages and events. public static string Name(RecipeKind kind) => kind switch { RecipeKind.FollowingSweep => "Giro dei seguiti", RecipeKind.AccountStories => "Storie di un account", RecipeKind.ProfileCheck => "Controllo del profilo", RecipeKind.Backfill => "Storico", RecipeKind.Highlight => "Raccolta in evidenza", RecipeKind.Tagged => "Taggati", RecipeKind.Reels => "Reel", RecipeKind.SinglePost => "Post singolo", RecipeKind.FollowingList => "Elenco dei seguiti", _ => kind.ToString(), }; /// /// Pages and targets a recipe cannot work without. «profile-not-found» is not among them: it is looked up only when /// the map sets it, and without it a profile that does not exist gives no data, judged like any page that arrives /// empty. /// public static (IReadOnlyList Pages, IReadOnlyList Targets) Requirements(RecipeKind kind) => kind switch { RecipeKind.FollowingSweep => ([SitePages.Home], []), RecipeKind.AccountStories => ([SitePages.Profile, SitePages.StoryViewer], [SiteTargets.ProfileStoryRing, SiteTargets.ViewerNext, SiteTargets.ViewerClose]), RecipeKind.ProfileCheck or RecipeKind.Backfill => ([SitePages.Profile], []), RecipeKind.Highlight => ([SitePages.Profile, SitePages.HighlightViewer], [SiteTargets.ProfileHighlight, SiteTargets.ViewerNext, SiteTargets.ViewerClose]), RecipeKind.Tagged => ([SitePages.Profile], [SiteTargets.ProfileTabTagged]), RecipeKind.Reels => ([SitePages.Profile], [SiteTargets.ProfileTabReels]), RecipeKind.SinglePost => ([SitePages.Post], []), RecipeKind.FollowingList => ([SitePages.Following], []), _ => ([], []), }; /// Italian description of a stop signal. public static string Describe(Signal signal) => signal.Level switch { SignalLevel.Throttle => $"Segnale di rallentamento ({signal.Code}): la ricetta si ferma dov'è.", SignalLevel.Challenge => $"Il sito chiede una verifica ({signal.Code}): Febe si ferma e lascia la pagina com'è.", SignalLevel.Disconnect => $"La sessione è terminata ({signal.Code}): Febe si ferma e non tenta di rientrare.", _ => $"Segnale {signal.Code}.", }; public async Task RunAsync(RecipeRequest request, ISessionContext session, CancellationToken ct) { ArgumentNullException.ThrowIfNull(request); ArgumentNullException.ThrowIfNull(session); var name = Name(request.Kind); // A map or a vocabulary that lacks what the recipe needs is a state of the configuration, not a failure of the // recipe: nothing is done, nothing counts towards suspension, and it is said once per recipe and process (the // engine stops planning it, NotConfiguredReason). if (Refusal(request.Kind) is { } refusal) { bool first; lock (_refusalsGate) { first = _refusalsReported.Add(request.Kind); } if (first) { await Writer.EventAsync(refusal.Code, refusal.Message + SessionComposer.NotConfiguredTail, EventSeverity.Warning, null, session.SessionId, ct).ConfigureAwait(false); } Env.Diagnostics?.RecipeEnded(session.SessionId, request.Kind, RecipeStatus.NotConfigured, null, 0, 0); return new RecipeOutcome(RecipeStatus.NotConfigured, 0, 0, refusal.Message); } if (Env.Breaker is { } breaker && !breaker.State.AllowsSiteActions) { Env.Diagnostics?.RecipeEnded(session.SessionId, request.Kind, RecipeStatus.Denied, breaker.State.LastSignal?.Code, 0, 0); return new RecipeOutcome(RecipeStatus.Denied, 0, 0, "L'interruttore è aperto: nessuna azione verso il sito."); } if (_sessionId != session.SessionId) { Env.Driver.BeginSession(session.Mood); _sessionId = session.SessionId; _lastKind = null; } using var run = new RecipeRun(this, request, session, ct); RecipeStatus status; string? text = null; string? errorType = null; try { await CheckSessionCookieAsync(run).ConfigureAwait(false); status = request.Kind switch { RecipeKind.FollowingSweep => await FollowingSweepAsync(run).ConfigureAwait(false), RecipeKind.AccountStories => await AccountStoriesAsync(run).ConfigureAwait(false), RecipeKind.ProfileCheck => await ProfileCheckAsync(run).ConfigureAwait(false), RecipeKind.Backfill => await WalkAsync(run, ProfileWalk.Posts).ConfigureAwait(false), RecipeKind.Tagged => await WalkAsync(run, ProfileWalk.Tagged).ConfigureAwait(false), RecipeKind.Reels => await WalkAsync(run, ProfileWalk.Reels).ConfigureAwait(false), RecipeKind.Highlight => await HighlightAsync(run).ConfigureAwait(false), RecipeKind.SinglePost => await SinglePostAsync(run).ConfigureAwait(false), RecipeKind.FollowingList => await FollowingListAsync(run).ConfigureAwait(false), _ => throw RecipeRun.Stop(RecipeStatus.Failed, $"Ricetta sconosciuta: {request.Kind}."), }; } catch (RecipeStopException stop) { status = stop.Status; text = stop.Message; errorType = stop.ErrorType; } catch (OperationCanceledException) when (ct.IsCancellationRequested) { status = RecipeStatus.Cancelled; text = "Ricetta interrotta."; } catch (OperationCanceledException ex) { // E.g. Alberto took the browser: the gesture was not performed. status = RecipeStatus.Cancelled; text = ex.Message; } catch (Exception ex) when (ex is not OutOfMemoryException) { status = RecipeStatus.Failed; text = $"Errore imprevisto in «{name}» ({ex.GetType().Name}): {ex.Message}"; errorType = ex.GetType().Name; } if (run.Signal is { } signal && status is not (RecipeStatus.StoppedBySignal or RecipeStatus.Cancelled)) { status = RecipeStatus.StoppedBySignal; text = Describe(signal); } try { await ReportAsync(run, status, text, CancellationToken.None).ConfigureAwait(false); } catch (Exception ex) when (ex is not OperationCanceledException and not OutOfMemoryException) { // The timeline is a convenience: failing to write it never changes how the recipe ended. } // The report takes the status and the code of the signal, never the message (it may name an account). A recipe // cancelled because the breaker stopped (a signal reported straight to it, like the session cookie lost) gets // the breaker's code too. var breakerState = Env.Breaker?.State; var signalCode = run.Signal?.Code ?? status switch { RecipeStatus.StoppedBySignal => breakerState?.LastSignal?.Code, RecipeStatus.Cancelled when breakerState is { AllowsSiteActions: false } => breakerState.LastSignal?.Code, _ => null, }; Env.Diagnostics?.RecipeEnded(session.SessionId, request.Kind, status, signalCode, run.Actions, run.NewEntries, status == RecipeStatus.Failed ? errorType : null); _lastKind = request.Kind; return new RecipeOutcome(status, run.Actions, run.NewEntries, text) { AccountsWithStories = [.. run.AccountsWithStories], AccountsWithNewPosts = [.. run.AccountsWithNewPosts], BackfillFinished = run.Finished, }; } /// public string? NotConfiguredReason(RecipeKind kind) => Refusal(kind)?.Message; /// /// Why the site's configuration does not let run, with the code of its timeline line: pages /// or targets missing from the map, or a reader vocabulary that cannot be relied on. Null when it can run. /// private (string Code, string Message)? Refusal(RecipeKind kind) { var name = Name(kind); var (pages, targets) = Requirements(kind); var missing = Env.SiteMap.Missing(pages, targets); if (missing.Count > 0) { return ("mappa-incompleta", $"«{name}» non può girare su {Env.SiteMap.Site}: la mappa dei bersagli non è ancora compilata (mancano {string.Join(", ", missing)})."); } return Env.Capture.Reader.Vocabulary.UnusableReason is { } unusable ? ("lettori-incompleti", $"«{name}» non può girare: {unusable}") : null; } /// /// The rest page (spec §6.1). After a verification request or a disconnection the page is left as it is, so that /// Alberto sees what happened (spec §7.6): resting is skipped while the breaker says so. /// public Task RestAsync(CancellationToken ct) { if (Env.Breaker?.State.Status is BreakerStatus.Challenge or BreakerStatus.Disconnected) { return Task.CompletedTask; } return Env.Driver.RestAsync(ct); } private async Task CheckSessionCookieAsync(RecipeRun run) { if (await Env.Driver.HasSessionCookieAsync(run.Ct).ConfigureAwait(false) is false) { Env.Signals.ObserveSessionCookie(false); run.ThrowIfSignal(); } } private async Task ReportAsync(RecipeRun run, RecipeStatus status, string? text, CancellationToken ct) { var name = Name(run.Request.Kind); var accountId = run.Request.AccountId; switch (status) { case RecipeStatus.UnrecognizedFormat: var shapes = run.UnknownShapes.Count == 0 ? "(nessuna struttura)" : string.Join(" · ", run.UnknownShapes.Take(3)); await Writer.EventAsync("formato-non-riconosciuto", $"Formato non riconosciuto in «{name}». Struttura delle chiavi: {shapes}", EventSeverity.Warning, accountId, run.Session.SessionId, ct).ConfigureAwait(false); if (accountId is { } id) { await Env.Storage.Accounts.AddNoticesAsync(id, AccountNotices.UnrecognizedFormat, Env.Clock.Now, ct).ConfigureAwait(false); } break; case RecipeStatus.TargetNotFound: await Writer.EventAsync("bersaglio-non-trovato", $"«{name}» si è fermata: {text}", EventSeverity.Error, accountId, run.Session.SessionId, ct).ConfigureAwait(false); break; case RecipeStatus.Failed: await Writer.EventAsync("ricetta-fallita", text ?? $"«{name}» non è riuscita.", EventSeverity.Error, accountId, run.Session.SessionId, ct).ConfigureAwait(false); break; } } private async Task LoadAccountAsync(RecipeRun run) { if (run.Request.AccountId is not { } id) { throw RecipeRun.Stop(RecipeStatus.Failed, $"«{Name(run.Request.Kind)}» ha bisogno di un account."); } var account = await Env.Storage.Accounts.GetAsync(id, run.Ct).ConfigureAwait(false) ?? throw RecipeRun.Stop(RecipeStatus.Failed, $"Account {id} non trovato."); if (account.State == AccountState.ArchiveOnly) { throw RecipeRun.Stop(RecipeStatus.Denied, $"{account.Username} è «Solo archivio»: non si contatta."); } return account; } /// The profile of in the reads: same identifier, or same name when the identifier is not known yet. private static ProfileInfo? FindProfile(IEnumerable reads, Account account) => reads.SelectMany(r => r.Result.Profiles) .Where(p => account.InstagramId is { } id ? p.Id == id || string.Equals(p.Username, account.Username, StringComparison.OrdinalIgnoreCase) : string.Equals(p.Username, account.Username, StringComparison.OrdinalIgnoreCase)) .OrderByDescending(p => (p.PostCount is null ? 0 : 1) + (p.IsPrivate is null ? 0 : 1)) .FirstOrDefault(); /// Content items of the reads, in arrival order, without repetitions. private static List Contents(IEnumerable reads, Func? filter = null) { var seen = new HashSet(StringComparer.Ordinal); return [.. reads.SelectMany(r => r.Result.Contents).Where(c => (filter is null || filter(c)) && seen.Add(c.Id))]; } private static bool IsOwnedBy(ContentItem item, Account account) => item.Owner is { } owner && (account.InstagramId is { } id ? owner.Id == id : string.Equals(owner.Username, account.Username, StringComparison.OrdinalIgnoreCase)); /// /// A post, not a story (it expires) nor an item found inside a highlight collection. The reader recognizes all three /// by the same shape; stories and collection items are saved only by the viewer recipes, and only when shown /// (spec §6.3: «anche quando la pagina riceve in anticipo l'elenco completo»). /// private static bool IsPostLike(ContentItem item) => item.ExpiresAt is null && item.HighlightId is null; /// /// Pinned on top of a profile (spec §6.3: not «already known» for the stop rule, nor a limit of history). When the /// vocabulary has no key for it, a post followed on its page by a newer one is out of the order of the grid, /// newest first: that is what a pinned post looks like. /// private bool LooksPinned(ContentItem item, IReadOnlyList page) { if (item.Pinned) { return true; } if (Env.Capture.Reader.Vocabulary.Content.Pinned.IsSet) { return false; } var index = -1; for (var i = 0; i < page.Count; i++) { if (string.Equals(page[i].Id, item.Id, StringComparison.Ordinal)) { index = i; break; } } return index >= 0 && page.Skip(index + 1).Any(later => later.TakenAt > item.TakenAt); } private static IReadOnlyDictionary Values(params (string Key, string Value)[] pairs) => pairs.ToDictionary(p => p.Key, p => p.Value, StringComparer.Ordinal); }