76 lines
2.8 KiB
C#
76 lines
2.8 KiB
C#
using System.Text.Json;
|
|
using DotBased.Monads;
|
|
using Manager.YouTube.Models.Innertube;
|
|
|
|
namespace Manager.YouTube.Parsers.Json;
|
|
|
|
/// <summary>
|
|
/// Parsing functionality for the response from the innertube browse endpoint.
|
|
/// </summary>
|
|
public static class ChannelJsonParser
|
|
{
|
|
public static Result<Channel> ParseJsonToChannelData(string json)
|
|
{
|
|
try
|
|
{
|
|
var channel = new Channel();
|
|
var doc = JsonDocument.Parse(json);
|
|
var rootDoc = doc.RootElement;
|
|
|
|
var microformat = rootDoc.GetProperty("microformat").GetProperty("microformatDataRenderer");
|
|
|
|
channel.AvailableCountries = microformat
|
|
.GetProperty("availableCountries")
|
|
.EnumerateArray()
|
|
.Select(e => e.GetString())
|
|
.OfType<string>().ToList();
|
|
channel.Description = microformat.GetProperty("description").GetString();
|
|
channel.NoIndex = microformat.GetProperty("noindex").GetBoolean();
|
|
channel.Unlisted = microformat.GetProperty("unlisted").GetBoolean();
|
|
channel.FamilySafe = microformat.GetProperty("familySafe").GetBoolean();
|
|
|
|
var channelMetadata = rootDoc
|
|
.GetProperty("metadata")
|
|
.GetProperty("channelMetadataRenderer");
|
|
channel.ChannelName = channelMetadata.GetProperty("title").GetString();
|
|
|
|
var avatarThumbnails = channelMetadata.GetProperty("avatar")
|
|
.GetProperty("thumbnails")
|
|
.EnumerateArray();
|
|
channel.AvatarImages = JsonParser.ParseImages(avatarThumbnails);
|
|
|
|
var headerContent = rootDoc
|
|
.GetProperty("header")
|
|
.GetProperty("pageHeaderRenderer")
|
|
.GetProperty("content");
|
|
|
|
channel.Handle = headerContent
|
|
.GetProperty("pageHeaderViewModel")
|
|
.GetProperty("metadata")
|
|
.GetProperty("contentMetadataViewModel")
|
|
.GetProperty("metadataRows")
|
|
.EnumerateArray()
|
|
.FirstOrDefault()
|
|
.GetProperty("metadataParts")
|
|
.EnumerateArray()
|
|
.FirstOrDefault()
|
|
.GetProperty("text")
|
|
.GetProperty("content").GetString();
|
|
|
|
var bannerImages = headerContent
|
|
.GetProperty("pageHeaderViewModel")
|
|
.GetProperty("banner")
|
|
.GetProperty("imageBannerViewModel")
|
|
.GetProperty("image")
|
|
.GetProperty("sources")
|
|
.EnumerateArray();
|
|
channel.BannerImages = JsonParser.ParseImages(bannerImages);
|
|
|
|
return channel;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return ResultError.Error(e);
|
|
}
|
|
}
|
|
} |