[CHANGE] Implementation video data
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using Manager.YouTube.Models.Innertube;
|
||||
|
||||
namespace Manager.YouTube.Parsers.Json;
|
||||
@@ -9,4 +11,63 @@ public static class JsonParser
|
||||
array
|
||||
.Select(image => new WebImage { Width = image.GetProperty("width").GetInt32(), Height = image.GetProperty("height").GetInt32(), Url = image.GetProperty("url").GetString() ?? "" })
|
||||
.ToList();
|
||||
|
||||
public static string ExtractTextOrHtml(JsonElement element)
|
||||
{
|
||||
// Case 1: Simple text (no formatting)
|
||||
if (element.TryGetProperty("simpleText", out var simpleText))
|
||||
return simpleText.GetString() ?? string.Empty;
|
||||
|
||||
// Case 2: Runs (formatted text segments)
|
||||
if (element.TryGetProperty("runs", out var runs) && runs.ValueKind == JsonValueKind.Array)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
|
||||
foreach (var run in runs.EnumerateArray())
|
||||
{
|
||||
var text = run.GetProperty("text").GetString() ?? string.Empty;
|
||||
var formatted = System.Net.WebUtility.HtmlEncode(text);
|
||||
|
||||
var bold = run.TryGetProperty("bold", out var boldProp) && boldProp.GetBoolean();
|
||||
var italic = run.TryGetProperty("italic", out var italicProp) && italicProp.GetBoolean();
|
||||
var underline = run.TryGetProperty("underline", out var underlineProp) && underlineProp.GetBoolean();
|
||||
var strikethrough = run.TryGetProperty("strikethrough", out var strikeProp) && strikeProp.GetBoolean();
|
||||
|
||||
if (bold) formatted = $"<b>{formatted}</b>";
|
||||
if (italic) formatted = $"<i>{formatted}</i>";
|
||||
if (underline) formatted = $"<u>{formatted}</u>";
|
||||
if (strikethrough) formatted = $"<s>{formatted}</s>";
|
||||
|
||||
if (run.TryGetProperty("navigationEndpoint", out var nav) &&
|
||||
nav.TryGetProperty("url", out var urlProp))
|
||||
{
|
||||
var url = urlProp.GetString();
|
||||
if (!string.IsNullOrEmpty(url))
|
||||
formatted = $"<a href=\"{url}\">{formatted}</a>";
|
||||
}
|
||||
|
||||
if (run.TryGetProperty("emoji", out var emoji) && emoji.ValueKind == JsonValueKind.Object)
|
||||
{
|
||||
if (emoji.TryGetProperty("url", out var emojiUrl))
|
||||
{
|
||||
var src = emojiUrl.GetString();
|
||||
if (!string.IsNullOrEmpty(src))
|
||||
formatted = $"<img src=\"{src}\" alt=\"{text}\" class=\"emoji\" />";
|
||||
}
|
||||
}
|
||||
|
||||
sb.Append(formatted);
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public static List<WebImage> ExtractWebImages(JsonElement element)
|
||||
{
|
||||
var thumbnailsArray = element.GetProperty("thumbnail").GetProperty("thumbnails");
|
||||
return thumbnailsArray.Deserialize<List<WebImage>>() ?? [];
|
||||
}
|
||||
}
|
||||
@@ -1,51 +1,35 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using DotBased.Logging;
|
||||
using DotBased.Monads;
|
||||
using Manager.YouTube.Models;
|
||||
using Manager.YouTube.Models.Parser;
|
||||
using Manager.YouTube.Util.Converters;
|
||||
|
||||
namespace Manager.YouTube.Parsers.Json;
|
||||
|
||||
public static class VideoJsonParser
|
||||
{
|
||||
private static readonly JsonSerializerOptions VideoParserOptions = new() { Converters = { new YouTubeVideoJsonConverter() } };
|
||||
private static readonly ILogger Logger = LogService.RegisterLogger(typeof(VideoJsonParser), "Video JSON parser");
|
||||
|
||||
public static Result<YouTubeVideo> ParseVideoData(YouTubeVideoData videoData)
|
||||
{
|
||||
if (videoData.YouTubeInitialData == null || videoData.YouTubeInitialData.Count == 0)
|
||||
{
|
||||
return ResultError.Fail("No initial video data found!");
|
||||
}
|
||||
|
||||
var videoDetails = videoData.YouTubeInitialData["videoDetails"];
|
||||
var microformat = videoData.YouTubeInitialData["microformat"]?["playerMicroformatRenderer"];
|
||||
if (videoDetails == null)
|
||||
|
||||
YouTubeVideo? video;
|
||||
try
|
||||
{
|
||||
return ResultError.Fail("No video details found!");
|
||||
video = videoData.YouTubeInitialData.Deserialize<YouTubeVideo>(VideoParserOptions);
|
||||
}
|
||||
|
||||
if (microformat == null)
|
||||
catch (Exception e)
|
||||
{
|
||||
return ResultError.Fail("No microformat found!");
|
||||
Logger.Error(e, "Failed to parse video data");
|
||||
return ResultError.Fail("Failed to parse video data");
|
||||
}
|
||||
|
||||
FlattenThumbnailArray(videoDetails);
|
||||
FlattenThumbnailArray(microformat);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
var video = videoDetails.Deserialize<YouTubeVideo>();
|
||||
|
||||
|
||||
return ResultError.Fail("Not implemented.");
|
||||
}
|
||||
|
||||
private static void FlattenThumbnailArray(JsonNode node)
|
||||
{
|
||||
var thumbnailsArray = node["thumbnail"]?["thumbnails"];
|
||||
if (thumbnailsArray != null)
|
||||
{
|
||||
node["thumbnail"]?.ReplaceWith(thumbnailsArray);
|
||||
}
|
||||
return video != null? video : ResultError.Fail("Failed to parse video data!");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user