[CHANGE] Reworked parsers/converters. Decipher operation from script do not work!

This commit is contained in:
max
2025-10-24 00:23:09 +02:00
parent de28591d24
commit a84195aefa
18 changed files with 285 additions and 77 deletions

View File

@@ -0,0 +1,39 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Manager.YouTube.Util.Converters;
public class NumericJsonConverter<T> : JsonConverter<T> where T : struct, IConvertible
{
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
try
{
if (reader.TokenType == JsonTokenType.Number)
{
// Direct numeric value
return (T)Convert.ChangeType(reader.GetDouble(), typeof(T));
}
if (reader.TokenType == JsonTokenType.String)
{
var str = reader.GetString();
if (string.IsNullOrWhiteSpace(str))
throw new JsonException("Empty string cannot be converted to a number.");
return (T)Convert.ChangeType(str, typeof(T));
}
throw new JsonException($"Unexpected token {reader.TokenType} for type {typeof(T)}.");
}
catch (Exception ex)
{
throw new JsonException($"Error converting value to {typeof(T)}.", ex);
}
}
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
writer.WriteNumberValue(Convert.ToDouble(value));
}
}

View File

@@ -1,5 +1,6 @@
using System.Runtime.Serialization;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using DotBased.Logging;
using Manager.YouTube.Models;
@@ -11,56 +12,74 @@ namespace Manager.YouTube.Util.Converters;
public class YouTubeVideoJsonConverter : JsonConverter<YouTubeVideo>
{
private readonly ILogger _logger = LogService.RegisterLogger<YouTubeVideoJsonConverter>();
private readonly JsonSerializerOptions _serializerOptions = new()
{
Converters = {
new NumericJsonConverter<int>(),
new NumericJsonConverter<uint>(),
new NumericJsonConverter<long>(),
new NumericJsonConverter<double>(),
new NumericJsonConverter<decimal>() },
PropertyNameCaseInsensitive = true
};
public override YouTubeVideo Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
using var document = JsonDocument.ParseValue(ref reader);
var root = document.RootElement;
var node = JsonNode.Parse(ref reader);
if (node == null)
{
throw new SerializationException("Failed to parse JSON reader.");
}
var playabilityStatus = root.GetProperty("playabilityStatus");
var streamingData = root.GetProperty("streamingData");
var videoDetails = root.GetProperty("videoDetails");
var playerConfigJson = root.GetProperty("playerConfig");
var microformat = root.GetProperty("microformat").GetProperty("playerMicroformatRenderer");
var rootObject = node.AsObject();
var videoId = videoDetails.GetProperty("videoId").GetString() ?? microformat.GetProperty("externalVideoId").GetString();
var playabilityStatus = rootObject["playabilityStatus"];
var streamingDataJson = rootObject["streamingData"];
var videoDetails = rootObject["videoDetails"];
var playerConfigJson = rootObject["playerConfig"];
var microformat = rootObject["microformat"]?["playerMicroformatRenderer"];
var videoId = videoDetails?["videoId"]?.GetValue<string>() ?? microformat?["externalVideoId"]?.GetValue<string>();
if (string.IsNullOrEmpty(videoId))
{
throw new SerializationException("Failed to get videoId");
}
var thumbnails = JsonParser.ExtractWebImages(videoDetails?["thumbnail"]);
thumbnails.AddRange(JsonParser.ExtractWebImages(microformat?["thumbnail"]));
var thumbnails = JsonParser.ExtractWebImages(videoDetails.GetProperty("thumbnail"));
thumbnails.AddRange(JsonParser.ExtractWebImages(microformat.GetProperty("thumbnail")));
var streamingData = streamingDataJson.Deserialize<StreamingData>(_serializerOptions);
var playerConfig = ExtractPlayerConfig(playerConfigJson);
var video = new YouTubeVideo
{
VideoId = videoId,
Title = JsonParser.ExtractTextOrHtml(microformat.GetProperty("title")),
Description = JsonParser.ExtractTextOrHtml(microformat.GetProperty("description")),
ViewCount = videoDetails.GetProperty("viewCount").GetInt32(),
LikeCount = videoDetails.GetProperty("likeCount").GetInt32(),
ChannelId = videoDetails.GetProperty("channelId").GetString() ?? "",
Author = JsonParser.ExtractTextOrHtml(videoDetails.GetProperty("author")),
PlayabilityStatus = playabilityStatus.GetProperty("status").GetString() ?? "",
LengthSeconds = videoDetails.GetProperty("lengthSeconds").GetInt32(),
Keywords = videoDetails.GetProperty("keywords").EnumerateArray().Select(v => v.GetString()).Cast<string>().ToArray(),
IsOwnerViewing = videoDetails.GetProperty("isOwnerViewing").GetBoolean(),
AllowRating = videoDetails.GetProperty("allowRating").GetBoolean(),
IsCrawlable = videoDetails.GetProperty("isCrawlable").GetBoolean(),
IsPrivate = videoDetails.GetProperty("isPrivate").GetBoolean(),
IsUnpluggedCorpus = videoDetails.GetProperty("isUnpluggedCorpus").GetBoolean(),
IsLive = videoDetails.GetProperty("isLiveContent").GetBoolean(),
IsFamilySave = microformat.GetProperty("isFamilySave").GetBoolean(),
AvailableCountries = microformat.GetProperty("availableCountries").EnumerateArray().Select(v => v.GetString()).Cast<string>().ToArray(),
IsUnlisted = microformat.GetProperty("isUnlisted").GetBoolean(),
HasYpcMetadata = microformat.GetProperty("hasYpcMetadata").GetBoolean(),
PublishDate = microformat.GetProperty("publishDate").GetDateTime(),
UploadDate = microformat.GetProperty("uploadDate").GetDateTime(),
IsShortsEligible = microformat.GetProperty("isShortsEligible").GetBoolean(),
Category = microformat.GetProperty("category").GetString() ?? "",
StreamingData = streamingData.Deserialize<StreamingData>(),
Title = JsonParser.ExtractTextOrHtml(microformat?["title"]),
Description = JsonParser.ExtractTextOrHtml(microformat?["description"]),
ViewCount = long.TryParse(microformat?["viewCount"]?.GetValue<string>(), out var viewCountParsed) ? viewCountParsed : -1,
LikeCount = long.TryParse(microformat?["likeCount"]?.GetValue<string>(), out var likeCountParsed) ? likeCountParsed : -1,
ChannelId = videoDetails?["channelId"]?.GetValue<string>() ?? "",
Author = videoDetails?["author"]?.GetValue<string>() ?? "",
PlayabilityStatus = playabilityStatus?["status"]?.GetValue<string>() ?? "",
LengthSeconds = long.TryParse(videoDetails?["lengthSeconds"]?.GetValue<string>(), out var lengthSecondsParsed) ? lengthSecondsParsed : -1,
Keywords = videoDetails?["keywords"]?.AsArray().Select(v => v?.GetValue<string>() ?? "").ToArray() ?? [],
IsOwnerViewing = videoDetails?["isOwnerViewing"]?.GetValue<bool>() ?? false,
AllowRating = videoDetails?["allowRating"]?.GetValue<bool>() ?? false,
IsCrawlable = videoDetails?["isCrawlable"]?.GetValue<bool>() ?? false,
IsPrivate = videoDetails?["isPrivate"]?.GetValue<bool>() ?? false,
IsUnpluggedCorpus = videoDetails?["isUnpluggedCorpus"]?.GetValue<bool>() ?? false,
IsLive = videoDetails?["isLiveContent"]?.GetValue<bool>() ?? false,
IsFamilySave = microformat?["isFamilySave"]?.GetValue<bool>() ?? false,
AvailableCountries = microformat?["availableCountries"]?.AsArray().Select(v => v?.GetValue<string>() ?? "").ToArray() ?? [],
IsUnlisted = microformat?["isUnlisted"]?.GetValue<bool>() ?? false,
HasYpcMetadata = microformat?["hasYpcMetadata"]?.GetValue<bool>() ?? false,
PublishDate = DateTime.TryParse(microformat?["publishDate"]?.GetValue<string>(), out var parsedPublishDate) ? parsedPublishDate : DateTime.MinValue,
UploadDate = DateTime.TryParse(microformat?["uploadDate"]?.GetValue<string>(), out var parsedUploadDate) ? parsedUploadDate : DateTime.MinValue,
IsShortsEligible = microformat?["isShortsEligible"]?.GetValue<bool>() ?? false,
Category = microformat?["category"]?.GetValue<string>() ?? "",
StreamingData = streamingData,
Thumbnails = thumbnails,
PlayerConfig = ExtractPlayerConfig(playerConfigJson)
PlayerConfig = playerConfig
};
return video;
@@ -71,23 +90,25 @@ public class YouTubeVideoJsonConverter : JsonConverter<YouTubeVideo>
throw new NotImplementedException("Converter only supports reading.");
}
private PlayerConfig? ExtractPlayerConfig(JsonElement element)
private PlayerConfig? ExtractPlayerConfig(JsonNode? playerConfigNode)
{
if (playerConfigNode == null)
{
return null;
}
try
{
var playerConfigObj = playerConfigNode.AsObject();
var playerConfig = new PlayerConfig
{
AudioLoudnessDb = element.GetProperty("audioConfig").GetProperty("loudnessDb").GetDouble(),
AudioPerceptualLoudnessDb = element.GetProperty("audioConfig").GetProperty("perceptualLoudnessDb").GetDouble(),
AudioEnablePerFormatLoudness = element.GetProperty("audioConfig").GetProperty("enablePerFormatLoudness")
.GetBoolean(),
MaxBitrate = element.GetProperty("streamSelectionConfig").GetProperty("maxBitrate").GetUInt32(),
MaxReadAheadMediaTimeMs = element.GetProperty("mediaCommonConfig").GetProperty("dynamicReadaheadConfig")
.GetProperty("maxReadAheadMediaTimeMs").GetUInt32(),
MinReadAheadMediaTimeMs = element.GetProperty("mediaCommonConfig").GetProperty("dynamicReadaheadConfig")
.GetProperty("minReadAheadMediaTimeMs").GetUInt32(),
ReadAheadGrowthRateMs = element.GetProperty("mediaCommonConfig").GetProperty("dynamicReadaheadConfig")
.GetProperty("readAheadGrowthRateMs").GetUInt32(),
AudioLoudnessDb = playerConfigObj["audioConfig"]?["loudnessDb"]?.GetValue<double>() ?? 0,
AudioPerceptualLoudnessDb = playerConfigObj["audioConfig"]?["perceptualLoudnessDb"]?.GetValue<double>() ?? 0,
AudioEnablePerFormatLoudness = playerConfigObj["audioConfig"]?["enablePerFormatLoudness"]?.GetValue<bool>() ?? false,
MaxBitrate = uint.TryParse(playerConfigObj["streamSelectionConfig"]?["maxBitrate"]?.GetValue<string>(), out var parsedMaxBitrate) ? parsedMaxBitrate : 0,
MaxReadAheadMediaTimeMs = playerConfigObj["mediaCommonConfig"]?["dynamicReadaheadConfig"]?["maxReadAheadMediaTimeMs"]?.GetValue<uint>() ?? 0,
MinReadAheadMediaTimeMs = playerConfigObj["mediaCommonConfig"]?["dynamicReadaheadConfig"]?["minReadAheadMediaTimeMs"]?.GetValue<uint>() ?? 0,
ReadAheadGrowthRateMs = playerConfigObj["mediaCommonConfig"]?["dynamicReadaheadConfig"]?["readAheadGrowthRateMs"]?.GetValue<uint>() ?? 0,
};
return playerConfig;
}