Instagram: Febe legge le risposte vere (campioni dal HAR del 21/10)
verifica / controlli (push) Successful in 2m10s
verifica / prove (push) Successful in 26m16s
verifica / installatore (push) Successful in 5m39s
verifica / immagine (push) Successful in 28m39s

Febe non estraeva nulla da Instagram per due motivi. Instagram manda i
dati di storie e raccolte (GraphQL) come text/javascript, e la cattura
leggeva solo le risposte dichiarate JSON: le scartava tutte. E i file di
Instagram (lettori, bersagli) erano scheletri, quindi le ricette si
rifiutavano di girare.

Alberto ha mandato un HAR del suo browser e una guida. La guida descrive
richieste costruite a mano, vietate dalle regole 2-4, e un formato che
nel HAR non compare: non si usa. Dal HAR si è letta solo la forma; le
risposte utili sono diventate campioni grezzi fuori dal repository,
ripuliti e verificati con febe-sonda (tests/Febe.Tests/campioni/instagram).

Che cosa cambia:
- la cattura legge le risposte text/javascript chieste dalla pagina (mai
  gli script) e ogni blocco JSON incorporato nel documento della pagina,
  e avvisa quando un corpo atteso non arriverà;
- le risposte che non parlano di contenuti (promozioni, «storia vista»)
  si ignorano invece di valere come formato non riconosciuto;
- lettori.instagram.json è confermato, con sole chiavi presenti nei
  campioni; identificativi delle raccolte nella forma dell'indirizzo;
- bersagli.instagram.json ha le pagine viste nel traffico vero; i
  bersagli restano vuoti (H11): storie, raccolte, Taggati e Reel
  aspettano lo script tools/bersagli/raccogli-bersagli.js;
- le ricette del profilo reggono dati in risposte diverse e prendono la
  paginazione solo dalla risposta che porta i loro post; l'elenco dei
  seguiti scarta i profili suggeriti; «Tu» si riconosce anche dal solo
  identificativo dell'account collegato;
- il visualizzatore delle raccolte conta i contenuti (l'indirizzo non
  cambia), chiede il permesso prima di ogni contenuto e si chiude sempre;
- una ricetta che la configurazione non lascia girare è «Da configurare»:
  non è un guasto, non si sospende, non si pianifica; le sospensioni
  dovute ai file vecchi si azzerano una volta al cambio dei file;
- la barra laterale dice «Collegato» dal cookie di sessione; i «Post
  singolo» per gli indirizzi scaduti non vengono più annullati;
- febe-sonda da-har; la ripulitura tiene latest_reel_media e mette un
  riempitivo neutro sotto le chiavi simili a segreti (gitleaks).

Prove su Windows: 783 superate, 76 saltate (Chromium e Xvfb, nella CI).
Nulla è stato provato contro Instagram. Dettagli e decisioni in
docs/stato.md e docs/decisioni.md (righe del 25 ottobre).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-10-25 14:22:16 +01:00
1 parent b3a7c31e5a
commit 31bbbf8bb7
69 files changed
+9184 -453

No files matched your search

