using System.Text;
using Febe.Core.Domain;
namespace Febe.Core.Collection.Readers;
/// One version of a file as the page received it: an address with its size.
/// Signed address: never logged (spec §13.2).
/// Width in pixels, when given.
/// Height in pixels, when given.
/// For videos: whether the rendition carries audio; null when the response does not say.
public sealed record FileVersion(string Url, int? Width, int? Height, bool? HasAudio)
{
public long Area => (long)(Width ?? 0) * (Height ?? 0);
private bool PrintMembers(StringBuilder builder)
{
builder.Append($"Width = {Width}, Height = {Height}, HasAudio = {HasAudio}");
return true;
}
}
/// One file of a content item, with the version chosen for download (spec §6.5 point 1).
/// Photo or video.
/// Identifier of this file of a series, when the site gives one.
/// Largest image, or best video version complete with audio.
/// For videos: the largest cover image.
/// Video duration, when given.
public sealed record ContentFile(MediaKind Kind, string? InstagramId, FileVersion Best, FileVersion? Cover, TimeSpan? Duration)
{
///
/// Place of the file in its series, from 1, counting the children the response declares even when one of them
/// cannot be read (no versions yet): a missing file leaves a gap instead of moving the following ones. Null for
/// a content with a single file.
///
public int? Position { get; init; }
}
/// A profile as it appears in a response (author of content, profile page, list of followed profiles).
public sealed record ProfileInfo
{
public required string Id { get; init; }
public required string Username { get; init; }
public string? DisplayName { get; init; }
public bool? IsPrivate { get; init; }
public bool? IsFollowed { get; init; }
public int? PostCount { get; init; }
/// Largest version of the profile picture.
public FileVersion? Picture { get; init; }
/// Publication time of the latest active story (stories bar).
public DateTimeOffset? LatestStoryAt { get; init; }
/// True when the response presents this profile as the connected account (H10).
public bool IsViewer { get; init; }
}
/// A content node: a post, a story or an item of a highlight collection.
public sealed record ContentItem
{
public required string Id { get; init; }
public string? Code { get; init; }
public required DateTimeOffset TakenAt { get; init; }
/// Caption: never logged.
public string? Caption { get; init; }
public ProfileInfo? Owner { get; init; }
public bool Pinned { get; init; }
public DateTimeOffset? ExpiresAt { get; init; }
/// The files in order (one for a single photo or video, several for a series).
public required IReadOnlyList Files { get; init; }
/// False when the series declares more files than the response carries: Post singolo completes it.
public bool IsComplete { get; init; } = true;
/// Identifier of the highlight collection the item was found in, if any.
public string? HighlightId { get; init; }
/// Order of appearance in the response, from 0.
public int Order { get; init; }
/// The response marks it as a reel, or it was found in the Reel tab (spec §7.2, «Cosa scaricare prima»).
public bool IsReel { get; init; }
private bool PrintMembers(StringBuilder builder)
{
builder.Append($"Id = {Id}, Code = {Code}, TakenAt = {TakenAt:O}, Owner = {Owner?.Username}, Pinned = {Pinned}, IsReel = {IsReel}, Files = {Files.Count}, IsComplete = {IsComplete}");
return true;
}
}
/// A highlight collection, with its contents when the response carries them.
public sealed record HighlightInfo
{
public required string Id { get; init; }
public required string Title { get; init; }
public ProfileInfo? Owner { get; init; }
public FileVersion? Cover { get; init; }
public IReadOnlyList Items { get; init; } = [];
public int Order { get; init; }
}
/// Pagination information of a page of data.
public sealed record PageInfo(bool? HasMore, string? Cursor);
/// What the reader made of a response.
public enum ReadShape
{
/// At least one content, profile or highlight was recognized.
Recognized,
/// Nothing to read (empty lists, a bare status): not an error, but counts as an empty page.
Empty,
/// A shape the vocabulary does not describe: event «formato non riconosciuto» (spec §6.4).
Unrecognized,
///
/// Nothing the vocabulary speaks about: none of its relevance keys appears anywhere (promotions, logging, a search
/// box). Ignored: neither data, nor an empty page, nor an unknown shape ().
///
Unrelated,
}
/// Everything one response gave, detached from the JSON document (which is disposed right after).
public sealed record ReadResult
{
public static ReadResult EmptyResult { get; } = new() { Shape = ReadShape.Empty };
public required ReadShape Shape { get; init; }
public IReadOnlyList Contents { get; init; } = [];
public IReadOnlyList Profiles { get; init; } = [];
public IReadOnlyList Highlights { get; init; } = [];
public PageInfo? Page { get; init; }
public ProfileInfo? Viewer { get; init; }
///
/// Identifier of the connected account, also when the answer describes it with nothing else (Instagram's
/// xdt_viewer.user): a profile with this identifier in another answer is the connected account (H10).
///
public string? ViewerId { get; init; }
/// For unrecognized responses: the key structure, without any value (spec §6.4).
public string? KeyStructure { get; init; }
///
/// The answer of the site's search box (issue #37), when this response is one. Its profiles are not in
/// , so a search answer never looks like a page of the followed list or a profile check.
///
public SearchAnswer? Search { get; init; }
}
/// The profiles a search box answer gave, in their order (issue #37). Never printed with names.
public sealed record SearchAnswer(IReadOnlyList Hits)
{
private bool PrintMembers(StringBuilder builder)
{
builder.Append($"Hits = {Hits.Count}");
return true;
}
}
/// One profile of a search answer. Private and followed are never in the answer: they stay null.
public sealed record SearchProfile(int Position, ProfileInfo Profile, bool? IsVerified)
{
private bool PrintMembers(StringBuilder builder)
{
builder.Append($"Position = {Position}, HasPicture = {Profile.Picture is not null}, IsVerified = {IsVerified}");
return true;
}
}