From 0e2d5c76c554916e6b005a1ff61f3bd9b1d6ccf9 Mon Sep 17 00:00:00 2001 From: Foxocube Date: Fri, 10 Jan 2025 20:35:57 +0000 Subject: [PATCH] Change when thumbnail sync is done, also sync other details if already pulling metadata --- .../YoutubeVideoSynchroniseConsumer.cs | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/MediaFeeder/MediaFeeder/Providers/Youtube/YoutubeVideoSynchroniseConsumer.cs b/MediaFeeder/MediaFeeder/Providers/Youtube/YoutubeVideoSynchroniseConsumer.cs index ae70f65..1e3903f 100644 --- a/MediaFeeder/MediaFeeder/Providers/Youtube/YoutubeVideoSynchroniseConsumer.cs +++ b/MediaFeeder/MediaFeeder/Providers/Youtube/YoutubeVideoSynchroniseConsumer.cs @@ -1,3 +1,4 @@ +using System.Text.Json; using System.Xml; using Google.Apis.YouTube.v3; using MassTransit; @@ -10,7 +11,8 @@ namespace MediaFeeder.Providers.Youtube; public class YoutubeActualVideoSynchroniseConsumer( ILogger logger, IDbContextFactory contextFactory, - YouTubeService youTubeService + YouTubeService youTubeService, + Utils utils ) : IConsumer { public async Task Consume(ConsumeContext context) @@ -62,7 +64,7 @@ public async Task Consume(ConsumeContext } } - if (video.Duration == 0) + if (video.Duration == 0 || string.IsNullOrWhiteSpace(video.Thumb)) { var videoRequest = youTubeService.Videos.List("id,statistics,contentDetails"); videoRequest.Id = video.VideoId; @@ -79,7 +81,15 @@ public async Task Consume(ConsumeContext } if (videoStats?.Snippet != null) + { video.Description = videoStats.Snippet.Description; + video.Name = videoStats.Snippet.Title; + } + + if (videoStats?.Snippet?.Thumbnails != null) + video.Thumb = await utils.LoadResourceThumbnail(video.VideoId, "video", videoStats.Snippet.Thumbnails, logger, context.CancellationToken); + + logger.LogInformation("Got state for video from YT: {}", JsonSerializer.Serialize(videoStats)); } await db.SaveChangesAsync(context.CancellationToken); @@ -87,11 +97,8 @@ public async Task Consume(ConsumeContext } public class YoutubeVideoSynchroniseConsumer( - ILogger logger, IDbContextFactory contextFactory, - IBus bus, - Utils utils, - YouTubeService youTubeService + IBus bus ) : IConsumer { public async Task Consume(ConsumeContext context) @@ -102,17 +109,6 @@ public async Task Consume(ConsumeContext contex if (video.DownloadedPath != null || video.Duration == 0 || string.IsNullOrWhiteSpace(video.Thumb)) await bus.Publish(new YoutubeActualVideoSynchroniseContract(video.Id), context.CancellationToken); - if (video.Thumb == null) - { - var videoRequest = youTubeService.Videos.List("snippet"); - videoRequest.Id = video.VideoId; - var videoResponse = await videoRequest.ExecuteAsync(context.CancellationToken); - var videoStats = videoResponse.Items.SingleOrDefault(); - - if (videoStats != null) - video.Thumb = await utils.LoadResourceThumbnail(video.VideoId, "video", videoStats.Snippet.Thumbnails, logger, context.CancellationToken); - } - await db.SaveChangesAsync(context.CancellationToken); } }