Files
Febe/src/Febe.Core/Collection/Recipes/RecipeRunner.Profile.cs
T
Alby96andClaude Opus 5.5 0547c747c5 D1, seconda parte: «Cosa scaricare prima» anche per account, reel distinti e quota per chi aspetta
L'ordine dei tipi (storie, evidenza, post, reel, taggati) ora vale anche per il singolo account:
nella scheda account → Impostazioni si sceglie «Come le impostazioni generali» o un ordine suo,
con lo stesso elenco da trascinare (o Su/Giù da tastiera) di Limiti e comportamento, ora un
modulo solo (download-order.js). Il motore pesa le visite con l'ordine dell'account, la coda dei
file mette ogni file al posto del suo tipo nell'ordine del suo account, stabile dentro il tipo
(scadenza, scoperta, elemento, posizione).

Perché nessun tipo resti fermo per sempre quando il tetto dei dati finisce prima della coda, un
posto su otto di ogni giro della coda va al file che aspetta da più tempo, di qualunque tipo.

I reel non si inventano (regola 9): un post è reel se viene dalla scheda Reel o se la risposta
ha product_type «clips», valore visto nei campioni ripuliti. Il segno si scrive da solo e non si
toglie mai, così una copia vecchia non sposta di nuovo i file in coda.

Migrazione 0002_ordine_scaricamento (account.download_order, entry.is_reel): prende il primo
numero dopo quelle unite, E1 userà il successivo. Prove su motore (seme fisso), coda, lettori,
ricette e API; dimostrazione con un account che ha l'ordine suo; guida e decisioni aggiornate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 15:25:34 +01:00

911 lines
46 KiB
C#

