[CHANGE] Implementation video data
This commit is contained in:
@@ -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