[CHANGE] Split up json parsing, added getting account info

This commit is contained in:
max
2025-09-07 22:18:56 +02:00
parent 3db61b599d
commit b2c6003203
11 changed files with 198 additions and 48 deletions

View File

@@ -0,0 +1,87 @@
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);
}
}
}

View File

@@ -0,0 +1,43 @@
using System.Text.Json;
using DotBased.Monads;
namespace Manager.YouTube.Parsers.Json;
/// <summary>
/// Parsing functionality for the response from endpoint: /youtubei/v1/account/account_menu
/// </summary>
public static class JsonAccountParser
{
public static Result<string> ParseAccountId(string json)
{
try
{
var jsonDocument = JsonDocument.Parse(json);
var id = jsonDocument.RootElement
.GetProperty("actions")[0]
.GetProperty("openPopupAction")
.GetProperty("popup")
.GetProperty("multiPageMenuRenderer")
.GetProperty("header")
.GetProperty("activeAccountHeaderRenderer")
.GetProperty("manageAccountTitle")
.GetProperty("runs")
.EnumerateArray()
.FirstOrDefault()
.GetProperty("navigationEndpoint")
.GetProperty("browseEndpoint")
.GetProperty("browseId").GetString();
if (string.IsNullOrWhiteSpace(id))
{
return ResultError.Fail("Unable to get account id!");
}
return id;
}
catch (Exception e)
{
return ResultError.Error(e);
}
}
}

View File

@@ -0,0 +1,12 @@
using System.Text.Json;
using Manager.YouTube.Models.Innertube;
namespace Manager.YouTube.Parsers.Json;
public static class JsonParser
{
public static List<WebImage> ParseImages(JsonElement.ArrayEnumerator array) =>
array
.Select(image => new WebImage { Width = image.GetProperty("width").GetInt32(), Height = image.GetProperty("height").GetInt32(), Url = image.GetProperty("url").GetString() ?? "" })
.ToList();
}