[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,16 @@
@using Manager.App.Models.System
@using Manager.App.Services.System
@inject ISnackbar Snackbar
@inject ClientService ClientService
<MudText>Video data</MudText>
<MudStack Spacing="2">
<MudAutocomplete T="YouTubeClientItem" Label="Client" @bind-Value="@_selectedClient" SearchFunc="SearchClientsAsync" ToStringFunc="@(i => i == null ? "null?" : $"{i.Name} ({i.Handle})")"
Variant="Variant.Outlined" ShowProgressIndicator ProgressIndicatorColor="Color.Primary">
</MudAutocomplete>
<MudTextField Label="Video id" @bind-Value="@_videoId"/>
</MudStack>
<MudStack>
<MudButton OnClick="GetDataAsync">Get data</MudButton>
</MudStack>

View File

@@ -0,0 +1,56 @@
using Manager.App.Models.System;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using MudBlazor;
namespace Manager.App.Components.Application.Dev;
public partial class DevelopmentVideo : ComponentBase
{
private YouTubeClientItem? _selectedClient;
private string _videoId = "";
private async Task<IEnumerable<YouTubeClientItem>> SearchClientsAsync(string? search, CancellationToken cancellationToken)
{
var searchResults = await ClientService.GetClientsAsync(search, cancellationToken: cancellationToken);
return !searchResults.IsSuccess ? [] : searchResults.Value;
}
private async Task GetDataAsync(MouseEventArgs obj)
{
if (_selectedClient == null)
{
Snackbar.Add("No client selected!", Severity.Warning);
return;
}
if (string.IsNullOrWhiteSpace(_videoId))
{
Snackbar.Add("No video ID set!", Severity.Warning);
return;
}
if (_videoId.Length != 11)
{
Snackbar.Add("Video ID needs to have an length of 11 chars!", Severity.Warning);
}
var clientResult = await ClientService.LoadClientByIdAsync(_selectedClient.Id);
if (!clientResult.IsSuccess)
{
Snackbar.Add(clientResult.Error?.Description ?? $"Failed to get client with id: {_selectedClient.Id}", Severity.Error);
return;
}
var ytClient = clientResult.Value;
var videoResult = await ytClient.GetVideoByIdAsync(_videoId);
if (!videoResult.IsSuccess)
{
Snackbar.Add(videoResult.Error?.Description ?? $"Failed to load video: {_videoId}", Severity.Error);
return;
}
var ytVideo = videoResult.Value;
Snackbar.Add($"Loaded video {ytVideo.Title}", Severity.Success);
}
}

View File

@@ -6,4 +6,7 @@
<MudTabPanel Text="Authentication"> <MudTabPanel Text="Authentication">
<AuthenticationHasher /> <AuthenticationHasher />
</MudTabPanel> </MudTabPanel>
<MudTabPanel Text="Video">
<DevelopmentVideo />
</MudTabPanel>
</MudTabs> </MudTabs>

View File

@@ -101,6 +101,7 @@ public class LibraryService : ILibraryService
} }
else else
{ {
context.HttpCookies.RemoveRange(context.HttpCookies.Where(x => x.ClientId == client.Id));
context.ClientAccounts.Add(dbClient); context.ClientAccounts.Add(dbClient);
} }

View File