+210 -4
View File
@@ -8,6 +8,8 @@ using Febe.Browser.Download;
using Febe.Browser.Hosting;
using Febe.Browser.RemoteView;
using Febe.Core.Collection;
using Febe.Core.Collection.Readers;
using Febe.Core.Collection.Recipes;
using Febe.Tests.Browser.TestSupport;
namespace Febe.Tests.Browser;
@@ -59,6 +61,174 @@ public sealed class CaptureUnitTests
Assert.AreEqual(2, cdp.Sent.Count(c => c.Method == "Network.getResponseBody"), "Only JSON bodies are fetched.");
}
[TestMethod]
public async Task JavaScriptAnswersOfThePageAreDataButScriptFilesAreNot()
{
// Instagram serves its GraphQL data as «text/javascript» (HAR of 21/10/2026).
var clock = new VirtualClock();
var cdp = new FakeCdpSession(clock);
cdp.Responder = (method, p) => method switch
{
"Page.getFrameTree" => new JsonObject { ["frameTree"] = new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["url"] = "about:blank" } } },
"Network.getResponseBody" when p!["requestId"]!.GetValue<string>() == "g1" => new JsonObject { ["body"] = """{"data":{"x":[1]}}""", ["base64Encoded"] = false },
"Network.getResponseBody" when p!["requestId"]!.GetValue<string>() == "a1" => new JsonObject { ["body"] = """for (;;);{"payload":{}}""", ["base64Encoded"] = false },
"Network.getResponseBody" => new JsonObject { ["body"] = "var x = 1;", ["base64Encoded"] = false },
_ => null,
};
var consumer = new CollectingConsumer();
var observer = new CollectingObserver();
await using var tap = new ResponseTap(cdp, Hosts, clock);
tap.AddConsumer(consumer);
tap.AddObserver(observer);
await tap.StartAsync();
cdp.Raise("Network.responseReceived", Received("g1", "http://127.0.0.1:5000/graphql/query", 200, "text/javascript; charset=utf-8", "XHR"));
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "g1", ["encodedDataLength"] = 20 });
cdp.Raise("Network.responseReceived", Received("s1", "http://127.0.0.1:5001/bundle.js", 200, "text/javascript", "Script"));
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "s1", ["encodedDataLength"] = 10 });
cdp.Raise("Network.responseReceived", Received("a1", "http://127.0.0.1:5000/ajax/navigation/", 200, "text/javascript", "XHR"));
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "a1", ["encodedDataLength"] = 24 });
await WaitUntilAsync(() => consumer.Items.Count == 1 && observer.Dropped.Contains("a1"));
Assert.AreEqual("""{"data":{"x":[1]}}""", consumer.Items.Single().Json);
Assert.AreEqual("XHR", consumer.Items.Single().Descriptor.ResourceType);
Assert.IsFalse(cdp.Sent.Any(c => c.Method == "Network.getResponseBody" && c.Parameters?["requestId"]?.GetValue<string>() == "s1"), "A script file is never fetched.");
Assert.AreEqual(1, tap.Stats.ParseErrors, "«for (;;);» is not JSON: skipped, and its waiters are told.");
}
[TestMethod]
public async Task EveryJsonBlockOfThePageDocumentIsAnAnswerOfItsOwn()
{
var clock = new VirtualClock();
var cdp = new FakeCdpSession(clock);
var tooDeep = new string('[', 300) + new string(']', 300);
var html = $$"""
<html><head><script type="application/json" data-sjs>{"a":1}</script><script>var b = 2;</script>
<script type="application/json">{ non è json </script><script type="application/json">{{tooDeep}}</script>
<script type="application/json"> </script></head>
<body><script type="application/json">{"c":[3]}</script></body></html>
""";
cdp.Responder = (method, p) => method switch
{
"Page.getFrameTree" => new JsonObject { ["frameTree"] = new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["url"] = "about:blank" } } },
"Network.getResponseBody" when p!["requestId"]!.GetValue<string>() == "L7" => new JsonObject { ["body"] = html, ["base64Encoded"] = false },
"Network.getResponseBody" when p!["requestId"]!.GetValue<string>() == "r10" => new JsonObject { ["body"] = """{"d":4}""", ["base64Encoded"] = false },
"Network.getResponseBody" => new JsonObject { ["body"] = "<html><body>niente</body></html>", ["base64Encoded"] = false },
_ => null,
};
var failing = new FailingConsumer();
var consumer = new CollectingConsumer();
var observer = new CollectingObserver();
await using var tap = new ResponseTap(cdp, Hosts, clock);
tap.AddConsumer(failing);
tap.AddConsumer(consumer);
tap.AddObserver(observer);
await tap.StartAsync();
// The frame commits the new document before its body has finished loading: the body is still read.
cdp.Raise("Network.responseReceived", Document("L7", "http://127.0.0.1:5000/marta.prova/"));
cdp.Raise("Page.frameNavigated", new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["loaderId"] = "L7", ["url"] = "http://127.0.0.1:5000/marta.prova/" } });
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "L7", ["encodedDataLength"] = html.Length });
// A document without embedded JSON gives nothing; one of another host is not even fetched. A data answer after
// them: the worker reads in order, so once it has arrived the documents have been dealt with.
cdp.Raise("Network.responseReceived", Document("L8", "http://127.0.0.1:5000/vuota/"));
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "L8", ["encodedDataLength"] = 30 });
cdp.Raise("Network.responseReceived", Document("L9", "https://esterno.example/"));
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "L9", ["encodedDataLength"] = 30 });
cdp.Raise("Network.responseReceived", Received("r10", "http://127.0.0.1:5000/api/dopo", 200, "application/json"));
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "r10", ["encodedDataLength"] = 7 });
await WaitUntilAsync(() => consumer.Items.Any(i => i.Descriptor.RequestId == "r10"));
// One answer per valid block, in the order of the document, each with the document's descriptor: a block that
// is not JSON, is too deep or is blank is left out without being a parse error.
var blocks = consumer.Items.Where(i => i.Descriptor.RequestId == "L7").ToList();
CollectionAssert.AreEqual(new[] { """{"a":1}""", """{"c":[3]}""" }, blocks.Select(b => b.Json).ToArray());
Assert.IsTrue(blocks.All(b => b.Descriptor.IsMainFrameDocument && b.Descriptor.MimeType == "text/html" && b.Descriptor.Path == "/marta.prova/"));
Assert.AreEqual(0, tap.Stats.ParseErrors);
Assert.AreEqual(3, tap.Stats.JsonBodies, "Two blocks and the answer after them.");
// A reader that fails on a block stops neither the other readers, nor the next block, nor the next answer.
Assert.AreEqual(3, failing.Calls);
// Those who wait for a body are told once when nothing reached the readers, never when blocks did.
Assert.IsFalse(observer.Dropped.Contains("L7"));
Assert.AreEqual(1, observer.Dropped.Count(id => id == "L8"));
Assert.IsFalse(cdp.Sent.Any(c => c.Method == "Network.getResponseBody" && c.Parameters?["requestId"]?.GetValue<string>() == "L9"));
}
[TestMethod]
public async Task ThePaginationOfAnEmbeddedBlockStaysWithTheBlockThatCarriesIt()
{
// Two real answers (cleaned samples of the HAR of 21/10/2026) embedded in one page document: first the
// collections, which say «has_next_page: true» of themselves, then a post, which says «more_available: false»
// of itself. Read as one answer, the post took the first pagination of the document, the collections'.
var clock = new VirtualClock();
var cdp = new FakeCdpSession(clock);
var html = $"""
<!DOCTYPE html><html><head><script type="application/json">{SampleBody("raccolta-in-evidenza")}</script></head>
<body><script type="application/json">{SampleBody("post-video")}</script></body></html>
""";
cdp.Responder = (method, p) => method switch
{
"Page.getFrameTree" => new JsonObject { ["frameTree"] = new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["url"] = "about:blank" } } },
"Network.getResponseBody" when p!["requestId"]!.GetValue<string>() == "L7" => new JsonObject { ["body"] = Convert.ToBase64String(Encoding.UTF8.GetBytes(html)), ["base64Encoded"] = true },
"Network.getResponseBody" => new JsonObject { ["body"] = """{"segni":[1]}""", ["base64Encoded"] = false },
_ => null,
};
var capture = new CaptureCollector(new ShapeReader(ReaderVocabulary.LoadInstagram()), clock);
var after = new CollectingConsumer();
await using var tap = new ResponseTap(cdp, Hosts, clock);
tap.AddConsumer(capture);
tap.AddObserver(capture);
tap.AddConsumer(after);
await tap.StartAsync();
cdp.Raise("Network.responseReceived", Document("L7", "http://127.0.0.1:5000/marta.prova/"));
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "L7", ["encodedDataLength"] = html.Length });
cdp.Raise("Network.responseReceived", Received("r2", "http://127.0.0.1:5000/api/dopo", 200, "application/json"));
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "r2", ["encodedDataLength"] = 7 });
await WaitUntilAsync(() => after.Items.Any(i => i.Descriptor.RequestId == "r2"));
var reads = capture.Since(0);
Assert.AreEqual(2, reads.Count, "One read per block (the answer after them is not about contents).");
Assert.IsTrue(reads.All(r => r.Response.RequestId == "L7" && r.Response.IsMainFrameDocument));
var collections = reads[0].Result;
Assert.AreEqual(2, collections.Highlights.Count);
Assert.AreEqual(true, collections.Page?.HasMore);
Assert.IsFalse(string.IsNullOrEmpty(collections.Page?.Cursor));
var post = reads[1].Result;
Assert.AreEqual(0, post.Highlights.Count);
Assert.AreEqual(1, post.Contents.Count);
Assert.AreEqual(false, post.Page?.HasMore, "The post's own pagination, not the collections'.");
Assert.IsNull(post.Page?.Cursor);
Assert.AreEqual(0, capture.Pending, "Nothing is left on its way.");
}
[TestMethod]
public async Task BodiesThatWillNotComeAreReportedToObservers()
{
var clock = new VirtualClock();
var cdp = new FakeCdpSession(clock);
cdp.Responder = (method, _) => method == "Page.getFrameTree"
? new JsonObject { ["frameTree"] = new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["url"] = "about:blank" } } }
: null;
var observer = new CollectingObserver();
await using var tap = new ResponseTap(cdp, Hosts, clock, options: new ResponseTapOptions { MaxBodyBytes = 100 });
tap.AddConsumer(new CollectingConsumer());
tap.AddObserver(observer);
await tap.StartAsync();
cdp.Raise("Network.responseReceived", Received("big", "http://127.0.0.1:5000/a", 200, "application/json"));
cdp.Raise("Network.loadingFinished", new JsonObject { ["requestId"] = "big", ["encodedDataLength"] = 1000 });
cdp.Raise("Network.responseReceived", Received("fail", "http://127.0.0.1:5000/b", 200, "application/json"));
cdp.Raise("Network.loadingFailed", new JsonObject { ["requestId"] = "fail" });
cdp.Raise("Network.responseReceived", Received("old", "http://127.0.0.1:5000/c", 200, "application/json"));
cdp.Raise("Page.frameNavigated", new JsonObject { ["frame"] = new JsonObject { ["id"] = "F1", ["loaderId"] = "L2", ["url"] = "http://127.0.0.1:5000/altra/" } });
CollectionAssert.AreEquivalent(new[] { "big", "fail", "old" }, observer.Dropped.ToArray());
}
[TestMethod]
public async Task MainFrameNavigationsAreReported()
{
@@ -229,15 +399,25 @@ public sealed class CaptureUnitTests
Assert.IsFalse(gate.IsDriving);
}
private static JsonObject Received(string id, string url, int status, string mime) => new()
private static JsonObject Received(string id, string url, int status, string mime, string type = "Fetch") => new()
{
["requestId"] = id,
["loaderId"] = "L1",
["frameId"] = "F1",
["type"] = "Fetch",
["type"] = type,
["response"] = new JsonObject { ["url"] = url, ["status"] = status, ["mimeType"] = mime },
};
/// <summary>The response of a main-frame navigation: its request id is the loader id.</summary>
private static JsonObject Document(string loader, string url) => new()
{
["requestId"] = loader,
["loaderId"] = loader,
["frameId"] = "F1",
["type"] = "Document",
["response"] = new JsonObject { ["url"] = url, ["status"] = 200, ["mimeType"] = "text/html; charset=utf-8" },
};
private static async Task WaitUntilAsync(Func<bool> condition)
{
for (var i = 0; i < 300 && !condition(); i++)
@@ -248,17 +428,39 @@ public sealed class CaptureUnitTests
Assert.IsTrue(condition(), "Condition not reached in time.");
}
/// <summary>The body of a cleaned Instagram sample (<c>tests/campioni/instagram</c>), as JSON text.</summary>
private static string SampleBody(string name)
{
var file = Directory.EnumerateFiles(Path.Combine(AppContext.BaseDirectory, "campioni", "instagram"), $"*-{name}.json").Single();
using var sample = JsonDocument.Parse(File.ReadAllText(file));
return sample.RootElement.GetProperty("corpo").GetRawText();
}
private sealed class CollectingConsumer : IResponseConsumer
{
public ConcurrentBag<(ResponseDescriptor Descriptor, string Json)> Items { get; } = [];
/// <summary>In arrival order.</summary>
public ConcurrentQueue<(ResponseDescriptor Descriptor, string Json)> Items { get; } = new();
public ValueTask ConsumeAsync(ResponseDescriptor response, JsonDocument body, CancellationToken ct)
{
Items.Add((response, body.RootElement.GetRawText()));
Items.Enqueue((response, body.RootElement.GetRawText()));
return ValueTask.CompletedTask;
}
}
private sealed class FailingConsumer : IResponseConsumer
{
private int _calls;
public int Calls => Volatile.Read(ref _calls);
public ValueTask ConsumeAsync(ResponseDescriptor response, JsonDocument body, CancellationToken ct)
{
Interlocked.Increment(ref _calls);
throw new InvalidOperationException("Lettore di prova che fallisce sempre.");
}
}
private sealed class CollectingObserver : IResponseObserver
{
public ConcurrentQueue<ResponseDescriptor> Responses { get; } = new();
@@ -267,7 +469,11 @@ public sealed class CaptureUnitTests
public void OnResponse(ResponseDescriptor response) => Responses.Enqueue(response);
public ConcurrentQueue<string> Dropped { get; } = new();
public void OnNavigation(NavigationDescriptor navigation) => Navigations.Enqueue(navigation);
public void OnBodyDropped(string requestId) => Dropped.Enqueue(requestId);
}
private sealed class SyncProgress(List<long> values) : IProgress<long>