35 lines
1.1 KiB
C#
35 lines
1.1 KiB
C#
using System.Text.Json;
|
|
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.YouTubePlayerData == null)
|
|
{
|
|
return ResultError.Fail("No initial video data found!");
|
|
}
|
|
|
|
YouTubeVideo? video;
|
|
try
|
|
{
|
|
video = videoData.YouTubePlayerData.Deserialize<YouTubeVideo>(VideoParserOptions);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Logger.Error(e, "Failed to parse video data");
|
|
return ResultError.Fail("Failed to parse video data");
|
|
}
|
|
|
|
return video != null? video : ResultError.Fail("Failed to parse video data!");
|
|
}
|
|
} |