Raccolta (M0/M2): sito di prova completo, lettori per forma, ricette su browser vero, coda degli scaricamenti, sonda con ripulitura e verifica; documenti delle ipotesi e delle prove dal vivo
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
74 files changed
+12689
-13
No files matched your search
@@ -0,0 +1,292 @@
|
||||
using Febe.Core.Abstractions;
|
||||
using Febe.Core.Collection.Readers;
|
||||
using Febe.Core.Domain;
|
||||
using Febe.Core.Engine;
|
||||
using Febe.Core.Storage;
|
||||
|
||||
namespace Febe.Core.Collection.Recipes;
|
||||
|
||||
/// <summary>Everything the recipes work with.</summary>
|
||||
public sealed record RecipeEnvironment
|
||||
{
|
||||
/// <summary>The page, through the browser (or a fake in tests and simulation).</summary>
|
||||
public required IPageDriver Driver { get; init; }
|
||||
|
||||
/// <summary>What the page receives, read by the site's readers.</summary>
|
||||
public required CaptureCollector Capture { get; init; }
|
||||
|
||||
/// <summary>The single point every counted action passes through (rule 7).</summary>
|
||||
public required IGovernor Governor { get; init; }
|
||||
|
||||
/// <summary>Stop signals: raised by the capture, reported by the recipes (empty pages, session cookie).</summary>
|
||||
public required IStopSignals Signals { get; init; }
|
||||
|
||||
public required IFebeStorage Storage { get; init; }
|
||||
|
||||
/// <summary>Pages and targets of the site (<c>bersagli.*.json</c>).</summary>
|
||||
public required SiteMap SiteMap { get; init; }
|
||||
|
||||
public required IClock Clock { get; init; }
|
||||
|
||||
public required IRandomSource Random { get; init; }
|
||||
|
||||
/// <summary>Remaining budgets (stories need at least ten). Null: the check is left to the Governor alone.</summary>
|
||||
public IBudgetView? Budget { get; init; }
|
||||
|
||||
/// <summary>Daily data budget: the backfill stops when it is used up (spec §6.3). Null: not checked here.</summary>
|
||||
public IDataBudget? DataBudget { get; init; }
|
||||
|
||||
/// <summary>The circuit breaker, to stop when it opens. Null: only the signals of the run count.</summary>
|
||||
public ICircuitBreaker? Breaker { get; init; }
|
||||
|
||||
public RecipeOptions Options { get; init; } = new();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 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 <see cref="RecipeStatus.UnrecognizedFormat"/> and an event
|
||||
/// that carries only the key structure. Recipes refuse to run on a site whose map or reader vocabulary is still a
|
||||
/// skeleton (the first Instagram files, to be filled after M0).
|
||||
/// </remarks>
|
||||
public sealed partial class RecipeRunner : IRecipeRunner
|
||||
{
|
||||
private long? _sessionId;
|
||||
private RecipeKind? _lastKind;
|
||||
|
||||
public RecipeRunner(RecipeEnvironment environment)
|
||||
{
|
||||
Env = environment ?? throw new ArgumentNullException(nameof(environment));
|
||||
Writer = new CollectionWriter(environment.Storage, environment.Clock);
|
||||
}
|
||||
|
||||
public RecipeEnvironment Env { get; }
|
||||
|
||||
public CollectionWriter Writer { get; }
|
||||
|
||||
/// <summary>Italian name of a recipe, for messages and events.</summary>
|
||||
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(),
|
||||
};
|
||||
|
||||
/// <summary>Pages and targets a recipe cannot work without.</summary>
|
||||
public static (IReadOnlyList<string> Pages, IReadOnlyList<string> 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], [SiteTargets.ProfileNotFound]),
|
||||
RecipeKind.Highlight => ([SitePages.Profile, SitePages.HighlightViewer], [SiteTargets.ProfileHighlight, SiteTargets.ViewerNext, SiteTargets.ViewerClose]),
|
||||
RecipeKind.Tagged => ([SitePages.Profile], [SiteTargets.ProfileNotFound, SiteTargets.ProfileTabTagged]),
|
||||
RecipeKind.Reels => ([SitePages.Profile], [SiteTargets.ProfileNotFound, SiteTargets.ProfileTabReels]),
|
||||
RecipeKind.SinglePost => ([SitePages.Post], []),
|
||||
RecipeKind.FollowingList => ([SitePages.Following], []),
|
||||
_ => ([], []),
|
||||
};
|
||||
|
||||
/// <summary>Italian description of a stop signal.</summary>
|
||||
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<RecipeOutcome> RunAsync(RecipeRequest request, ISessionContext session, CancellationToken ct)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(request);
|
||||
ArgumentNullException.ThrowIfNull(session);
|
||||
var name = Name(request.Kind);
|
||||
|
||||
var (pages, targets) = Requirements(request.Kind);
|
||||
var missing = Env.SiteMap.Missing(pages, targets);
|
||||
if (missing.Count > 0)
|
||||
{
|
||||
var message = $"«{name}» non può girare su {Env.SiteMap.Site}: la mappa dei bersagli non è ancora compilata (mancano {string.Join(", ", missing)}). Va completata con i campioni della sonda di M0 (ipotesi H11).";
|
||||
await Writer.EventAsync("mappa-incompleta", message, EventSeverity.Error, null, session.SessionId, ct).ConfigureAwait(false);
|
||||
return new RecipeOutcome(RecipeStatus.TargetNotFound, 0, 0, message);
|
||||
}
|
||||
|
||||
if (Env.Capture.Reader.Vocabulary.UnusableReason is { } unusable)
|
||||
{
|
||||
var message = $"«{name}» non può girare: {unusable}";
|
||||
await Writer.EventAsync("lettori-incompleti", message, EventSeverity.Error, null, session.SessionId, ct).ConfigureAwait(false);
|
||||
return new RecipeOutcome(RecipeStatus.UnrecognizedFormat, 0, 0, message);
|
||||
}
|
||||
|
||||
if (Env.Breaker is { } breaker && !breaker.State.AllowsSiteActions)
|
||||
{
|
||||
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;
|
||||
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;
|
||||
}
|
||||
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}";
|
||||
}
|
||||
|
||||
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.
|
||||
}
|
||||
|
||||
_lastKind = request.Kind;
|
||||
return new RecipeOutcome(status, run.Actions, run.NewEntries, text)
|
||||
{
|
||||
AccountsWithStories = [.. run.AccountsWithStories],
|
||||
AccountsWithNewPosts = [.. run.AccountsWithNewPosts],
|
||||
BackfillFinished = run.Finished,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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<Account> 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;
|
||||
}
|
||||
|
||||
/// <summary>The profile of <paramref name="account"/> in the reads: same identifier, or same name when the identifier is not known yet.</summary>
|
||||
private static ProfileInfo? FindProfile(IEnumerable<CapturedRead> 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();
|
||||
|
||||
/// <summary>Content items of the reads, in arrival order, without repetitions.</summary>
|
||||
private static List<ContentItem> Contents(IEnumerable<CapturedRead> reads, Func<ContentItem, bool>? filter = null)
|
||||
{
|
||||
var seen = new HashSet<string>(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));
|
||||
|
||||
private static IReadOnlyDictionary<string, string> Values(params (string Key, string Value)[] pairs) =>
|
||||
pairs.ToDictionary(p => p.Key, p => p.Value, StringComparer.Ordinal);
|
||||
}
|
||||
Reference in new issue
Block a user