using System.Globalization;
using Febe.Core.Abstractions;
using Febe.Core.Collection.Readers;
using Febe.Core.Domain;
using Febe.Core.Engine;
namespace Febe.Core.Collection.Recipes;
public sealed partial class RecipeRunner
{
/// <summary>A walk through the pages of a profile tab (Storico on the grid, Taggati, Reel, the gap of the check).</summary>
private sealed record ProfileWalk(EntryKind Kind, string? TabTarget, string ResumePage, string SettingPrefix)
{
public static readonly ProfileWalk Posts = new(EntryKind.Post, null, SitePages.ProfileFrom, "raccolta.post");
public static readonly ProfileWalk Tagged = new(EntryKind.Tagged, SiteTargets.ProfileTabTagged, SitePages.TaggedFrom, "raccolta.taggati");
public static readonly ProfileWalk Reels = new(EntryKind.Post, SiteTargets.ProfileTabReels, SitePages.ReelsFrom, "raccolta.reel");
/// <summary>The posts missing below the first page of the grid, filled by the profile check (same pages as the grid).</summary>
public static readonly ProfileWalk Gap = new(EntryKind.Post, null, SitePages.ProfileFrom, "raccolta.lacuna");
public bool IsGrid => TabTarget is null;
/// <summary>The Reel tab: every post it gives is a reel (<see cref="Entry.IsReel"/>).</summary>
public bool MarksReels => TabTarget == SiteTargets.ProfileTabReels;
/// <summary>The grid and the reels come newest first (pinned posts aside); the order of tagged posts is not known.</summary>
public bool IsChronological => Kind != EntryKind.Tagged;
public string CursorKey(long accountId) => Key(accountId, "cursore");
public string CompletedKey(long accountId) => Key(accountId, "completo");
/// <summary>The last item read before the stored cursor («time|id»): with it the point is found again by scrolling.</summary>
public string MarkKey(long accountId) => Key(accountId, "segno");
/// <summary>Gap only: known posts newer than this time do not end the walk (an older gap is still open below them).</summary>
public string KnownBeforeKey(long accountId) => Key(accountId, "fino");
private string Key(long accountId, string part) => $"{SettingPrefix}.{accountId.ToString(CultureInfo.InvariantCulture)}.{part}";
}
/// <summary>
/// Where a walk stopped: the cursor the site gave after the last page read, and the last item of that page. Without
/// a resume page in the map the walk gets back there by scrolling (decision 76): new posts on top move the pages
/// and their cursors, the item stays where it is.
/// </summary>
private sealed record ResumePoint(string Cursor, string? LastId, DateTimeOffset? LastAt)
{
public static ResumePoint Parse(string cursor, string? mark)
{
var separator = mark?.IndexOf('|', StringComparison.Ordinal) ?? -1;
if (mark is null || separator <= 0)
{
return new ResumePoint(cursor, null, null);
}
var at = DateTimeOffset.TryParse(mark[..separator], CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var time) ? time : (DateTimeOffset?)null;
var id = mark[(separator + 1)..];
return new ResumePoint(cursor, id.Length > 0 ? id : null, at);
}
public static string? MarkOf(ContentItem? item) =>
item is null ? null : $"{item.TakenAt.UtcDateTime.ToString("O", CultureInfo.InvariantCulture)}|{item.Id}";
/// <summary>
/// True when the page of <paramref name="items"/> holds the stored point or comes after it: the same cursor,
/// the same last item, or (newest first) a regular post older than it.
/// </summary>
public bool IsReachedBy(PageInfo? page, IReadOnlyList<ContentItem> items, Func<ContentItem, bool> olderCounts) =>
(page?.Cursor is { } cursor && string.Equals(cursor, Cursor, StringComparison.Ordinal))
|| (LastId is { } id && items.Any(i => string.Equals(i.Id, id, StringComparison.Ordinal)))
|| (LastAt is { } at && items.Any(i => olderCounts(i) && i.TakenAt < at));
}
/// <summary>What stops a walk at known content: nothing, any known post, or only known posts older than a time.</summary>
private sealed record KnownStop(bool Enabled, DateTimeOffset? Before = null)
{
public static readonly KnownStop Never = new(false);
public static readonly KnownStop Any = new(true);
public bool Stops(ContentItem item) => Enabled && (Before is not { } before || item.TakenAt < before);
}
/// <summary>How a walk over the pages of a profile ended.</summary>
private enum WalkEnd
{
/// <summary>No more pages: the beginning of the profile.</summary>
EndOfList,
/// <summary>A history limit of the account (from a date, last N posts).</summary>
Limit,
/// <summary>Known content, where the walk was to stop at it.</summary>
Known,
/// <summary>The pages allowed to this run were read.</summary>
Pages,
/// <summary>No page came after the bursts (counted as an empty page).</summary>
NoData,
/// <summary>By requests: the pages go on, but there is no request for the next one (no cursor, or its role not configured).</summary>
NoNext,
}
/// <summary>The rules of one walk over the pages of a profile, starting from the page on screen.</summary>
private sealed record WalkRules
{
/// <summary>A stored point to get back to by scrolling first: the pages before it are read and saved, not counted.</summary>
public ResumePoint? CatchUpTo { get; init; }
/// <summary>Pages of this run counted before the walk (a resume page, a tab).</summary>
public int PagesRead { get; init; }
public required int MaxPages { get; init; }
/// <summary>Known content that ends the walk, once past the stored point.</summary>
public required KnownStop Stop { get; init; }
/// <summary>«Lo storico non corre più veloce dei download» before each page.</summary>
public required bool CheckDownloads { get; init; }
/// <summary>Whether the backfill suspension is announced in the timeline («Storico sospeso»).</summary>
public bool AnnounceSuspension { get; init; } = true;
/// <summary>Stores the point after each page read past the stored one: its cursor and its last item.</summary>
public required Func<string, ContentItem?, Task> Store { get; init; }
/// <summary>Items already saved by the caller (they are not saved again).</summary>
public HashSet<string> Processed { get; init; } = new(StringComparer.Ordinal);
/// <summary>
/// The reads the walk starts from answer a gesture of the walk itself (a resume page, a tab): a bare empty answer
/// among them is its empty page. After opening the profile it is not: an empty answer there may be about
/// something else (the collections of an account that has none).
/// </summary>
public bool StartsFromOwnAnswer { get; init; }
}
/// <summary>
/// Controllo del profilo (spec §6.3): opens the profile (1 counted action), reads its state, the first page of
/// posts and the list of highlight collections, keeps a copy of the profile picture when it changes, and stops at
/// known content. Pinned posts are recognized and skipped for the stop rule. When the first page ends with posts
/// Febe did not have, more may be missing below it: the check goes on down the grid until known content
/// (<see cref="FillGapAsync"/>).
/// </summary>
private async Task<RecipeStatus> ProfileCheckAsync(RecipeRun run)
{
var account = await LoadAccountAsync(run).ConfigureAwait(false);
var (current, reads) = await OpenProfileAsync(run, account, waitForGrid: true).ConfigureAwait(false);
account = current;
await RecordCheckedProfileAsync(run, account, AccountProfile(reads, account)!, reads, highlights: true).ConfigureAwait(false);
var (items, tailIsNew, fillGap) = await SaveFirstGridPageAsync(run, account, reads).ConfigureAwait(false);
if (fillGap)
{
await FillGapAsync(run, account, reads, items, tailIsNew).ConfigureAwait(false);
}
await Env.Driver.MaybeDistractionAsync(run.Ct).ConfigureAwait(false);
return RecipeStatus.Completed;
}
/// <summary>
/// What a profile check keeps of the profile, whatever brought it: whether it has stories newer than the last
/// look, the list of its highlight collections (when <paramref name="highlights"/>: the list was read), a copy of
/// its picture when it changed.
/// </summary>
private async Task RecordCheckedProfileAsync(RecipeRun run, Account account, ProfileInfo profile, IReadOnlyList<CapturedRead> reads, bool highlights)
{
if (profile.LatestStoryAt is { } latest && latest > (account.LastStoriesCheckAt ?? DateTimeOffset.MinValue)
&& account.ContentTypes.HasFlag(ContentTypes.Stories))
{
run.AccountsWithStories.Add(account.Id);
}
if (highlights)
{
await UpsertListedHighlightsAsync(run, account, reads).ConfigureAwait(false);
}
if ((await Writer.SaveProfilePictureAsync(account, profile, run.Ct).ConfigureAwait(false)).Outcome == SaveOutcome.Created)
{
run.NewEntries++;
}
}
/// <summary>The highlight collections of the account that <paramref name="reads"/> list, stored.</summary>
private Task UpsertListedHighlightsAsync(RecipeRun run, Account account, IReadOnlyList<CapturedRead> reads)
{
var listed = reads.SelectMany(r => r.Result.Highlights).Where(h => h.Owner is null || h.Owner.Id == account.InstagramId).ToList();
return Writer.UpsertHighlightsAsync(account, listed, run.Ct);
}
/// <summary>
/// Saves the first page of the grid of a profile check, whatever brought it, and starts or ends the history when
/// it has not started yet. Returns the items of the page, whether the last regular one was new, and whether the
/// gap below the page is to be filled (the history has started or ended: <see cref="FillGapAsync"/>).
/// </summary>
private async Task<(IReadOnlyList<ContentItem> Items, bool TailIsNew, bool FillGap)> SaveFirstGridPageAsync(RecipeRun run, Account account, IReadOnlyList<CapturedRead> reads)
{
// The whole first page is read (it is on screen already): known posts at its top do not hide new ones below
// them, e.g. when the giro dei seguiti saved only the newest. Pinned posts are known but stop nothing.
var walk = ProfileWalk.Posts;
var items = PageItems(reads, account, walk);
var sawKnown = false;
SaveOutcome? tail = null;
foreach (var item in items)
{
var saved = await Writer.SaveAsync(account, item, EntryKind.Post, run.Session.SessionId, run.Ct).ConfigureAwait(false);
if (saved.Outcome == SaveOutcome.Created)
{
run.NewEntries++;
run.AccountsWithNewPosts.Add(account.Id);
}
if (!IsPinned(item, items, walk))
{
tail = saved.Outcome;
sawKnown |= saved.Outcome == SaveOutcome.Known;
}
}
var page = WalkPage(reads, account, walk, ownAnswer: false);
if (!sawKnown && account.BackfillCompletedAt is null && account.BackfillCursor is null)
{
if (page?.HasMore == true && page.Cursor is { } cursor)
{
await Env.Storage.Accounts.SetBackfillAsync(account.Id, cursor, null, run.Ct).ConfigureAwait(false);
await Env.Storage.Settings.SetAsync(walk.MarkKey(account.Id), ResumePoint.MarkOf(items.LastOrDefault()), run.Ct).ConfigureAwait(false);
}
else if (page?.HasMore == false)
{
// The whole profile is on the first page, saved just now. For an account «Storico in corso» its history
// is complete: the engine closes it, with its line (RecipeOutcome.BackfillFinished). An account that
// keeps only the new posts had no history to announce.
await Env.Storage.Accounts.SetBackfillAsync(account.Id, null, Env.Clock.Now, run.Ct).ConfigureAwait(false);
run.Finished = account.State == AccountState.Backfilling;
}
return (items, tail == SaveOutcome.Created, false);
}
var fillGap = CollectionWriter.Wants(account, EntryKind.Post) && (account.BackfillCursor is not null || account.BackfillCompletedAt is not null);
return (items, tail == SaveOutcome.Created, fillGap);
}
/// <summary>
/// The second half of the profile check, once the backfill has started or ended: when the first page ends with
/// posts Febe did not have (Febe off for days, the account paused or unreachable), the posts between it and the
/// known ones would be fetched by nothing else. The check goes on down the grid, one counted page each and at most
/// <see cref="RecipeOptions.GapPagesPerCheck"/>, until it meets known posts; the rest waits for the next check from
/// the stored point. A refused permit, the end of the session or too many files waiting for download leave the
/// gap for later: the check itself is done.
/// </summary>
private async Task FillGapAsync(RecipeRun run, Account account, IReadOnlyList<CapturedRead> reads, IReadOnlyList<ContentItem> firstPage, bool tailIsNew)
{
var walk = ProfileWalk.Gap;
var settings = Env.Storage.Settings;
var keys = new[] { walk.CursorKey(account.Id), walk.MarkKey(account.Id), walk.KnownBeforeKey(account.Id) };
var storedCursor = await settings.GetAsync(keys[0], run.Ct).ConfigureAwait(false);
var stored = storedCursor is null ? null : ResumePoint.Parse(storedCursor, await settings.GetAsync(keys[1], run.Ct).ConfigureAwait(false));
var before = ParseTime(await settings.GetAsync(keys[2], run.Ct).ConfigureAwait(false));
async Task ClearAsync() => await settings.SetManyAsync(keys.ToDictionary(k => k, _ => (string?)null), run.Ct).ConfigureAwait(false);
if (WalkPage(reads, account, walk, ownAnswer: false)?.HasMore == false)
{
if (stored is not null || before is not null)
{
await ClearAsync().ConfigureAwait(false); // the whole grid is on the first page, read just now
}
return;
}
// Known posts newer than the end of an older gap still open further down do not end the walk: one walk fills
// both. A stored point also stops only at posts older than its last item (those before it were saved by it).
var floor = Earliest(before, stored?.LastAt);
IReadOnlyList<CapturedRead> start = reads;
ResumePoint? catchUpTo = null;
var pages = 0;
var ownStart = false;
if (!tailIsNew)
{
if (stored is null)
{
return;
}
if (Env.SiteMap.HasPage(walk.ResumePage))
{
await run.PermitAsync(ActionKind.BackfillPage, account.Id, opensPage: true).ConfigureAwait(false);
var mark = Env.Capture.Mark();
await run.OpenAsync(walk.ResumePage, Values(("username", account.Username), ("cursor", stored.Cursor))).ConfigureAwait(false);
start = await WaitAndSettleAsync(run, mark, r => IsWalkPage(r, account, walk)).ConfigureAwait(false);
if (!run.Judge(start, r => IsWalkPage(r, account, walk), null))
{
return; // an empty page, counted: the gap stays for the next check
}
pages = 1;
ownStart = true;
}
else
{
catchUpTo = stored; // scroll down from the first page, on screen, back to the stored point
}
}
try
{
var (end, _, _) = await WalkPagesAsync(run, account, walk, start, new WalkRules
{
CatchUpTo = catchUpTo,
PagesRead = pages,
StartsFromOwnAnswer = ownStart,
MaxPages = Math.Max(1, Env.Options.GapPagesPerCheck),
Stop = floor is { } f ? new KnownStop(true, f) : KnownStop.Any,
CheckDownloads = true,
AnnounceSuspension = false,
Processed = new HashSet<string>(firstPage.Select(i => i.Id), StringComparer.Ordinal),
Store = (cursor, last) => settings.SetManyAsync(
new Dictionary<string, string?>
{
[keys[0]] = cursor,
[keys[1]] = ResumePoint.MarkOf(last),
[keys[2]] = floor?.ToString("O", CultureInfo.InvariantCulture),
},
run.Ct),
}).ConfigureAwait(false);
if (end is WalkEnd.EndOfList or WalkEnd.Limit or WalkEnd.Known)
{
await ClearAsync().ConfigureAwait(false);
}
}
catch (RecipeStopException stop) when (stop.Status == RecipeStatus.Denied)
{
// No permit, the session is over, or too many files wait for download: the gap waits for the next check.
}
}
/// <summary>
/// Storico, Taggati and Reel (spec §6.3): one page per permit, resuming from the stored point and stopping at the
/// beginning of the profile. Without a resume page in the map (profileFrom, taggedFrom, reelsFrom are optional)
/// the walk scrolls from the top back to the stored point, each page counted by the Governor and saved on the
/// way, and only the pages after it count against the pages of the run (decision 76): never a silent restart. The
/// backfill stops while the account has more than 60 files waiting for download or the data budget is used up
/// (the engine checks too, the recipe must stop as well). Once a tab has been walked to the end, later runs only
/// check its first page and stop at known content.
/// </summary>
private async Task<RecipeStatus> WalkAsync(RecipeRun run, ProfileWalk walk)
{
var account = await LoadAccountAsync(run).ConfigureAwait(false);
if (await WalkStartAsync(run, account, walk).ConfigureAwait(false) is not { } start)
{
return RecipeStatus.Completed;
}
var (cursor, checkOnly) = start;
var settings = Env.Storage.Settings;
var target = cursor is null ? null : ResumePoint.Parse(cursor, await settings.GetAsync(walk.MarkKey(account.Id), run.Ct).ConfigureAwait(false));
if (!checkOnly)
{
await EnsureBackfillAllowedAsync(run, account).ConfigureAwait(false);
}
var maxPages = Math.Max(1, run.Request.MaxPages ?? Env.Options.PagesPerRun);
var parameters = Values(("username", account.Username));
IReadOnlyList<CapturedRead> reads;
var pages = 0;
ResumePoint? catchUpTo = null;
var ownStart = true; // a resume page or a tab; not the profile as opened
if (target is not null && Env.SiteMap.HasPage(walk.ResumePage))
{
// Resume where the last run stopped: the page of the site that starts after the stored point.
await run.PermitAsync(ActionKind.BackfillPage, account.Id, opensPage: true).ConfigureAwait(false);
var mark = Env.Capture.Mark();
await run.OpenAsync(walk.ResumePage, Values(("username", account.Username), ("cursor", target.Cursor))).ConfigureAwait(false);
reads = await WaitAndSettleAsync(run, mark, r => IsWalkPage(r, account, walk)).ConfigureAwait(false);
if (!run.Judge(reads, r => IsWalkPage(r, account, walk), Env.SiteMap.BuildPath(SitePages.Profile, parameters)))
{
throw RecipeRun.Stop(RecipeStatus.Completed, EmptyPageMessage);
}
pages++;
}
else
{
(account, reads) = await OpenProfileAsync(run, account, waitForGrid: walk.IsGrid).ConfigureAwait(false);
ownStart = walk.TabTarget is not null;
if (walk.TabTarget is { } tab)
{
await run.PermitAsync(ActionKind.BackfillPage, account.Id).ConfigureAwait(false);
var mark = Env.Capture.Mark();
await run.ClickAsync(tab, parameters).ConfigureAwait(false);
await Env.Driver.WaitAfterLoadAsync(run.Ct).ConfigureAwait(false);
reads = await WaitAndSettleAsync(run, mark, r => IsWalkPage(r, account, walk)).ConfigureAwait(false);
if (!run.Judge(reads, r => IsWalkPage(r, account, walk), null))
{
throw RecipeRun.Stop(RecipeStatus.Completed, EmptyPageMessage);
}
if (target is null)
{
pages++;
}
}
catchUpTo = target;
}
var (end, caughtUp, catchUpPages) = await WalkPagesAsync(run, account, walk, reads, new WalkRules
{
CatchUpTo = catchUpTo,
PagesRead = pages,
StartsFromOwnAnswer = ownStart,
MaxPages = maxPages,
Stop = checkOnly ? KnownStop.Any : KnownStop.Never,
CheckDownloads = !checkOnly,
Store = (next, last) => StoreWalkPointAsync(run, account, walk, next, last),
}).ConfigureAwait(false);
await FinishWalkAsync(run, account, walk, end, checkOnly, caughtUp, catchUpPages).ConfigureAwait(false);
return RecipeStatus.Completed;
}
/// <summary>
/// The end of a walk of Storico, Taggati or Reel, whatever turned its pages: at the beginning of the profile, or a
/// history limit (not on a check-only walk), the walk is complete and its point cleared; when the stored point was
/// not found again by scrolling, the timeline says so and the point stays.
/// </summary>
private async Task FinishWalkAsync(RecipeRun run, Account account, ProfileWalk walk, WalkEnd end, bool checkOnly, bool caughtUp, int catchUpPages)
{
var settings = Env.Storage.Settings;
if (end == WalkEnd.EndOfList || (end == WalkEnd.Limit && !checkOnly))
{
run.Finished = true;
if (walk.IsGrid)
{
// «Storico completato» in Attività, and the new state of the account, are the engine's: one line per
// history, with its numbers (SessionComposer, from RecipeOutcome.BackfillFinished).
await Env.Storage.Accounts.SetBackfillAsync(account.Id, null, Env.Clock.Now, run.Ct).ConfigureAwait(false);
await settings.SetAsync(walk.MarkKey(account.Id), null, run.Ct).ConfigureAwait(false);
}
else
{
await settings.SetManyAsync(
new Dictionary<string, string?>
{
[walk.CursorKey(account.Id)] = null,
[walk.MarkKey(account.Id)] = null,
[walk.CompletedKey(account.Id)] = Env.Clock.Now.ToString("O", CultureInfo.InvariantCulture),
},
run.Ct).ConfigureAwait(false);
}
}
else if (!caughtUp)
{
await Writer.EventAsync(
"ripresa-scorrendo",
$"«{Name(run.Request.Kind)}» di {account.Username}: {ItalianText.Number(catchUpPages + 1)} pagine lette scorrendo senza ritrovare ancora il punto in cui si era fermato. Il punto resta salvato: si riprende da lì alla prossima sessione.",
EventSeverity.Info,
account.Id,
run.Session.SessionId,
run.Ct).ConfigureAwait(false);
}
}
/// <summary>
/// Saves the pages of a walk, from the page on screen: first back to the stored point when there is one (pages
/// read and saved, not counted against <see cref="WalkRules.MaxPages"/>, no point stored before it is reached),
/// then one page per permit until the end of the profile, a history limit, known content (when the rules say so)
/// or the pages of the run. Returns how it ended, whether the stored point was reached, and the pages scrolled to
/// get back there.
/// </summary>
private async Task<(WalkEnd End, bool CaughtUp, int CatchUpPages)> WalkPagesAsync(RecipeRun run, Account account, ProfileWalk walk, IReadOnlyList<CapturedRead> reads, WalkRules rules)
{
var pending = rules.CatchUpTo;
var pages = rules.PagesRead;
var catchUp = 0;
var ownAnswer = rules.StartsFromOwnAnswer;
IReadOnlySet<string>? seen = null; // the first page: only what its own answers bring
while (true)
{
var items = PageItems(reads, account, walk);
var page = WalkPage(reads, account, walk, ownAnswer, seen);
if (pending is not null && pending.IsReachedBy(page, items, i => walk.IsChronological && !IsPinned(i, items, walk)))
{
pending = null; // this page holds the stored point: from here on the walk goes on as usual
}
var (reachedKnown, reachedLimit) = await SaveWalkPageAsync(run, account, walk, items, rules.Processed, pending is null ? rules.Stop : KnownStop.Never).ConfigureAwait(false);
if (reachedLimit)
{
return (WalkEnd.Limit, pending is null, catchUp);
}
if (page?.HasMore == false)
{
return (WalkEnd.EndOfList, pending is null, catchUp);
}
if (reachedKnown)
{
return (WalkEnd.Known, true, catchUp);
}
if (pending is null && page?.Cursor is { } next)
{
await rules.Store(next, items.LastOrDefault()).ConfigureAwait(false);
}
if (pending is null && pages >= rules.MaxPages)
{
return (WalkEnd.Pages, true, catchUp);
}
if (rules.CheckDownloads)
{
await EnsureBackfillAllowedAsync(run, account, rules.AnnounceSuspension).ConfigureAwait(false);
}
await run.PermitAsync(ActionKind.BackfillPage, account.Id).ConfigureAwait(false);
var counted = pending is null;
// Only items of the walk not read yet say the next page came: an answer about something else that arrives
// meanwhile (a post of another account, the collections), or one that brings again a post already read
// (the page of that post), neither counts as it nor ends the walk with its pagination.
reads = await ScrollForPageAsync(run, r => r.Contents.Any(c => IsWalkItem(c, account, walk) && !rules.Processed.Contains(c.Id))).ConfigureAwait(false);
ownAnswer = true;
seen = rules.Processed;
if (counted)
{
pages++;
}
else
{
catchUp++;
}
if (reads.Count == 0)
{
return (WalkEnd.NoData, pending is null, catchUp); // nothing came in after the bursts (counted as an empty page)
}
}
}
/// <summary>
/// Saves the new items of a walk page. Returns whether known content that ends the walk was met and whether a
/// history limit of the account was reached (from a date, last N posts).
/// </summary>
private async Task<(bool ReachedKnown, bool ReachedLimit)> SaveWalkPageAsync(RecipeRun run, Account account, ProfileWalk walk, IReadOnlyList<ContentItem> page, HashSet<string> processed, KnownStop stop)
{
foreach (var item in page.Where(c => processed.Add(c.Id)).ToList())
{
var pinned = IsPinned(item, page, walk);
if (walk.Kind != EntryKind.Tagged && !pinned)
{
if (account.Limits.Depth == HistoryDepth.FromDate && account.Limits.FromDate is { } from && item.TakenAt < from)
{
return (false, true);
}
if (account.Limits.Depth == HistoryDepth.LastPosts && account.Limits.LastPosts is { } last
&& await Env.Storage.Entries.ListByAccountAsync(account.Id, new Storage.EntryListOptions { Kind = EntryKind.Post, Limit = last }, run.Ct).ConfigureAwait(false) is { } kept
&& kept.Count >= last)
{
return (false, true);
}
}
// What the Reel tab gives is a reel, whatever the response says (spec §7.2, «Cosa scaricare prima»).
var content = walk.MarksReels && !item.IsReel ? item with { IsReel = true } : item;
var saved = await Writer.SaveAsync(account, content, walk.Kind, run.Session.SessionId, run.Ct).ConfigureAwait(false);
if (saved.Outcome == SaveOutcome.Created)
{
run.NewEntries++;
run.AccountsWithNewPosts.Add(account.Id);
}
else if (saved.Outcome == SaveOutcome.Known && !pinned && stop.Stops(item))
{
return (true, false);
}
}
return (false, false);
}
/// <summary>The stored point of a walk: its cursor, and its last item to find it again by scrolling.</summary>
private async Task StoreWalkPointAsync(RecipeRun run, Account account, ProfileWalk walk, string cursor, ContentItem? last)
{
if (walk.IsGrid)
{
await Env.Storage.Accounts.SetBackfillAsync(account.Id, cursor, null, run.Ct).ConfigureAwait(false);
await Env.Storage.Settings.SetAsync(walk.MarkKey(account.Id), ResumePoint.MarkOf(last), run.Ct).ConfigureAwait(false);
}
else
{
await Env.Storage.Settings.SetManyAsync(
new Dictionary<string, string?>
{
[walk.CursorKey(account.Id)] = cursor,
[walk.MarkKey(account.Id)] = ResumePoint.MarkOf(last),
},
run.Ct).ConfigureAwait(false);
}
}
/// <summary>The posts of a walk page: of the account (tagged: of others), never a story or a collection item.</summary>
private static IReadOnlyList<ContentItem> PageItems(IReadOnlyList<CapturedRead> reads, Account account, ProfileWalk walk) =>
Contents(reads, c => IsWalkItem(c, account, walk));
/// <summary>An item of a walk: a post of the account (tagged: of another account), never a story or a collection item.</summary>
private static bool IsWalkItem(ContentItem item, Account account, ProfileWalk walk) =>
IsPostLike(item) && (walk.Kind == EntryKind.Tagged ? !IsOwnedBy(item, account) : IsOwnedBy(item, account));
/// <summary>An answer that brings items of the walk.</summary>
private static bool CarriesWalk(ReadResult result, Account account, ProfileWalk walk) =>
result.Contents.Any(c => IsWalkItem(c, account, walk));
/// <summary>
/// An answer to a gesture of the walk (a resume page, a tab) that is a page of it on its own: it brings items of the
/// walk, or it is an empty page of it (<see cref="IsEmptyWalkPage"/>).
/// </summary>
private static bool IsWalkPage(ReadResult result, Account account, ProfileWalk walk) =>
CarriesWalk(result, account, walk) || IsEmptyWalkPage(result, account, walk, ownAnswer: true);
/// <summary>
/// The end of a tab with nothing in it: a pagination and no item. With the profile of the account (the test site
/// sends it with every page of the tab), when nothing else comes with it, or — on the grid — when that profile says
/// it has no posts (the test site sends the collections with the first page of the grid). A bare empty answer only
/// when it answers a gesture of the walk (<paramref name="ownAnswer"/>): on Instagram the collections of an account
/// that has none are an empty connection with «has_next_page: false», as an empty grid would be, and they come
/// with the profile too.
/// </summary>
private static bool IsEmptyWalkPage(ReadResult result, Account account, ProfileWalk walk, bool ownAnswer) =>
result.Page is not null && result.Shape switch
{
ReadShape.Empty => ownAnswer,
ReadShape.Recognized => ProfileIn(result, account) is { } profile
&& ((result.Contents.Count == 0 && result.Highlights.Count == 0)
|| (walk.IsGrid && profile.PostCount == 0 && !result.Contents.Any(IsPostLike))),
_ => false,
};
/// <summary>
/// The pagination of a walk page: from the answer that brought the most items of the walk not read before
/// (<paramref name="seen"/>: the pages already read); when none did, from the last empty page of the walk
/// (<paramref name="ownAnswer"/>: the reads answer a gesture of the walk, <see cref="IsEmptyWalkPage"/>). Never from
/// an answer about something else, nor from one that only brings again a post already read: on Instagram the page
/// of a post says «more_available: false» of its single post and the collections say «has_next_page» of
/// themselves (HAR of 21/10/2026), and either would end the walk, or mark the history complete, where the grid
/// goes on.
/// </summary>
private static PageInfo? WalkPage(IReadOnlyList<CapturedRead> reads, Account account, ProfileWalk walk, bool ownAnswer, IReadOnlySet<string>? seen = null) =>
PageOf(reads, r => r.Contents.Where(c => IsWalkItem(c, account, walk)).Select(c => c.Id), r => IsEmptyWalkPage(r, account, walk, ownAnswer), seen);
/// <summary>
/// The pagination of a page of a list (a profile tab, the feed, the following list): from the answer that brings
/// the most items of the list not met before — in <paramref name="seen"/>, or in an earlier answer of the same page
/// with a pagination of its own — the last one on a tie; when none brings any, from the last empty page. An answer
/// that only brings again items already met, or fewer new ones than another (the page of a single post next to the
/// grid), never gives it.
/// </summary>
private static PageInfo? PageOf(IEnumerable<CapturedRead> reads, Func<ReadResult, IEnumerable<string>> itemIds, Func<ReadResult, bool> isEmptyPage, IReadOnlySet<string>? seen = null)
{
var met = new HashSet<string>(StringComparer.Ordinal);
PageInfo? withItems = null;
var most = 0;
PageInfo? empty = null;
foreach (var result in reads.Select(r => r.Result).Where(r => r.Page is not null))
{
var carried = 0;
var fresh = 0;
foreach (var id in itemIds(result))
{
carried++;
if (met.Add(id) && seen?.Contains(id) != true)
{
fresh++;
}
}
if (fresh > 0)
{
if (fresh >= most)
{
most = fresh;
withItems = result.Page;
}
}
else if (carried == 0 && isEmptyPage(result))
{
empty = result.Page;
}
}
return withItems ?? empty;
}
/// <summary>
/// The profile of the account as all the answers of the page describe it: the same profile can come in several
/// of them (the profile itself, the author of the posts, the owner of the collections), each with part of its
/// facts. The best described one is completed with what the others say; of the pictures, the largest.
/// </summary>
private static ProfileInfo? AccountProfile(IReadOnlyList<CapturedRead> reads, Account account)
{
if (FindProfile(reads, account) is not { } best)
{
return null;
}
foreach (var other in reads.SelectMany(r => r.Result.Profiles).Where(p => p.Id == best.Id).ToList())
{
best = best with
{
DisplayName = best.DisplayName ?? other.DisplayName,
IsPrivate = best.IsPrivate ?? other.IsPrivate,
IsFollowed = best.IsFollowed ?? other.IsFollowed,
PostCount = best.PostCount ?? other.PostCount,
Picture = other.Picture is { } picture && (best.Picture is null || picture.Area > best.Picture.Area) ? picture : best.Picture,
LatestStoryAt = best.LatestStoryAt ?? other.LatestStoryAt,
IsViewer = best.IsViewer || other.IsViewer,
};
}
return best;
}
/// <summary>Pinned on top of the profile: the vocabulary says so, or (newest first) the order of the page does.</summary>
private bool IsPinned(ContentItem item, IReadOnlyList<ContentItem> page, ProfileWalk walk) =>
walk.IsChronological ? LooksPinned(item, page) : item.Pinned;
/// <summary>
/// «Lo storico non corre più veloce dei download» (spec §6.3): stops while the account has more than 60 files waiting
/// or the data budget is used up.
/// </summary>
private async Task EnsureBackfillAllowedAsync(RecipeRun run, Account account, bool announce = true)
{
var pending = await Env.Storage.Media.CountPendingDownloadsAsync(account.Id, run.Ct).ConfigureAwait(false);
if (pending > Env.Options.MaxPendingDownloadsForBackfill)
{
if (announce)
{
await Writer.EventAsync("storico-sospeso", $"Storico di {account.Username} sospeso: {ItalianText.Number(pending)} file aspettano di essere scaricati.", EventSeverity.Info, account.Id, run.Session.SessionId, run.Ct).ConfigureAwait(false);
}
throw RecipeRun.Stop(RecipeStatus.Denied, $"Storico sospeso: {ItalianText.Number(pending)} file di {account.Username} aspettano di essere scaricati (più di {Env.Options.MaxPendingDownloadsForBackfill}).");
}
if (Env.DataBudget is { } budget && await budget.RemainingTodayAsync(run.Ct).ConfigureAwait(false) <= 0)
{
throw RecipeRun.Stop(RecipeStatus.Denied, "Storico sospeso: il tetto giornaliero dei dati scaricati è raggiunto.");
}
}
/// <summary>
/// Opens the profile page (1 counted action) and reads its state: missing profile (when the map has its marker), a
/// name now used by another account, private profile not followed (rule 6: not touched). The profile, the posts
/// and the collections may come in different answers, and not together: after the profile the page is left to
/// settle, and with <paramref name="waitForGrid"/> the first page of posts is waited for too. Returns the updated
/// account and everything the page received.
/// </summary>
private async Task<(Account Account, IReadOnlyList<CapturedRead> Reads)> OpenProfileAsync(RecipeRun run, Account account, bool waitForGrid = false)
{
var parameters = Values(("username", account.Username));
await run.PermitAsync(ActionKind.ProfileOpen, account.Id, opensPage: true).ConfigureAwait(false);
var mark = Env.Capture.Mark();
await run.OpenAsync(SitePages.Profile, parameters).ConfigureAwait(false);
if (Env.SiteMap.IsTargetConfigured(SiteTargets.ProfileNotFound)
&& (await run.FindAsync(SiteTargets.ProfileNotFound, parameters).ConfigureAwait(false)).Found)
{
await MarkUnreachableAsync(run, account, UnreachableReason.NotFound).ConfigureAwait(false);
throw RecipeRun.Stop(RecipeStatus.AccountUnreachable, $"Il profilo {account.Username} non esiste o non è più disponibile.");
}
var reads = await WaitAndSettleAsync(run, mark, r => ProfileIn(r, account) is not null).ConfigureAwait(false);
if (!run.Judge(reads, r => ProfileIn(r, account) is not null, Env.SiteMap.BuildPath(SitePages.Profile, parameters)))
{
throw RecipeRun.Stop(RecipeStatus.Completed, EmptyPageMessage);
}
account = await ApplyProfileStateAsync(run, account, AccountProfile(reads, account)!).ConfigureAwait(false);
if (waitForGrid)
{
reads = await WaitForGridAsync(run, mark, account, reads).ConfigureAwait(false);
}
return (account, reads);
}
/// <summary>
/// What the profile just read says of the account, whatever brought it (the page or a request): a name now used by
/// another account, private profile not followed (rule 6: not touched; both stop the run as
/// <see cref="RecipeStatus.AccountUnreachable"/>), the facts of the profile, an unreachable account reachable again.
/// Returns the updated account.
/// </summary>
private async Task<Account> ApplyProfileStateAsync(RecipeRun run, Account account, ProfileInfo profile)
{
if (account.InstagramId is { } known && profile.Id != known)
{
await MarkUnreachableAsync(run, account, UnreachableReason.Renamed).ConfigureAwait(false);
throw RecipeRun.Stop(RecipeStatus.AccountUnreachable, $"Il nome {account.Username} ora appartiene a un altro profilo: l'account resta com'era.");
}
account = await Writer.ApplyProfileAsync(account, profile, run.Ct).ConfigureAwait(false);
if (profile.IsPrivate == true && profile.IsFollowed == false)
{
await MarkUnreachableAsync(run, account, UnreachableReason.PrivateNotFollowed).ConfigureAwait(false);
throw RecipeRun.Stop(RecipeStatus.AccountUnreachable, $"{account.Username} è un profilo privato che non segui: non si tocca.");
}
if (account.State == AccountState.Unreachable)
{
var state = account.BackfillCursor is not null && account.BackfillCompletedAt is null ? AccountState.Backfilling : AccountState.UpToDate;
await Env.Storage.Accounts.SetStateAsync(account.Id, state, Env.Clock.Now, null, run.Ct).ConfigureAwait(false);
await Writer.EventAsync("account-raggiungibile", $"{account.Username} è di nuovo raggiungibile.", EventSeverity.Info, account.Id, run.Session.SessionId, run.Ct).ConfigureAwait(false);
account = await Env.Storage.Accounts.GetAsync(account.Id, run.Ct).ConfigureAwait(false) ?? account;
}
return account;
}
/// <summary>
/// Waits for <paramref name="expected"/> after <paramref name="mark"/>, then lets the page settle and returns
/// everything that arrived since the mark: what one page shows can come in several answers, the expected one not
/// always last, and stopping at the first that matches would lose the others. When the expected data does not
/// come, returns what came, to be judged.
/// </summary>
private async Task<IReadOnlyList<CapturedRead>> WaitAndSettleAsync(RecipeRun run, long mark, Func<ReadResult, bool> expected)
{
var reads = await run.WaitForDataAsync(mark, expected).ConfigureAwait(false);
if (!reads.Any(r => r.Result.Shape == ReadShape.Recognized && expected(r.Result)))
{
return reads;
}
await run.SettleAsync().ConfigureAwait(false);
return Env.Capture.Since(mark);
}
/// <summary>
/// The first page of the grid, when it did not come with the profile: the page may ask for it only after drawing
/// the profile, later than it takes to settle. Waits for posts of the account or an empty grid that is surely its
/// own (<see cref="IsEmptyWalkPage"/>: a bare empty answer may be the collections), then lets the page settle again.
/// Nothing is judged here (the profile came): a profile without posts that does not say so only costs the wait.
/// </summary>
private async Task<IReadOnlyList<CapturedRead>> WaitForGridAsync(RecipeRun run, long mark, Account account, IReadOnlyList<CapturedRead> reads)
{
var walk = ProfileWalk.Posts;
bool Arrived(IReadOnlyList<CapturedRead> list) => PageItems(list, account, walk).Count > 0 || WalkPage(list, account, walk, ownAnswer: false) is not null;
if (Arrived(reads))
{
return reads;
}
var arrived = await Env.Capture.WaitAsync(mark, list => run.Signal is not null || Arrived(list), Env.Options.DataTimeout, Env.Options.Poll, run.Ct).ConfigureAwait(false);
run.ThrowIfSignal();
if (!Arrived(arrived))
{
return arrived;
}
await run.SettleAsync().ConfigureAwait(false);
return Env.Capture.Since(mark);
}
/// <summary>
/// Scrolls with bursts until a new page of data that satisfies <paramref name="expected"/> arrives, at most
/// <see cref="RecipeOptions.BurstsPerPage"/> bursts. Only the expected data ends the bursts: a response the page
/// fetches for something else (a background poll, a bare status) says nothing about the next page. When the
/// bursts are used up without it, the responses received are judged: an unknown shape is «formato non
/// riconosciuto», anything else an empty page. Returns the reads of that page (empty when none came).
/// </summary>
private async Task<IReadOnlyList<CapturedRead>> ScrollForPageAsync(RecipeRun run, Func<ReadResult, bool> expected)
{
var mark = Env.Capture.Mark();
var seen = mark;
for (var burst = 0; burst < Env.Options.BurstsPerPage; burst++)
{
await Env.Driver.ScrollBurstAsync(run.Ct).ConfigureAwait(false);
run.ThrowIfSignal();
if (Env.Capture.Pending > 0 || Env.Capture.Mark() != seen)
{
await run.SettleAsync().ConfigureAwait(false);
seen = Env.Capture.Mark();
}
var reads = Env.Capture.Since(mark);
var page = reads.Where(r => r.Result.Shape == ReadShape.Recognized && expected(r.Result)).ToList();
if (page.Count > 0)
{
run.Judge(reads, expected, null);
return page;
}
}
run.Judge(Env.Capture.Since(mark), expected, null);
return [];
}
/// <summary>Outcome message of a page that arrived without data (three in a row raise a throttle signal).</summary>
private const string EmptyPageMessage = "La pagina è arrivata vuota: nessun dato da leggere.";
private static DateTimeOffset? ParseTime(string? value) =>
DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var time) ? time : null;
private static DateTimeOffset? Earliest(DateTimeOffset? a, DateTimeOffset? b) =>
a is null ? b : b is null ? a : a < b ? a : b;
}