@@ -40,7 +40,7 @@ public class ClientService(IServiceScopeFactory scopeFactory, ILogger<ClientServ
} }
} }
public async Task<ListResult<YouTubeClientItem>> GetClientsAsync(string search, int offset = 0, int limit = 10, CancellationToken cancellationToken = default) public async Task<ListResult<YouTubeClientItem>> GetClientsAsync(string? search, int offset = 0, int limit = 10, CancellationToken cancellationToken = default)
{ {
if (_libraryService == null) if (_libraryService == null)
{ {

View File

@@ -1,8 +1,13 @@
using System.Text.Json.Serialization;
namespace Manager.YouTube.Models.Innertube; namespace Manager.YouTube.Models.Innertube;
public class ColorInfo public class ColorInfo
{ {
[JsonPropertyName("primaries")]
public string Primaries { get; set; } = ""; public string Primaries { get; set; } = "";
[JsonPropertyName("transferCharacteristics")]
public string TransferCharacteristics { get; set; } = ""; public string TransferCharacteristics { get; set; } = "";
[JsonPropertyName("matrixCoefficients")]
public string MatrixCoefficients { get; set; } = ""; public string MatrixCoefficients { get; set; } = "";
} }

View File

@@ -1,7 +1,11 @@
using System.Text.Json.Serialization;
namespace Manager.YouTube.Models.Innertube; namespace Manager.YouTube.Models.Innertube;
public class Range public class Range
{ {
[JsonPropertyName("start")]
public uint Start { get; set; } public uint Start { get; set; }
[JsonPropertyName("end")]
public uint End { get; set; } public uint End { get; set; }
} }

View File

@@ -1,10 +1,16 @@
using System.Text.Json.Serialization;
namespace Manager.YouTube.Models.Innertube; namespace Manager.YouTube.Models.Innertube;
public class StreamingData public class StreamingData
{ {
public DateTime FetchedUtc { get; set; } = DateTime.UtcNow; public DateTime FetchedUtc { get; set; } = DateTime.UtcNow;
[JsonPropertyName("expiresInSeconds")]
public int ExpiresInSeconds { get; set; } public int ExpiresInSeconds { get; set; }
[JsonPropertyName("serverAbrStreamingUrl")]
public string ServerAbrStreamingUrl { get; set; } = ""; public string ServerAbrStreamingUrl { get; set; } = "";
[JsonPropertyName("formats")]
public List<StreamingFormat> Formats { get; set; } = []; public List<StreamingFormat> Formats { get; set; } = [];
[JsonPropertyName("adaptiveFormats")]
public List<StreamingFormat> AdaptiveFormats { get; set; } = []; public List<StreamingFormat> AdaptiveFormats { get; set; } = [];
} }

View File

@@ -1,31 +1,59 @@
using System.Text.Json.Serialization;
namespace Manager.YouTube.Models.Innertube; namespace Manager.YouTube.Models.Innertube;
public class StreamingFormat public class StreamingFormat
{ {
[JsonPropertyName("itag")]
public int Itag { get; set; } public int Itag { get; set; }
[JsonPropertyName("url")]
public string? Url { get; set; } public string? Url { get; set; }
[JsonPropertyName("mimeType")]
public string MimeType { get; set; } = ""; public string MimeType { get; set; } = "";
[JsonPropertyName("bitrate")]
public uint Bitrate { get; set; } public uint Bitrate { get; set; }
[JsonPropertyName("width")]
public uint? Width { get; set; } public uint? Width { get; set; }
[JsonPropertyName("height")]
public uint? Height { get; set; } public uint? Height { get; set; }
[JsonPropertyName("initRange")]
public Range? InitRange { get; set; } public Range? InitRange { get; set; }
[JsonPropertyName("indexRange")]
public Range? IndexRange { get; set; } public Range? IndexRange { get; set; }
[JsonPropertyName("lastModified")]
public long LastModified { get; set; } public long LastModified { get; set; }
[JsonPropertyName("contentLength")]
public long ContentLength { get; set; } public long ContentLength { get; set; }
[JsonPropertyName("quality")]
public string Quality { get; set; } = ""; public string Quality { get; set; } = "";
[JsonPropertyName("xtags")]
public string? Xtags { get; set; } public string? Xtags { get; set; }
[JsonPropertyName("fps")]
public uint Fps { get; set; } public uint Fps { get; set; }
[JsonPropertyName("qualityLabel")]
public string QualityLabel { get; set; } = ""; public string QualityLabel { get; set; } = "";
[JsonPropertyName("projectionType")]
public string ProjectionType { get; set; } = ""; public string ProjectionType { get; set; } = "";
[JsonPropertyName("averagebitrate")]
public uint? AverageBitrate { get; set; } public uint? AverageBitrate { get; set; }
[JsonPropertyName("highReplication")]
public bool? HighReplication { get; set; } public bool? HighReplication { get; set; }
[JsonPropertyName("colorInfo")]
public ColorInfo? ColorInfo { get; set; } public ColorInfo? ColorInfo { get; set; }
[JsonPropertyName("audioQuality")]
public string? AudioQuality { get; set; } = ""; public string? AudioQuality { get; set; } = "";
[JsonPropertyName("approxDurationMs")]
public long ApproxDurationMs { get; set; } public long ApproxDurationMs { get; set; }
[JsonPropertyName("audioSampleRate")]
public int? AudioSampleRate { get; set; } public int? AudioSampleRate { get; set; }
[JsonPropertyName("audioChannels")]
public int? AudioChannels { get; set; } public int? AudioChannels { get; set; }
[JsonPropertyName("loudnessDb")]
public double? LoudnessDb { get; set; } public double? LoudnessDb { get; set; }
[JsonPropertyName("isDrc")]
public bool? IsDrc { get; set; } public bool? IsDrc { get; set; }
[JsonPropertyName("signatureCipher")]
public string? SignatureCipher { get; set; } public string? SignatureCipher { get; set; }
[JsonPropertyName("qualityOrdinal")]
public string QualityOrdinal { get; set; } = ""; public string QualityOrdinal { get; set; } = "";
} }

View File

@@ -1,8 +1,13 @@
using System.Text.Json.Serialization;
namespace Manager.YouTube.Models.Innertube; namespace Manager.YouTube.Models.Innertube;
public class WebImage public class WebImage
{ {
[JsonPropertyName("width")]
public int Width { get; set; } public int Width { get; set; }
[JsonPropertyName("height")]
public int Height { get; set; } public int Height { get; set; }
[JsonPropertyName("url")]
public string Url { get; set; } = ""; public string Url { get; set; } = "";
} }

View File

@@ -4,6 +4,6 @@ namespace Manager.YouTube.Models.Parser;
public class YouTubeVideoData public class YouTubeVideoData
{ {
public JsonObject? YouTubePlayerData { get; set; } public JsonNode? YouTubePlayerData { get; set; }
public JsonObject? YouTubeInitialData { get; set; } public JsonNode? YouTubeInitialData { get; set; }
} }

View File

@@ -74,8 +74,8 @@ public static class HtmlParser
{ {
return new YouTubeVideoData return new YouTubeVideoData
{ {
YouTubePlayerData = parsedPlayerInitialData?.AsObject(), YouTubePlayerData = parsedPlayerInitialData,
YouTubeInitialData = parsedInitialData?.AsObject() YouTubeInitialData = parsedInitialData
}; };
} }
catch (Exception e) catch (Exception e)

View File

@@ -12,45 +12,58 @@ public static class JsonParser
.Select(image => new WebImage { Width = image.GetProperty("width").GetInt32(), Height = image.GetProperty("height").GetInt32(), Url = image.GetProperty("url").GetString() ?? "" }) .Select(image => new WebImage { Width = image.GetProperty("width").GetInt32(), Height = image.GetProperty("height").GetInt32(), Url = image.GetProperty("url").GetString() ?? "" })
.ToList(); .ToList();
public static string ExtractTextOrHtml(JsonElement element) public static string ExtractTextOrHtml(JsonNode? node)
{ {
if (node is not JsonObject nodeObj)
{
return "";
}
// Case 1: Simple text (no formatting) // Case 1: Simple text (no formatting)
if (element.TryGetProperty("simpleText", out var simpleText)) if (nodeObj.TryGetPropertyValue("simpleText", out var simpleText))
return simpleText.GetString() ?? string.Empty; return simpleText?.GetValue<string>() ?? string.Empty;
// Case 2: Runs (formatted text segments) // Case 2: Runs (formatted text segments)
if (element.TryGetProperty("runs", out var runs) && runs.ValueKind == JsonValueKind.Array) if (nodeObj.TryGetPropertyValue("runs", out var runs) && runs != null && runs.GetValueKind() == JsonValueKind.Array)
{ {
var sb = new StringBuilder(); var sb = new StringBuilder();
foreach (var run in runs.EnumerateArray()) foreach (var runNode in runs.AsArray())
{ {
var text = run.GetProperty("text").GetString() ?? string.Empty; if (runNode is not JsonObject run)
{
continue;
}
var text = runNode["text"]?.GetValue<string>() ?? string.Empty;
var formatted = System.Net.WebUtility.HtmlEncode(text); var formatted = System.Net.WebUtility.HtmlEncode(text);
var bold = run.TryGetProperty("bold", out var boldProp) && boldProp.GetBoolean(); var bold = run.TryGetPropertyValue("bold", out var boldNode) && boldNode is JsonValue bv && bv.GetValue<bool>();
var italic = run.TryGetProperty("italic", out var italicProp) && italicProp.GetBoolean();
var underline = run.TryGetProperty("underline", out var underlineProp) && underlineProp.GetBoolean(); var italic = run.TryGetPropertyValue("italic", out var italicNode) && italicNode is JsonValue iv && iv.GetValue<bool>();
var strikethrough = run.TryGetProperty("strikethrough", out var strikeProp) && strikeProp.GetBoolean();
var underline = run.TryGetPropertyValue("underline", out var underlineNode) && underlineNode is JsonValue uv && uv.GetValue<bool>();
var strikethrough = run.TryGetPropertyValue("strikethrough", out var strikeNode) && strikeNode is JsonValue sv && sv.GetValue<bool>();
if (bold) formatted = $"<b>{formatted}</b>"; if (bold) formatted = $"<b>{formatted}</b>";
if (italic) formatted = $"<i>{formatted}</i>"; if (italic) formatted = $"<i>{formatted}</i>";
if (underline) formatted = $"<u>{formatted}</u>"; if (underline) formatted = $"<u>{formatted}</u>";
if (strikethrough) formatted = $"<s>{formatted}</s>"; if (strikethrough) formatted = $"<s>{formatted}</s>";
if (run.TryGetProperty("navigationEndpoint", out var nav) && if (run.TryGetPropertyValue("navigationEndpoint", out var nav) && nav is JsonObject navObj &&
nav.TryGetProperty("url", out var urlProp)) navObj.TryGetPropertyValue("url", out var urlProp))
{ {
var url = urlProp.GetString(); var url = urlProp?.GetValue<string>();
if (!string.IsNullOrEmpty(url)) if (!string.IsNullOrEmpty(url))
formatted = $"<a href=\"{url}\">{formatted}</a>"; formatted = $"<a href=\"{url}\">{formatted}</a>";
} }
if (run.TryGetProperty("emoji", out var emoji) && emoji.ValueKind == JsonValueKind.Object) if (run.TryGetPropertyValue("emoji", out var emoji) && emoji is JsonObject emojiObj)
{ {
if (emoji.TryGetProperty("url", out var emojiUrl)) if (emojiObj.TryGetPropertyValue("url", out var emojiUrl))
{ {
var src = emojiUrl.GetString(); var src = emojiUrl?.GetValue<string>();
if (!string.IsNullOrEmpty(src)) if (!string.IsNullOrEmpty(src))
formatted = $"<img src=\"{src}\" alt=\"{text}\" class=\"emoji\" />"; formatted = $"<img src=\"{src}\" alt=\"{text}\" class=\"emoji\" />";
} }
@@ -65,9 +78,14 @@ public static class JsonParser
return string.Empty; return string.Empty;
} }
public static List<WebImage> ExtractWebImages(JsonElement element) public static List<WebImage> ExtractWebImages(JsonNode? node)
{ {
var thumbnailsArray = element.GetProperty("thumbnail").GetProperty("thumbnails"); if (node == null)
return thumbnailsArray.Deserialize<List<WebImage>>() ?? []; {
return [];
}
var thumbnailsArray = node["thumbnails"];
return thumbnailsArray?.Deserialize<List<WebImage>>() ?? [];
} }
} }

View File

@@ -14,7 +14,7 @@ public static class VideoJsonParser
public static Result<YouTubeVideo> ParseVideoData(YouTubeVideoData videoData) public static Result<YouTubeVideo> ParseVideoData(YouTubeVideoData videoData)
{ {
if (videoData.YouTubeInitialData == null || videoData.YouTubeInitialData.Count == 0) if (videoData.YouTubePlayerData == null)
{ {
return ResultError.Fail("No initial video data found!"); return ResultError.Fail("No initial video data found!");
} }
@@ -22,7 +22,7 @@ public static class VideoJsonParser
YouTubeVideo? video; YouTubeVideo? video;
try try
{ {
video = videoData.YouTubeInitialData.Deserialize<YouTubeVideo>(VideoParserOptions); video = videoData.YouTubePlayerData.Deserialize<YouTubeVideo>(VideoParserOptions);
} }
catch (Exception e) catch (Exception e)
{ {

View File

@@ -39,7 +39,7 @@ public static class CipherManager
private static string GetCipherVersion(string relativePlayerUrl) private static string GetCipherVersion(string relativePlayerUrl)
{ {
var split = relativePlayerUrl.Split('/'); var split = relativePlayerUrl.Split('/', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
var v = split[2]; var v = split[2];
var lang = split[4]; var lang = split[4];
return $"{v}_{lang}"; return $"{v}_{lang}";

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

View File

@@ -175,10 +175,10 @@ public sealed class YouTubeClient : IDisposable
public async Task<Result> RotateCookiesPageAsync(string origin = NetworkService.Origin, int ytPid = 1) public async Task<Result> RotateCookiesPageAsync(string origin = NetworkService.Origin, int ytPid = 1)
{ {
if (IsAnonymous) /*if (IsAnonymous)
{ {
return ResultError.Fail("Anonymous clients cannot rotate cookies!"); return ResultError.Fail("Anonymous clients cannot rotate cookies!");
} }*/
if (string.IsNullOrWhiteSpace(origin)) if (string.IsNullOrWhiteSpace(origin))
{ {
@@ -277,6 +277,12 @@ public sealed class YouTubeClient : IDisposable
return stateResult; return stateResult;
} }
if (State is { LoggedIn: false })
{
_logger.Warning("Client is not logged in!");
return ResultError.Fail("Client login failed!");
}
var cookieRotationResult = await RotateCookiesPageAsync(); var cookieRotationResult = await RotateCookiesPageAsync();
return !cookieRotationResult.IsSuccess ? cookieRotationResult : Result.Success(); return !cookieRotationResult.IsSuccess ? cookieRotationResult : Result.Success();
} }