«Aggiungi account» ha una sola casella: mentre Alberto scrive compaiono subito i suggerimenti locali, poi i profili di Instagram con la foto, come chiesto in #37. Il browser di Febe scrive nella casella «Cerca» di Instagram con tasti veri da una mappa chiusa (mai Invio, Tab o Esc), controlla prima il fuoco, legge la risposta della pagina e non clicca mai un risultato. La ricerca ha una categoria propria nel Governatore, con tetti larghi che non consumano quelli della raccolta, e si dà il turno con il motore sull'unica pagina; a fine sessione la pagina torna comunque al riposo. Le foto passano dal CDN con un permesso ciascuna e restano solo in memoria. Le risposte di Alberto del 15 novembre (controllo del fuoco, turni, evento requestWillBeSent, niente foto dei seguiti per ora) sono in §6.1 e in docs/decisioni.md. Su Instagram vero la ricerca parte quando i bersagli search-open e search-box saranno raccolti dal vivo. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
406 lines
15 KiB
C#
406 lines
15 KiB
C#
using System.Globalization;
|
|
using System.Text.Json.Nodes;
|
|
|
|
namespace Febe.SitoDiProva;
|
|
|
|
/// <summary>
|
|
/// The JSON the pages fetch, in the site's own synthetic format (LEGGIMI.md). It is what the response tap captures
|
|
/// and what the readers of <c>lettori.prova.json</c> read. Keys are Italian and the shape is ours: it does not
|
|
/// imitate Instagram. One exception: the answer of the search box (<see cref="Search"/>) has the shape of the cleaned
|
|
/// Instagram sample, so the search of Febe is exercised end to end on the real format (issue #37).
|
|
/// </summary>
|
|
public sealed class DataApi(World world, CdnSigner signer, SiteControl control, Func<string> cdnBase, TimeProvider time)
|
|
{
|
|
public const int FeedPageSize = 6;
|
|
public const int GridPageSize = 12;
|
|
public const int FollowingPageSize = 10;
|
|
|
|
private DateTimeOffset Now => time.GetUtcNow();
|
|
|
|
/// <summary>Home: the connected account, the stories bar and the first page of the feed.</summary>
|
|
public JsonObject Home()
|
|
{
|
|
var feed = world.Feed();
|
|
return new JsonObject
|
|
{
|
|
["io"] = ProfileLite(world.Viewer),
|
|
["barra"] = new JsonArray([.. world.StoryBar(Now).Select(a => (JsonNode)Profile(a))]),
|
|
["contenuti"] = new JsonArray([.. feed.Take(FeedPageSize).Select(p => (JsonNode)Post(p, inFeed: true))]),
|
|
["pagina"] = Page(feed.Count, 0, FeedPageSize),
|
|
};
|
|
}
|
|
|
|
public JsonObject HomeMore(string? after)
|
|
{
|
|
var feed = world.Feed();
|
|
var offset = ParseCursor(after);
|
|
return new JsonObject
|
|
{
|
|
["contenuti"] = new JsonArray([.. feed.Skip(offset).Take(FeedPageSize).Select(p => (JsonNode)Post(p, inFeed: true))]),
|
|
["pagina"] = Page(feed.Count, offset, FeedPageSize),
|
|
};
|
|
}
|
|
|
|
/// <summary>A profile page: profile, highlights (first page only) and a page of the tab. Null when it does not exist.</summary>
|
|
public JsonObject? Profile(string username, ProfileTab tab, string? after)
|
|
{
|
|
var account = world.FindByUsername(username);
|
|
if (account is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var result = new JsonObject { ["profilo"] = Profile(account) };
|
|
if (account.IsPrivate && !account.FollowedByViewer)
|
|
{
|
|
result["contenuti"] = new JsonArray();
|
|
result["pagina"] = new JsonObject { ["altro"] = false, ["dopo"] = null };
|
|
return result;
|
|
}
|
|
|
|
var offset = ParseCursor(after);
|
|
if (tab == ProfileTab.Posts && offset == 0)
|
|
{
|
|
result["raccolte"] = new JsonArray([.. account.Highlights.Select(h => (JsonNode)Highlight(h, withItems: false))]);
|
|
}
|
|
|
|
var grid = world.Grid(account, tab);
|
|
result["contenuti"] = new JsonArray([.. grid.Skip(offset).Take(GridPageSize).Select(p => (JsonNode)Post(p, inFeed: false))]);
|
|
result["pagina"] = Page(grid.Count, offset, GridPageSize);
|
|
return result;
|
|
}
|
|
|
|
public JsonObject? ProfileMore(string username, ProfileTab tab, string? after)
|
|
{
|
|
var account = world.FindByUsername(username);
|
|
if (account is null || (account.IsPrivate && !account.FollowedByViewer))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var grid = world.Grid(account, tab);
|
|
var offset = ParseCursor(after);
|
|
return new JsonObject
|
|
{
|
|
["contenuti"] = new JsonArray([.. grid.Skip(offset).Take(GridPageSize).Select(p => (JsonNode)Post(p, inFeed: false))]),
|
|
["pagina"] = Page(grid.Count, offset, GridPageSize),
|
|
};
|
|
}
|
|
|
|
public JsonObject? SinglePost(string code)
|
|
{
|
|
var post = world.FindPost(code);
|
|
if (post is null || (post.Owner.IsPrivate && !post.Owner.FollowedByViewer))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return new JsonObject { ["contenuto"] = Post(post, inFeed: false) };
|
|
}
|
|
|
|
/// <summary>All the active stories of an account (the page receives the whole list in advance), and who comes next.</summary>
|
|
public JsonObject? Stories(string username)
|
|
{
|
|
var account = world.FindByUsername(username);
|
|
if (account is null || (account.IsPrivate && !account.FollowedByViewer))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var bar = world.StoryBar(Now);
|
|
var index = bar.ToList().IndexOf(account);
|
|
var next = index >= 0 && index + 1 < bar.Count ? bar[index + 1].Username : null;
|
|
return new JsonObject
|
|
{
|
|
["autore"] = ProfileLite(account),
|
|
["storie"] = new JsonArray([.. account.Stories.Where(s => s.ExpiresAt > Now).Select(s => (JsonNode)Story(s))]),
|
|
["successivo"] = next,
|
|
};
|
|
}
|
|
|
|
public JsonObject? HighlightItems(string id)
|
|
{
|
|
var highlight = world.FindHighlight(id);
|
|
if (highlight is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var siblings = highlight.Owner.Highlights;
|
|
var index = siblings.IndexOf(highlight);
|
|
return new JsonObject
|
|
{
|
|
["raccolta"] = Highlight(highlight, withItems: true),
|
|
["successiva"] = index + 1 < siblings.Count ? siblings[index + 1].Id : null,
|
|
};
|
|
}
|
|
|
|
public JsonObject Following(string? after)
|
|
{
|
|
var list = world.Following;
|
|
var offset = ParseCursor(after);
|
|
return new JsonObject
|
|
{
|
|
["seguiti"] = new JsonArray([.. list.Skip(offset).Take(FollowingPageSize).Select(a => (JsonNode)Profile(a))]),
|
|
["pagina"] = Page(list.Count, offset, FollowingPageSize),
|
|
};
|
|
}
|
|
|
|
public static JsonObject EmptyPage() => new()
|
|
{
|
|
["contenuti"] = new JsonArray(),
|
|
["pagina"] = new JsonObject { ["altro"] = false, ["dopo"] = null },
|
|
};
|
|
|
|
/// <summary>A response in a shape the readers do not know: what a site that changed its format would send.</summary>
|
|
public static JsonObject UnknownShape() => new()
|
|
{
|
|
["risposta"] = new JsonObject
|
|
{
|
|
["voci"] = new JsonArray(
|
|
new JsonObject { ["chiave"] = "A1", ["valore"] = "un formato nuovo", ["dettagli"] = new JsonObject { ["x"] = 1 } },
|
|
new JsonObject { ["chiave"] = "B2", ["valore"] = "che nessun lettore conosce", ["dettagli"] = new JsonObject { ["x"] = 2 } }),
|
|
},
|
|
["versione"] = 2,
|
|
};
|
|
|
|
public static int ParseCursor(string? cursor) =>
|
|
cursor is { Length: > 1 } && cursor[0] == 'c' && int.TryParse(cursor.AsSpan(1), NumberStyles.Integer, CultureInfo.InvariantCulture, out var n) && n >= 0 ? n : 0;
|
|
|
|
public static string Cursor(int offset) => "c" + offset.ToString(CultureInfo.InvariantCulture);
|
|
|
|
/// <summary>
|
|
/// The answer of the search box: up to <see cref="SearchSize"/> profiles whose name or display name contains the text
|
|
/// (accents folded), the ones that start with it first. The shape and keys are those of the cleaned Instagram sample
|
|
/// «ricerca-profili» (PolarisSearchBoxRefetchableQuery), and only keys present there: no «is_private», no
|
|
/// «friendship_status», no echo of the text.
|
|
/// </summary>
|
|
public JsonObject Search(string text)
|
|
{
|
|
var wanted = Fold(text.Trim());
|
|
var users = new JsonArray();
|
|
if (wanted.Length > 0)
|
|
{
|
|
var found = world.Accounts
|
|
.Where(a => !a.IsViewer)
|
|
.Select(a => (Account: a, Name: Fold(a.Username), Display: Fold(a.DisplayName)))
|
|
.Where(x => x.Name.Contains(wanted, StringComparison.Ordinal) || x.Display.Contains(wanted, StringComparison.Ordinal))
|
|
.OrderBy(x => x.Name.StartsWith(wanted, StringComparison.Ordinal) || x.Display.StartsWith(wanted, StringComparison.Ordinal) ? 0 : 1)
|
|
.ThenBy(x => x.Name, StringComparer.Ordinal)
|
|
.Take(SearchSize)
|
|
.ToList();
|
|
var position = 0;
|
|
foreach (var (account, _, _) in found)
|
|
{
|
|
var small = Version(account.PictureKey, 150, 150, video: false)["indirizzo"]!.GetValue<string>();
|
|
var large = Version(account.PictureKey, 320, 320, video: false)["indirizzo"]!.GetValue<string>();
|
|
users.Add(new JsonObject
|
|
{
|
|
["position"] = position++,
|
|
["user"] = new JsonObject
|
|
{
|
|
["username"] = account.Username,
|
|
["is_verified"] = false,
|
|
["full_name"] = account.DisplayName,
|
|
["pk"] = account.Id,
|
|
["profile_pic_url"] = small,
|
|
["hd_profile_pic_url_info"] = new JsonObject { ["url"] = large },
|
|
["id"] = account.Id,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
|
|
return new JsonObject
|
|
{
|
|
["data"] = new JsonObject
|
|
{
|
|
["xdt_api__v1__fbsearch__topsearch_connection"] = new JsonObject
|
|
{
|
|
["hashtags"] = new JsonArray(),
|
|
["places"] = new JsonArray(),
|
|
["users"] = users,
|
|
},
|
|
},
|
|
["extensions"] = new JsonObject { ["is_final"] = true },
|
|
};
|
|
}
|
|
|
|
/// <summary>The «ricerche recenti» shown when the search panel opens: the site's own format, never read by Febe.</summary>
|
|
public JsonObject RecentSearches(IReadOnlyList<string> names) => new()
|
|
{
|
|
["recenti"] = new JsonArray([.. names.Select(n => (JsonNode)new JsonObject { ["voce"] = n })]),
|
|
};
|
|
|
|
/// <summary>Profiles in one search answer.</summary>
|
|
public const int SearchSize = 8;
|
|
|
|
private static string Fold(string text)
|
|
{
|
|
var decomposed = text.ToLowerInvariant().Normalize(System.Text.NormalizationForm.FormD);
|
|
var builder = new System.Text.StringBuilder(decomposed.Length);
|
|
foreach (var c in decomposed)
|
|
{
|
|
if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
|
|
{
|
|
builder.Append(c);
|
|
}
|
|
}
|
|
|
|
return builder.ToString();
|
|
}
|
|
|
|
private static JsonObject Page(int total, int offset, int size)
|
|
{
|
|
var more = offset + size < total;
|
|
return new JsonObject { ["altro"] = more, ["dopo"] = more ? Cursor(offset + size) : null };
|
|
}
|
|
|
|
private JsonObject ProfileLite(SiteAccount account) => new()
|
|
{
|
|
["numero"] = account.Id,
|
|
["nome"] = account.Username,
|
|
};
|
|
|
|
private JsonObject Profile(SiteAccount account)
|
|
{
|
|
var node = new JsonObject
|
|
{
|
|
["numero"] = account.Id,
|
|
["nome"] = account.Username,
|
|
["nome_visibile"] = account.DisplayName,
|
|
["riservato"] = account.IsPrivate,
|
|
["seguito"] = account.FollowedByViewer,
|
|
["pubblicazioni"] = account.Posts.Count,
|
|
["ritratto"] = new JsonArray(
|
|
Version(account.PictureKey, 160, 160, video: false),
|
|
Version(account.PictureKey, 320, 320, video: false)),
|
|
};
|
|
if (account.LatestStoryAt(Now) is { } latest && (!account.IsPrivate || account.FollowedByViewer))
|
|
{
|
|
node["ultima_storia"] = Date(latest);
|
|
}
|
|
|
|
return node;
|
|
}
|
|
|
|
private JsonObject Post(SitePost post, bool inFeed)
|
|
{
|
|
var node = new JsonObject
|
|
{
|
|
["numero"] = post.Id,
|
|
["sigla"] = post.Code,
|
|
["pubblicato"] = Date(post.PublishedAt),
|
|
["testo"] = post.Caption,
|
|
["fissato"] = post.Pinned && !inFeed,
|
|
["autore"] = ProfileLite(post.Owner),
|
|
};
|
|
if (post.Files.Count == 1)
|
|
{
|
|
AddFile(node, post.Files[0]);
|
|
}
|
|
else
|
|
{
|
|
// The feed carries only the first file of a series, with the declared count: the post is incomplete there
|
|
// and «Post singolo» completes it. Profile grids and post pages carry the whole series.
|
|
var parts = inFeed ? post.Files.Take(1) : post.Files;
|
|
node["parti"] = new JsonArray([.. parts.Select(f =>
|
|
{
|
|
var part = new JsonObject { ["numero"] = f.Id };
|
|
AddFile(part, f);
|
|
return (JsonNode)part;
|
|
})]);
|
|
node["parti_totali"] = post.Files.Count;
|
|
}
|
|
|
|
return node;
|
|
}
|
|
|
|
private JsonObject Story(SiteStory story)
|
|
{
|
|
var node = new JsonObject
|
|
{
|
|
["numero"] = story.Id,
|
|
["pubblicato"] = Date(story.PublishedAt),
|
|
["scade"] = Date(story.ExpiresAt),
|
|
["autore"] = ProfileLite(story.Owner),
|
|
};
|
|
AddFile(node, story.File);
|
|
return node;
|
|
}
|
|
|
|
private JsonObject Highlight(SiteHighlight highlight, bool withItems)
|
|
{
|
|
var node = new JsonObject
|
|
{
|
|
["numero"] = highlight.Id,
|
|
["titolo"] = highlight.Title,
|
|
["autore"] = ProfileLite(highlight.Owner),
|
|
["copertina"] = highlight.Items.Count > 0 ? Version(highlight.Items[0].File.MediaKey, 160, 284, video: false) : null,
|
|
};
|
|
if (withItems)
|
|
{
|
|
node["contenuti"] = new JsonArray([.. highlight.Items.Select(i =>
|
|
{
|
|
var item = new JsonObject
|
|
{
|
|
["numero"] = i.Id,
|
|
["pubblicato"] = Date(i.PublishedAt),
|
|
["autore"] = ProfileLite(i.Owner),
|
|
};
|
|
AddFile(item, i.File);
|
|
return (JsonNode)item;
|
|
})]);
|
|
}
|
|
|
|
return node;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The versions of a file. Images: a medium, the largest and a small one (in this order: the largest is not the
|
|
/// first). Videos: two renditions with audio and a larger one without audio (the reader must not pick it), plus
|
|
/// cover images.
|
|
/// </summary>
|
|
private void AddFile(JsonObject node, SiteFile file)
|
|
{
|
|
if (file.IsVideo)
|
|
{
|
|
node["filmati"] = new JsonArray(
|
|
Version(file.MediaKey, file.Width / 2, file.Height / 2, video: true, audio: true),
|
|
Version(file.MediaKey, file.Width, file.Height, video: true, audio: true),
|
|
Version(file.MediaKey, file.Width * 3 / 2, file.Height * 3 / 2, video: true, audio: false));
|
|
node["foto"] = new JsonArray(
|
|
Version(file.MediaKey, file.Width / 2, file.Height / 2, video: false),
|
|
Version(file.MediaKey, file.Width, file.Height, video: false));
|
|
node["durata"] = file.DurationSeconds;
|
|
}
|
|
else
|
|
{
|
|
node["foto"] = new JsonArray(
|
|
Version(file.MediaKey, file.Width / 2, file.Height / 2, video: false),
|
|
Version(file.MediaKey, file.Width, file.Height, video: false),
|
|
Version(file.MediaKey, file.Width / 4, file.Height / 4, video: false));
|
|
}
|
|
}
|
|
|
|
private JsonObject Version(string key, int width, int height, bool video, bool audio = true)
|
|
{
|
|
var size = $"{width.ToString(CultureInfo.InvariantCulture)}x{height.ToString(CultureInfo.InvariantCulture)}";
|
|
var path = video ? $"/m/{key}/{size}{(audio ? string.Empty : "-muto")}.mp4" : $"/m/{key}/{size}.jpg";
|
|
var node = new JsonObject
|
|
{
|
|
["indirizzo"] = cdnBase() + signer.Sign(path, Now + control.AddressLifetime),
|
|
["larghezza"] = width,
|
|
["altezza"] = height,
|
|
};
|
|
if (video)
|
|
{
|
|
node["audio"] = audio;
|
|
}
|
|
|
|
return node;
|
|
}
|
|
|
|
private static string Date(DateTimeOffset at) => at.UtcDateTime.ToString("yyyy-MM-dd'T'HH:mm:ss'Z'", CultureInfo.InvariantCulture);
|
|
}
|