87 lines
3.0 KiB
C#
87 lines
3.0 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<ChannelFetch> ParseJsonToChannelData(string json)
|
|
{
|
|
try
|
|
{
|
|
var doc = JsonDocument.Parse(json);
|
|
var rootDoc = doc.RootElement;
|
|
|
|
var microformat = rootDoc.GetProperty("microformat").GetProperty("microformatDataRenderer");
|
|
|
|
var availableCountries = microformat
|
|
.GetProperty("availableCountries")
|
|
.EnumerateArray()
|
|
.Select(e => e.GetString())
|
|
.ToList();
|
|
|
|
var description = microformat.GetProperty("description").GetString();
|
|
var noIndex = microformat.GetProperty("noindex").GetBoolean();
|
|
var unlisted = microformat.GetProperty("unlisted").GetBoolean();
|
|
var familySafe = microformat.GetProperty("familySafe").GetBoolean();
|
|
|
|
var avatarThumbnails = rootDoc
|
|
.GetProperty("metadata")
|
|
.GetProperty("channelMetadataRenderer")
|
|
.GetProperty("avatar")
|
|
.GetProperty("thumbnails")
|
|
.EnumerateArray();
|
|
|
|
var avatars = JsonParser.ParseImages(avatarThumbnails);
|
|
|
|
var headerContent = rootDoc
|
|
.GetProperty("header")
|
|
.GetProperty("pageHeaderRenderer")
|
|
.GetProperty("content");
|
|
|
|
var metadataPartHandle = 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();
|
|
|
|
var banners = JsonParser.ParseImages(bannerImages);
|
|
|
|
var resultFetch = new ChannelFetch
|
|
{
|
|
NoIndex = noIndex,
|
|
Unlisted = unlisted,
|
|
FamilySafe = familySafe,
|
|
Handle = metadataPartHandle,
|
|
Description = description,
|
|
AvailableCountries = availableCountries.OfType<string>().ToList(),
|
|
AvatarImages = avatars,
|
|
BannerImages = banners
|
|
};
|
|
return resultFetch;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return ResultError.Error(e);
|
|
}
|
|
}
|
|
} |